Skip to content

Commit

Permalink
refactored event listener api, started impl; added todo macros to code
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed May 22, 2024
1 parent 8aa4d25 commit 2595d51
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
8 changes: 8 additions & 0 deletions include/gamepad/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ enum EventType {
};

class Button {
friend class Controller;
public:
bool rising_edge = false;
bool falling_edge = false;
bool is_pressed = false;
uint32_t last_press_time = pros::millis();
uint32_t last_release_time = last_press_time;
uint32_t time_held = 0;
uint32_t time_released = 0;
uint32_t long_press_threshold = 500;

uint32_t onPress(std::function<void(void)> func);
uint32_t onLongPress(std::function<void(void)> func);
uint32_t onRelease(std::function<void(void)> func);
uint32_t addListener(EventType event, std::function<void(void)> func);
bool removeListener(uint32_t id);
private:

void update(bool is_held);

EventHandler<> onPressEvent;
EventHandler<> onLongPressEvent;
EventHandler<> onReleaseEvent;
Expand All @@ -57,6 +64,7 @@ class Controller {
* @param joystick Which joystick axis's value to return
*/
float operator[](pros::controller_analog_e_t joystick);
TODO("hide memebrs and expose getters/const refs")
Button L1{}, L2{}, R1{}, R2{},
Up{}, Down{}, Left{}, Right{},
X{}, B{}, Y{}, A{};
Expand Down
4 changes: 3 additions & 1 deletion include/gamepad/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <map>
#include <atomic>

#include "gamepad/todo.hpp"
#include "pros/rtos.hpp"

namespace Gamepad {
Expand All @@ -31,7 +32,8 @@ class EventHandler {
bool remove_listener(uint32_t id) {
std::lock_guard lock(mutex);
if(listeners.find(id) == listeners.end()) {
return false; // TODO: change handling?
TODO("change handling maybe?")
return false;
}
listeners.erase(id);
return true;
Expand Down
57 changes: 46 additions & 11 deletions src/gamepad/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
#include "gamepad/controller.hpp"
#include "gamepad/todo.hpp"

namespace Gamepad {

uint32_t Button::onPress(std::function<void(void)> func) {
return this->onPressEvent.add_listener(std::move(func));
}

uint32_t Button::onLongPress(std::function<void(void)> func) {
return this->onLongPressEvent.add_listener(std::move(func));
}

uint32_t Button::onRelease(std::function<void(void)> func) {
return this->onReleaseEvent.add_listener(std::move(func));
}

void Button::update(const bool is_held) {
static uint32_t last_update_time = pros::millis();

this->rising_edge = !this->is_pressed && is_held;
this->falling_edge = this->is_pressed && !is_held;
this->is_pressed = is_held;
if (is_held) {
this->time_held += pros::millis() - last_update_time;
} else {
this->time_released += pros::millis() - last_update_time;
}
if (this->rising_edge) {
this->time_held = 0;
}
if (this->falling_edge) {
this->time_released = 0;
}

if (this->rising_edge) {
onPressEvent.fire();
} else if (this->falling_edge) {
onReleaseEvent.fire();
}
TODO("implement longPress");
last_update_time = pros::millis();
}

void Controller::updateButton(pros::controller_digital_e_t button_id) {
Button Controller::* button = Controller::button_to_ptr(button_id);
bool is_held = this->controller.get_digital(button_id);
(this->*button).rising_edge = !(this->*button).is_pressed && is_held;
(this->*button).falling_edge = (this->*button).is_pressed && !is_held;
(this->*button).is_pressed = is_held;
if ((this->*button).rising_edge) {
(this->*button).last_press_time = pros::millis();
}
if ((this->*button).falling_edge) {
(this->*button).last_release_time = pros::millis();
}
(this->*button).update(is_held);
}

void Controller::update() {
Expand All @@ -36,7 +69,8 @@ float Controller::operator[](pros::controller_analog_e_t axis) {
case ANALOG_LEFT_Y: return this->LeftY;
case ANALOG_RIGHT_X: return this->RightX;
case ANALOG_RIGHT_Y: return this->RightY;
default: std::exit(1); // TODO: change handling
TODO("change handling for default")
default: std::exit(1);
}
}

Expand All @@ -54,7 +88,8 @@ Button Controller::* Controller::button_to_ptr(pros::controller_digital_e_t butt
case DIGITAL_B: return &Controller::B;
case DIGITAL_Y: return &Controller::Y;
case DIGITAL_A: return &Controller::A;
default: std::exit(1); // TODO: change handling
TODO("change handling for default")
default: std::exit(1);
}
}
}

0 comments on commit 2595d51

Please sign in to comment.