Skip to content

Commit

Permalink
feat: 🚧 Add ability to add transformations to gamepad
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Nov 4, 2024
1 parent 297295a commit 83c8315
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
33 changes: 29 additions & 4 deletions include/gamepad/controller.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "pros/misc.h"
#include <optional>
#include <string>
#include "button.hpp"
#include "joystick_modifier.hpp"
Expand Down Expand Up @@ -78,13 +79,35 @@ class Gamepad {

const Button& A() { return m_A; }

float LeftX() { return m_LeftX; }
float LeftX(bool use_curve = true) {
if (use_curve && m_left_transformation) return m_left_transformation->get_value({m_LeftX, m_LeftY}).first;
else return m_LeftX;
}

float LeftY() { return m_LeftY; }
float LeftY(bool use_curve = true) {
if (use_curve && m_left_transformation) return m_left_transformation->get_value({m_LeftX, m_LeftY}).second;
else return m_LeftY;
}

float RightX() { return m_RightX; }
float RightX(bool use_curve = true) {
if (use_curve && m_right_transformation)
return m_right_transformation->get_value({m_RightX, m_RightY}).first;
else return m_RightX;
}

float RightY() { return m_RightY; }
float RightY(bool use_curve = true) {
if (use_curve && m_right_transformation)
return m_right_transformation->get_value({m_RightX, m_RightY}).second;
else return m_RightY;
}

void set_left_transform(Transformation left_transformation) {
m_left_transformation = std::move(left_transformation);
}

void set_right_transform(Transformation right_transformation) {
m_right_transformation = std::move(right_transformation);
}

/// The master controller, same as @ref gamepad::master
static Gamepad master;
Expand All @@ -98,6 +121,8 @@ class Gamepad {
m_A {};
float m_LeftX = 0, m_LeftY = 0, m_RightX = 0, m_RightY = 0;
Button Fake {};
std::optional<Transformation> m_left_transformation {std::nullopt};
std::optional<Transformation> m_right_transformation {std::nullopt};
/**
* @brief Gets a unique name for a listener that will not conflict with user listener names.
*
Expand Down
12 changes: 10 additions & 2 deletions include/gamepad/joystick_modifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,23 @@ class Transformation final {
class TransformationBuilder final {
public:
template <std::derived_from<AbstractTransformation> T> TransformationBuilder(T first) {
m_transform.m_all_transforms.push_back(std::make_unique(std::move(first)));
m_transform.m_all_transforms.push_back(std::make_unique<T>(std::move(first)));
}

TransformationBuilder() = delete;

template <std::derived_from<AbstractTransformation> T> TransformationBuilder& and_then(T next) {
m_transform.m_all_transforms.push_back(std::make_unique(std::move(next)));
m_transform.m_all_transforms.push_back(std::make_unique<T>(std::move(next)));
return *this;
}

Transformation build() {
return std::move(m_transform);
}

operator Transformation() {
return std::move(m_transform);
}
private:
Transformation m_transform {};
};
Expand Down
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "main.h"
#include "gamepad/api.hpp"
#include "gamepad/controller.hpp"
#include "gamepad/joystick_modifier.hpp"
#include "pros/rtos.hpp"
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -52,6 +53,10 @@ void initialize() {
gamepad::master.A().onRepeatPress("aRepeatPress", aRepeatPress1);
// And we can use lambda's too
gamepad::master.X().onShortRelease("xShortRelease1", []() { printf("X Short Release!\n"); });

// set up controller curves:
gamepad::master.set_left_transform(
gamepad::TransformationBuilder(gamepad::Deadband(5, 5)).and_then(gamepad::ExpoCurve(2, 2)));
}

/**
Expand Down

0 comments on commit 83c8315

Please sign in to comment.