diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 7ba75ef..1c4cf9d 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -19,38 +19,38 @@ enum EventType { }; class Button { - friend class Controller; + 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; + 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 func) const; - uint32_t onLongPress(std::function func) const; - uint32_t onRelease(std::function func) const; - uint32_t addListener(EventType event, std::function func) const; - bool removeListener(uint32_t id) const; - explicit operator bool() const { - return is_pressed; - } - private: + uint32_t onPress(std::function func) const; + uint32_t onLongPress(std::function func) const; + uint32_t onRelease(std::function func) const; + uint32_t addListener(EventType event, std::function func) const; + bool removeListener(uint32_t id) const; - void update(bool is_held); + explicit operator bool() const { return is_pressed; } + private: + void update(bool is_held); - uint32_t last_update_time = pros::millis(); - mutable EventHandler<> onPressEvent{}; - mutable EventHandler<> onLongPressEvent{}; - mutable EventHandler<> onReleaseEvent{}; + uint32_t last_update_time = pros::millis(); + mutable EventHandler<> onPressEvent {}; + mutable EventHandler<> onLongPressEvent {}; + mutable EventHandler<> onReleaseEvent {}; }; class Controller { public: - explicit Controller(pros::controller_id_e_t id): controller(id) {} + explicit Controller(pros::controller_id_e_t id) + : controller(id) {} + /** * Updates the state of the gamepad (all joysticks and buttons), and also runs * any registered handlers. @@ -69,13 +69,11 @@ class Controller { */ 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{}; + Button L1 {}, L2 {}, R1 {}, R2 {}, Up {}, Down {}, Left {}, Right {}, X {}, B {}, Y {}, A {}; float LeftX = 0, LeftY = 0, RightX = 0, RightY = 0; private: - static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); + static Button Controller::*button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); pros::Controller controller; }; // namespace Gamepad -} \ No newline at end of file +} // namespace Gamepad \ No newline at end of file diff --git a/include/gamepad/event_handler.hpp b/include/gamepad/event_handler.hpp index 6bdc188..d4660a0 100644 --- a/include/gamepad/event_handler.hpp +++ b/include/gamepad/event_handler.hpp @@ -12,44 +12,46 @@ namespace Gamepad { class MonotonicCounter { - template friend class EventHandler; - static uint32_t next_value() { - static std::atomic counter = 0; - return ++counter; - } + template friend class EventHandler; + + static uint32_t next_value() { + static std::atomic counter = 0; + return ++counter; + } }; -template -class EventHandler { +template class EventHandler { public: - using Listener = std::function; - uint32_t add_listener(Listener func) { - std::lock_guard lock(mutex); - uint32_t id = MonotonicCounter::next_value(); - listeners.emplace(id, std::move(func)); - return id; - } - bool remove_listener(uint32_t id) { - std::lock_guard lock(mutex); - if(listeners.find(id) == listeners.end()) { - TODO("change handling maybe?") - return false; + using Listener = std::function; + + uint32_t add_listener(Listener func) { + std::lock_guard lock(mutex); + uint32_t id = MonotonicCounter::next_value(); + listeners.emplace(id, std::move(func)); + return id; + } + + bool remove_listener(uint32_t id) { + std::lock_guard lock(mutex); + if (listeners.find(id) == listeners.end()) { + TODO("change handling maybe?") + return false; + } + listeners.erase(id); + return true; + } + + bool is_empty() { + std::lock_guard lock(mutex); + return listeners.empty(); } - listeners.erase(id); - return true; - } - bool is_empty() { - std::lock_guard lock(mutex); - return listeners.empty(); - } - void fire(Args... args) { - std::lock_guard lock(mutex); - for(auto listener : listeners) { - listener.second(args...); + + void fire(Args... args) { + std::lock_guard lock(mutex); + for (auto listener : listeners) { listener.second(args...); } } - } private: - std::map listeners; - pros::Mutex mutex; + std::map listeners; + pros::Mutex mutex; }; -} \ No newline at end of file +} // namespace Gamepad \ No newline at end of file diff --git a/include/gamepad/todo.hpp b/include/gamepad/todo.hpp index 6d61cf6..c1e0b7b 100644 --- a/include/gamepad/todo.hpp +++ b/include/gamepad/todo.hpp @@ -1,5 +1,5 @@ #pragma once -#define DO_PRAGMA(x) _Pragma (#x) -#define TODO(x) DO_PRAGMA(message ("TODO - " #x)) -#define FIXME(x) DO_PRAGMA(warning ("FIXME - " #x)) \ No newline at end of file +#define DO_PRAGMA(x) _Pragma(#x) +#define TODO(x) DO_PRAGMA(message("TODO - " #x)) +#define FIXME(x) DO_PRAGMA(warning("FIXME - " #x)) \ No newline at end of file diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index cc7d994..f2a35a1 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -17,17 +17,15 @@ uint32_t Button::onRelease(std::function func) const { uint32_t Button::addListener(EventType event, std::function func) const { switch (event) { - case Gamepad::EventType::ON_PRESS: - this->onPress(std::move(func)); - case Gamepad::EventType::ON_LONG_PRESS: - this->onLongPress(std::move(func)); - case Gamepad::EventType::ON_RELEASE: - this->onRelease(std::move(func)); + case Gamepad::EventType::ON_PRESS: this->onPress(std::move(func)); + case Gamepad::EventType::ON_LONG_PRESS: this->onLongPress(std::move(func)); + case Gamepad::EventType::ON_RELEASE: this->onRelease(std::move(func)); } } bool Button::removeListener(uint32_t id) const { - return this->onPressEvent.remove_listener(id) || this->onLongPressEvent.remove_listener(id) || this->onReleaseEvent.remove_listener(id); + return this->onPressEvent.remove_listener(id) || this->onLongPressEvent.remove_listener(id) || + this->onReleaseEvent.remove_listener(id); } void Button::update(const bool is_held) { @@ -39,34 +37,28 @@ void Button::update(const bool is_held) { } else { this->time_released += pros::millis() - this->last_update_time; } - if (this->rising_edge) { - this->time_held = 0; - } - if (this->falling_edge) { - this->time_released = 0; - } + if (this->rising_edge) { this->time_held = 0; } + if (this->falling_edge) { this->time_released = 0; } if (this->rising_edge) { this->onPressEvent.fire(); } else if (this->is_pressed && this->time_held >= this->long_press_threshold) { TODO("change onLongPress handling if onPress is present") this->onLongPressEvent.fire(); - }else if (this->falling_edge) { + } else if (this->falling_edge) { this->onReleaseEvent.fire(); } this->last_update_time = pros::millis(); } void Controller::updateButton(pros::controller_digital_e_t button_id) { - Button Controller::* button = Controller::button_to_ptr(button_id); + Button Controller::*button = Controller::button_to_ptr(button_id); bool is_held = this->controller.get_digital(button_id); (this->*button).update(is_held); } void Controller::update() { - for(int i = DIGITAL_L1; i != DIGITAL_A; ++i) { - this->updateButton(static_cast(i)); - } + for (int i = DIGITAL_L1; i != DIGITAL_A; ++i) { this->updateButton(static_cast(i)); } this->LeftX = this->controller.get_analog(ANALOG_LEFT_X); this->LeftY = this->controller.get_analog(ANALOG_LEFT_Y); @@ -83,13 +75,12 @@ float Controller::operator[](pros::controller_analog_e_t axis) { case ANALOG_LEFT_X: return this->LeftX; case ANALOG_LEFT_Y: return this->LeftY; case ANALOG_RIGHT_X: return this->RightX; - case ANALOG_RIGHT_Y: return this->RightY; - TODO("change handling for default") + case ANALOG_RIGHT_Y: return this->RightY; TODO("change handling for default") default: std::exit(1); } } -Button Controller::* Controller::button_to_ptr(pros::controller_digital_e_t button) { +Button Controller::*Controller::button_to_ptr(pros::controller_digital_e_t button) { switch (button) { case DIGITAL_L1: return &Controller::L1; case DIGITAL_L2: return &Controller::L2; @@ -102,9 +93,8 @@ Button Controller::* Controller::button_to_ptr(pros::controller_digital_e_t butt case DIGITAL_X: return &Controller::X; case DIGITAL_B: return &Controller::B; case DIGITAL_Y: return &Controller::Y; - case DIGITAL_A: return &Controller::A; - TODO("change handling for default") + case DIGITAL_A: return &Controller::A; TODO("change handling for default") default: std::exit(1); } } -} \ No newline at end of file +} // namespace Gamepad \ No newline at end of file