Skip to content

Commit

Permalink
feat: 🚧 Start work on automatic motor-controller bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Sep 9, 2024
1 parent 8280f2f commit 6ae93e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/gamepad/api.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "gamepad/event_handler.hpp" // IWYU pragma: export
#include "gamepad/bindings.hpp" // IWYU pragma: export
#include "gamepad/controller.hpp" // IWYU pragma: export
9 changes: 9 additions & 0 deletions include/gamepad/bindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "controller.hpp"

namespace Gamepad::Bindings {

bool motorTwoButton(std::string name, pros::Motor motor, Button& fwd_button, Button& rev_button);
bool motorOneButton(std::string name, pros::Motor motor, Button& toggle_button, uint32_t speed = 127);
} // namespace Gamepad::Bindings
45 changes: 45 additions & 0 deletions src/gamepad/bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <memory>

#include "bindings.hpp"
#include "controller.hpp"

namespace Gamepad::Bindings {
bool motorTwoButton(std::string name, pros::Motor motor, Button& fwd_button, Button& rev_button) {
enum states { FWD, REV, NONE };

bool ret_val = true;
auto state = std::make_shared<states>(NONE);
ret_val &= fwd_button.onPress("Bindings" + name, [=]() {
*state = FWD;
motor.move(127);
});
ret_val &= fwd_button.onRelease("Bindings" + name, [=]() {
if (*state == FWD) {
*state = NONE;
motor.move(0);
}
});
ret_val &= rev_button.onPress("Bindings" + name, [=]() {
*state = REV;
motor.move(-127);
});
ret_val &= rev_button.onRelease("Bindings" + name, [=]() {
if (*state == REV) {
*state = NONE;
motor.move(0);
}
});
return ret_val;
}

bool motorOneButton(std::string name, pros::Motor motor, Button& toggle_button, uint32_t speed) {
bool ret_val = true;
auto state = std::make_shared<bool>(false);
ret_val &= toggle_button.onPress("Bindings" + name, [=]() {
if (*state) motor.move(speed);
else motor.move(0);
*state = !*state;
});
return ret_val;
}
} // namespace Gamepad::Bindings

0 comments on commit 6ae93e1

Please sign in to comment.