From 3ed4b4937c3576e05f24b47f419f33d1661765ad Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:47:34 -0700 Subject: [PATCH 1/5] refactor: :truck: split button class into seperate file from controller --- include/gamepad/button.hpp | 169 +++++++++++++++++++++++++++++++++ include/gamepad/controller.hpp | 168 +------------------------------- src/gamepad/button.cpp | 64 +++++++++++++ src/gamepad/controller.cpp | 60 +----------- 4 files changed, 235 insertions(+), 226 deletions(-) create mode 100644 include/gamepad/button.hpp create mode 100644 src/gamepad/button.cpp diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp new file mode 100644 index 0000000..1117174 --- /dev/null +++ b/include/gamepad/button.hpp @@ -0,0 +1,169 @@ +#pragma once + +#include + +#include "event_handler.hpp" + +namespace Gamepad { +enum EventType { + ON_PRESS, + ON_LONG_PRESS, + ON_RELEASE, + ON_SHORT_RELEASE, +}; + +class Button { + friend class Controller; + public: + /// Whether the button has just been pressed + bool rising_edge = false; + /// Whether the button has just been released + bool falling_edge = false; + /// Whether the button is currently held down + bool is_pressed = false; + /// How long the button has been held down + uint32_t time_held = 0; + /// How long the button has been released + uint32_t time_released = 0; + /// How long the threshold should be for the longPress and shortRelease events + uint32_t long_press_threshold = 500; + /** + * @brief Register a function to run when the button is pressed. + * + * @param listenerName The name of the listener, this must be a unique name + * @param func The function to run when the button is pressed, the function MUST NOT block + * @return true The listener was successfully registered + * @return false The listener was not successfully registered (there is already a listener with this name) + * + * @b Example: + * @code {.cpp} + * // Use a function... + * Gamepad::master.Down.onPress("downPress1", downPress1); + * // ...or a lambda + * Gamepad::master.Up.onPress("upPress1", []() { std::cout << "I was pressed!" << std::endl; }); + * @endcode + */ + bool onPress(std::string listenerName, std::function func) const; + /** + * @brief Register a function to run when the button is long pressed. + * + * By default, onLongPress will fire when the button has been held down for + * 500ms or more, this threshold can be adjusted by changing long_press_threshold. + * + * @warning When using this event along with onPress, both the onPress + * and onlongPress listeners may fire together. + * + * @param listenerName The name of the listener, this must be a unique name + * @param func The function to run when the button is long pressed, the function MUST NOT block + * @return true The listener was successfully registered + * @return false The listener was not successfully registered (there is already a listener with this name) + * + * @b Example: + * @code {.cpp} + * // Use a function... + * Gamepad::master.Left.onLongPress("fireCatapult", fireCatapult); + * // ...or a lambda + * Gamepad::master.Right.onLongPress("print_right", []() { std::cout << "Right button was long pressed!" << + * std::endl; }); + * @endcode + */ + bool onLongPress(std::string listenerName, std::function func) const; + /** + * @brief Register a function to run when the button is released. + * + * @param listenerName The name of the listener, this must be a unique name + * @param func The function to run when the button is released, the function MUST NOT block + * @return true The listener was successfully registered + * @return false The listener was not successfully registered (there is already a listener with this name) + * + * @b Example: + * @code {.cpp} + * // Use a function... + * Gamepad::master.X.onRelease("stopFlywheel", stopFlywheel); + * // ...or a lambda + * Gamepad::master.Y.onRelease("stopIntake", []() { intake.move(0); }); + * @endcode + */ + bool onRelease(std::string listenerName, std::function func) const; + /** + * @brief Register a function to run when the button is short released. + * + * By default, shortRelease will fire when the button has been released before 500ms, this threshold can be + * adjusted by changing long_press_threshold. + * + * @note This event will most likely be used along with the longPress event. + * + * @param listenerName The name of the listener, this must be a unique name + * @param func The function to run when the button is short released, the function MUST NOT block + * @return true The listener was successfully registered + * @return false The listener was not successfully registered (there is already a listener with this name) + * + * @b Example: + * @code {.cpp} + * // Use a function... + * Gamepad::master.A.onShortRelease("raiseLiftOneLevel", raiseLiftOneLevel); + * // ...or a lambda + * Gamepad::master.B.onShortRelease("intakeOnePicce", []() { intake.move_relative(600, 100); }); + * @endcode + */ + bool onShortRelease(std::string listenerName, std::function func) const; + /** + * @brief Register a function to run for a given event. + * + * @param event Which event to register the listener on. + * @param listenerName The name of the listener, this must be a unique name + * @param func The function to run for the given event, the function MUST NOT block + * @return true The listener was successfully registered + * @return false The listener was not successfully registered (there is already a listener with this name) + * + * @b Example: + * @code {.cpp} + * // Use a function... + * Gamepad::master.L1.addListener(Gamepad::ON_PRESS, "start_spin", startSpin); + * // ...or a lambda + * Gamepad::master.L1.addListener(Gamepad::ON_RELEASE, "stop_spin", []() { motor1.brake(); }); + * @endcode + */ + bool addListener(EventType event, std::string listenerName, std::function func) const; + /** + * @brief Removes a listener from the button + * @warning Usage of this function is discouraged. + * + * @param listenerName The name of the listener to remove + * @return true The specified listener was successfully removed + * @return false The specified listener could not be removed + * + * @b Example: + * @code {.cpp} + * // Add an event listener... + * Gamepad::master.L1.addListener(Gamepad::ON_PRESS, "do_something", doSomething); + * // ...and now get rid of it + * Gamepad::master.L1.removeListener("do_something"); + * @endcode + */ + bool removeListener(std::string listenerName) const; + + /** + * @brief Returns a value indicating whether the button is currently being held. + * + * @return true The button is currently pressed + * @return false The button is not currently pressed + */ + explicit operator bool() const { return is_pressed; } + private: + /** + * @brief Updates the button and runs any event handlers, if necessary + * + * @param is_held Whether or not the button is currently held down + */ + void update(bool is_held); + /// he last time the update function was called + uint32_t last_update_time = pros::millis(); + /// The last time the long press event was fired + uint32_t last_long_press_time = 0; + mutable _impl::EventHandler onPressEvent {}; + mutable _impl::EventHandler onLongPressEvent {}; + mutable _impl::EventHandler onReleaseEvent {}; + mutable _impl::EventHandler onShortReleaseEvent {}; +}; +} \ No newline at end of file diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index df78d52..5d0a00f 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -4,178 +4,12 @@ #include #include #include -#ifndef PROS_USE_SIMPLE_NAMES -#define PROS_USE_SIMPLE_NAMES -#endif -#include "event_handler.hpp" +#include "button.hpp" #include "pros/misc.hpp" #include "pros/rtos.hpp" namespace Gamepad { - -enum EventType { - ON_PRESS, - ON_LONG_PRESS, - ON_RELEASE, - ON_SHORT_RELEASE, -}; - -class Button { - friend class Controller; - public: - /// Whether the button has just been pressed - bool rising_edge = false; - /// Whether the button has just been released - bool falling_edge = false; - /// Whether the button is currently held down - bool is_pressed = false; - /// How long the button has been held down - uint32_t time_held = 0; - /// How long the button has been released - uint32_t time_released = 0; - /// How long the threshold should be for the longPress and shortRelease events - uint32_t long_press_threshold = 500; - /** - * @brief Register a function to run when the button is pressed. - * - * @param listenerName The name of the listener, this must be a unique name - * @param func The function to run when the button is pressed, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) - * - * @b Example: - * @code {.cpp} - * // Use a function... - * Gamepad::master.Down.onPress("downPress1", downPress1); - * // ...or a lambda - * Gamepad::master.Up.onPress("upPress1", []() { std::cout << "I was pressed!" << std::endl; }); - * @endcode - */ - bool onPress(std::string listenerName, std::function func) const; - /** - * @brief Register a function to run when the button is long pressed. - * - * By default, onLongPress will fire when the button has been held down for - * 500ms or more, this threshold can be adjusted by changing long_press_threshold. - * - * @warning When using this event along with onPress, both the onPress - * and onlongPress listeners may fire together. - * - * @param listenerName The name of the listener, this must be a unique name - * @param func The function to run when the button is long pressed, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) - * - * @b Example: - * @code {.cpp} - * // Use a function... - * Gamepad::master.Left.onLongPress("fireCatapult", fireCatapult); - * // ...or a lambda - * Gamepad::master.Right.onLongPress("print_right", []() { std::cout << "Right button was long pressed!" << - * std::endl; }); - * @endcode - */ - bool onLongPress(std::string listenerName, std::function func) const; - /** - * @brief Register a function to run when the button is released. - * - * @param listenerName The name of the listener, this must be a unique name - * @param func The function to run when the button is released, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) - * - * @b Example: - * @code {.cpp} - * // Use a function... - * Gamepad::master.X.onRelease("stopFlywheel", stopFlywheel); - * // ...or a lambda - * Gamepad::master.Y.onRelease("stopIntake", []() { intake.move(0); }); - * @endcode - */ - bool onRelease(std::string listenerName, std::function func) const; - /** - * @brief Register a function to run when the button is short released. - * - * By default, shortRelease will fire when the button has been released before 500ms, this threshold can be - * adjusted by changing long_press_threshold. - * - * @note This event will most likely be used along with the longPress event. - * - * @param listenerName The name of the listener, this must be a unique name - * @param func The function to run when the button is short released, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) - * - * @b Example: - * @code {.cpp} - * // Use a function... - * Gamepad::master.A.onShortRelease("raiseLiftOneLevel", raiseLiftOneLevel); - * // ...or a lambda - * Gamepad::master.B.onShortRelease("intakeOnePicce", []() { intake.move_relative(600, 100); }); - * @endcode - */ - bool onShortRelease(std::string listenerName, std::function func) const; - /** - * @brief Register a function to run for a given event. - * - * @param event Which event to register the listener on. - * @param listenerName The name of the listener, this must be a unique name - * @param func The function to run for the given event, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) - * - * @b Example: - * @code {.cpp} - * // Use a function... - * Gamepad::master.L1.addListener(Gamepad::ON_PRESS, "start_spin", startSpin); - * // ...or a lambda - * Gamepad::master.L1.addListener(Gamepad::ON_RELEASE, "stop_spin", []() { motor1.brake(); }); - * @endcode - */ - bool addListener(EventType event, std::string listenerName, std::function func) const; - /** - * @brief Removes a listener from the button - * @warning Usage of this function is discouraged. - * - * @param listenerName The name of the listener to remove - * @return true The specified listener was successfully removed - * @return false The specified listener could not be removed - * - * @b Example: - * @code {.cpp} - * // Add an event listener... - * Gamepad::master.L1.addListener(Gamepad::ON_PRESS, "do_something", doSomething); - * // ...and now get rid of it - * Gamepad::master.L1.removeListener("do_something"); - * @endcode - */ - bool removeListener(std::string listenerName) const; - - /** - * @brief Returns a value indicating whether the button is currently being held. - * - * @return true The button is currently pressed - * @return false The button is not currently pressed - */ - explicit operator bool() const { return is_pressed; } - private: - /** - * @brief Updates the button and runs any event handlers, if necessary - * - * @param is_held Whether or not the button is currently held down - */ - void update(bool is_held); - /// he last time the update function was called - uint32_t last_update_time = pros::millis(); - /// The last time the long press event was fired - uint32_t last_long_press_time = 0; - mutable _impl::EventHandler onPressEvent {}; - mutable _impl::EventHandler onLongPressEvent {}; - mutable _impl::EventHandler onReleaseEvent {}; - mutable _impl::EventHandler onShortReleaseEvent {}; -}; - class Controller { public: /** diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp new file mode 100644 index 0000000..459f40a --- /dev/null +++ b/src/gamepad/button.cpp @@ -0,0 +1,64 @@ +#include "gamepad/button.hpp" +#include "gamepad/todo.hpp" +#include "pros/misc.h" + +namespace Gamepad { +bool Button::onPress(std::string listenerName, std::function func) const { + return this->onPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); +} + +bool Button::onLongPress(std::string listenerName, std::function func) const { + return this->onLongPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); +} + +bool Button::onRelease(std::string listenerName, std::function func) const { + return this->onReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); +} + +bool Button::onShortRelease(std::string listenerName, std::function func) const { + return this->onShortReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); +} + +bool Button::addListener(EventType event, std::string listenerName, std::function func) const { + switch (event) { + case Gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func)); + case Gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func)); + case Gamepad::EventType::ON_RELEASE: return this->onRelease(std::move(listenerName), std::move(func)); + case Gamepad::EventType::ON_SHORT_RELEASE: + return this->onShortRelease(std::move(listenerName), std::move(func)); + default: + TODO("add error logging") + errno = EINVAL; + return false; + } +} + +bool Button::removeListener(std::string listenerName) const { + return this->onPressEvent.remove_listener(listenerName + "_user") || + this->onLongPressEvent.remove_listener(listenerName + "_user") || + this->onReleaseEvent.remove_listener(listenerName + "_user") || + this->onShortReleaseEvent.remove_listener(listenerName + "_user"); +} + +void Button::update(const bool is_held) { + 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() - this->last_update_time; + else this->time_released += pros::millis() - this->last_update_time; + + if (this->rising_edge) { + this->onPressEvent.fire(); + } else if (this->is_pressed && this->time_held >= this->long_press_threshold && + this->last_long_press_time <= pros::millis() - this->time_held) { + this->onLongPressEvent.fire(); + this->last_long_press_time = pros::millis(); + } else if (this->falling_edge) { + this->onReleaseEvent.fire(); + if (this->time_held < this->long_press_threshold) this->onShortReleaseEvent.fire(); + } + if (this->rising_edge) this->time_held = 0; + if (this->falling_edge) this->time_released = 0; + this->last_update_time = pros::millis(); +} +} \ No newline at end of file diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 720c1f4..a651bab 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -1,68 +1,10 @@ +#include "gamepad/button.hpp" #include "gamepad/controller.hpp" #include "gamepad/todo.hpp" #include "pros/misc.h" #include namespace Gamepad { -bool Button::onPress(std::string listenerName, std::function func) const { - return this->onPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); -} - -bool Button::onLongPress(std::string listenerName, std::function func) const { - return this->onLongPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); -} - -bool Button::onRelease(std::string listenerName, std::function func) const { - return this->onReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); -} - -bool Button::onShortRelease(std::string listenerName, std::function func) const { - return this->onShortReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); -} - -bool Button::addListener(EventType event, std::string listenerName, std::function func) const { - switch (event) { - case Gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func)); - case Gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func)); - case Gamepad::EventType::ON_RELEASE: return this->onRelease(std::move(listenerName), std::move(func)); - case Gamepad::EventType::ON_SHORT_RELEASE: - return this->onShortRelease(std::move(listenerName), std::move(func)); - default: - TODO("add error logging") - errno = EINVAL; - return false; - } -} - -bool Button::removeListener(std::string listenerName) const { - return this->onPressEvent.remove_listener(listenerName + "_user") || - this->onLongPressEvent.remove_listener(listenerName + "_user") || - this->onReleaseEvent.remove_listener(listenerName + "_user") || - this->onShortReleaseEvent.remove_listener(listenerName + "_user"); -} - -void Button::update(const bool is_held) { - 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() - this->last_update_time; - else this->time_released += pros::millis() - this->last_update_time; - - if (this->rising_edge) { - this->onPressEvent.fire(); - } else if (this->is_pressed && this->time_held >= this->long_press_threshold && - this->last_long_press_time <= pros::millis() - this->time_held) { - this->onLongPressEvent.fire(); - this->last_long_press_time = pros::millis(); - } else if (this->falling_edge) { - this->onReleaseEvent.fire(); - if (this->time_held < this->long_press_threshold) this->onShortReleaseEvent.fire(); - } - if (this->rising_edge) this->time_held = 0; - if (this->falling_edge) this->time_released = 0; - 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); bool is_held = this->controller.get_digital(button_id); From b4db4210c6d3d048348d3f2965f84f8d4949b834 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:48:35 -0700 Subject: [PATCH 2/5] fix: :adhesive_bandage: add missing pragma once --- include/gamepad/recursive_mutex.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/gamepad/recursive_mutex.hpp b/include/gamepad/recursive_mutex.hpp index d892f2c..b272175 100644 --- a/include/gamepad/recursive_mutex.hpp +++ b/include/gamepad/recursive_mutex.hpp @@ -1,3 +1,5 @@ +#pragma once + #include "pros/apix.h" #include "pros/rtos.h" From c94c460c898bed57325d81a221443aa28a7eb1b9 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:58:22 -0700 Subject: [PATCH 3/5] refactor: :truck: rename Gamepad namespace to gamepad & Controller class to Gamepad --- include/gamepad/button.hpp | 28 +++++++++--------- include/gamepad/controller.hpp | 34 ++++++++++----------- include/gamepad/event_handler.hpp | 6 ++-- include/gamepad/recursive_mutex.hpp | 4 +-- src/gamepad/button.cpp | 10 +++---- src/gamepad/controller.cpp | 46 ++++++++++++++--------------- src/main.cpp | 16 +++++----- 7 files changed, 72 insertions(+), 72 deletions(-) diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp index 1117174..d7cb114 100644 --- a/include/gamepad/button.hpp +++ b/include/gamepad/button.hpp @@ -4,7 +4,7 @@ #include "event_handler.hpp" -namespace Gamepad { +namespace gamepad { enum EventType { ON_PRESS, ON_LONG_PRESS, @@ -13,7 +13,7 @@ enum EventType { }; class Button { - friend class Controller; + friend class Gamepad; public: /// Whether the button has just been pressed bool rising_edge = false; @@ -38,9 +38,9 @@ class Button { * @b Example: * @code {.cpp} * // Use a function... - * Gamepad::master.Down.onPress("downPress1", downPress1); + * gamepad::master.Down.onPress("downPress1", downPress1); * // ...or a lambda - * Gamepad::master.Up.onPress("upPress1", []() { std::cout << "I was pressed!" << std::endl; }); + * gamepad::master.Up.onPress("upPress1", []() { std::cout << "I was pressed!" << std::endl; }); * @endcode */ bool onPress(std::string listenerName, std::function func) const; @@ -61,9 +61,9 @@ class Button { * @b Example: * @code {.cpp} * // Use a function... - * Gamepad::master.Left.onLongPress("fireCatapult", fireCatapult); + * gamepad::master.Left.onLongPress("fireCatapult", fireCatapult); * // ...or a lambda - * Gamepad::master.Right.onLongPress("print_right", []() { std::cout << "Right button was long pressed!" << + * gamepad::master.Right.onLongPress("print_right", []() { std::cout << "Right button was long pressed!" << * std::endl; }); * @endcode */ @@ -79,9 +79,9 @@ class Button { * @b Example: * @code {.cpp} * // Use a function... - * Gamepad::master.X.onRelease("stopFlywheel", stopFlywheel); + * gamepad::master.X.onRelease("stopFlywheel", stopFlywheel); * // ...or a lambda - * Gamepad::master.Y.onRelease("stopIntake", []() { intake.move(0); }); + * gamepad::master.Y.onRelease("stopIntake", []() { intake.move(0); }); * @endcode */ bool onRelease(std::string listenerName, std::function func) const; @@ -101,9 +101,9 @@ class Button { * @b Example: * @code {.cpp} * // Use a function... - * Gamepad::master.A.onShortRelease("raiseLiftOneLevel", raiseLiftOneLevel); + * gamepad::master.A.onShortRelease("raiseLiftOneLevel", raiseLiftOneLevel); * // ...or a lambda - * Gamepad::master.B.onShortRelease("intakeOnePicce", []() { intake.move_relative(600, 100); }); + * gamepad::master.B.onShortRelease("intakeOnePicce", []() { intake.move_relative(600, 100); }); * @endcode */ bool onShortRelease(std::string listenerName, std::function func) const; @@ -119,9 +119,9 @@ class Button { * @b Example: * @code {.cpp} * // Use a function... - * Gamepad::master.L1.addListener(Gamepad::ON_PRESS, "start_spin", startSpin); + * gamepad::master.L1.addListener(gamepad::ON_PRESS, "start_spin", startSpin); * // ...or a lambda - * Gamepad::master.L1.addListener(Gamepad::ON_RELEASE, "stop_spin", []() { motor1.brake(); }); + * gamepad::master.L1.addListener(gamepad::ON_RELEASE, "stop_spin", []() { motor1.brake(); }); * @endcode */ bool addListener(EventType event, std::string listenerName, std::function func) const; @@ -136,9 +136,9 @@ class Button { * @b Example: * @code {.cpp} * // Add an event listener... - * Gamepad::master.L1.addListener(Gamepad::ON_PRESS, "do_something", doSomething); + * gamepad::master.L1.addListener(gamepad::ON_PRESS, "do_something", doSomething); * // ...and now get rid of it - * Gamepad::master.L1.removeListener("do_something"); + * gamepad::master.L1.removeListener("do_something"); * @endcode */ bool removeListener(std::string listenerName) const; diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 5d0a00f..0a0e1ea 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -9,8 +9,8 @@ #include "pros/misc.hpp" #include "pros/rtos.hpp" -namespace Gamepad { -class Controller { +namespace gamepad { +class Gamepad { public: /** * @brief Updates the state of the gamepad (all joysticks and buttons), and also runs @@ -21,7 +21,7 @@ class Controller { * @b Example: * @code {.cpp} * while (true) { - * Gamepad::master.update(); + * gamepad::master.update(); * // do robot control stuff here... * pros::delay(25); * } @@ -36,7 +36,7 @@ class Controller { * * @b Example: * @code {.cpp} - * if(Gamepad::master[DIGITAL_L1]) { + * if(gamepad::master[DIGITAL_L1]) { * // do something here... * } * @endcode @@ -51,7 +51,7 @@ class Controller { * @b Example: * @code {.cpp} * // control a motor with a joystick - * intake.move(Gamepad::master[ANALOG_RIGHT_Y]); + * intake.move(gamepad::master[ANALOG_RIGHT_Y]); * @endcode * */ @@ -72,12 +72,12 @@ class Controller { const float& LeftY = m_LeftY; const float& RightX = m_RightX; const float& RightY = m_RightY; - /// The master controller, same as @ref Gamepad::master - static Controller master; - /// The partner controller, same as @ref Gamepad::partner - static Controller partner; + /// The master controller, same as @ref gamepad::master + static Gamepad master; + /// The partner controller, same as @ref gamepad::partner + static Gamepad partner; private: - Controller(pros::controller_id_e_t id) + Gamepad(pros::controller_id_e_t id) : controller(id) {} Button m_L1 {}, m_L2 {}, m_R1 {}, m_R2 {}, m_Up {}, m_Down {}, m_Left {}, m_Right {}, m_X {}, m_B {}, m_Y {}, @@ -93,16 +93,16 @@ class Controller { * @return std::string A unique listener name */ static std::string unique_name(); - static Button Controller::*button_to_ptr(pros::controller_digital_e_t button); + static Button Gamepad::*button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); - pros::Controller controller; + pros::Gamepad controller; }; -inline Controller Controller::master {pros::E_CONTROLLER_MASTER}; -inline Controller Controller::partner {pros::E_CONTROLLER_PARTNER}; +inline Gamepad Gamepad::master {pros::E_CONTROLLER_MASTER}; +inline Gamepad Gamepad::partner {pros::E_CONTROLLER_PARTNER}; /// The master controller -inline Controller& master = Controller::master; +inline Gamepad& master = Gamepad::master; /// The partner controller -inline Controller& partner = Controller::partner; +inline Gamepad& partner = Gamepad::partner; -} // namespace Gamepad +} // namespace gamepad diff --git a/include/gamepad/event_handler.hpp b/include/gamepad/event_handler.hpp index 76ab6cc..bf51b20 100644 --- a/include/gamepad/event_handler.hpp +++ b/include/gamepad/event_handler.hpp @@ -7,7 +7,7 @@ #include "gamepad/recursive_mutex.hpp" -namespace Gamepad::_impl { +namespace gamepad::_impl { /** * @brief Event handling class with thread safety that supports adding, removing, and running listeners @@ -76,6 +76,6 @@ template class EventHandler { private: std::vector keys {}; std::vector listeners {}; - Gamepad::_impl::RecursiveMutex mutex {}; + gamepad::_impl::RecursiveMutex mutex {}; }; -} // namespace Gamepad::_impl +} // namespace gamepad::_impl diff --git a/include/gamepad/recursive_mutex.hpp b/include/gamepad/recursive_mutex.hpp index b272175..cd84052 100644 --- a/include/gamepad/recursive_mutex.hpp +++ b/include/gamepad/recursive_mutex.hpp @@ -3,7 +3,7 @@ #include "pros/apix.h" #include "pros/rtos.h" -namespace Gamepad::_impl { +namespace gamepad::_impl { class RecursiveMutex { public: @@ -61,4 +61,4 @@ class RecursiveMutex { pros::mutex_t mutex; }; -} // namespace Gamepad::_impl \ No newline at end of file +} // namespace gamepad::_impl \ No newline at end of file diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp index 459f40a..5709924 100644 --- a/src/gamepad/button.cpp +++ b/src/gamepad/button.cpp @@ -2,7 +2,7 @@ #include "gamepad/todo.hpp" #include "pros/misc.h" -namespace Gamepad { +namespace gamepad { bool Button::onPress(std::string listenerName, std::function func) const { return this->onPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func)); } @@ -21,10 +21,10 @@ bool Button::onShortRelease(std::string listenerName, std::function bool Button::addListener(EventType event, std::string listenerName, std::function func) const { switch (event) { - case Gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func)); - case Gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func)); - case Gamepad::EventType::ON_RELEASE: return this->onRelease(std::move(listenerName), std::move(func)); - case Gamepad::EventType::ON_SHORT_RELEASE: + case gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func)); + case gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func)); + case gamepad::EventType::ON_RELEASE: return this->onRelease(std::move(listenerName), std::move(func)); + case gamepad::EventType::ON_SHORT_RELEASE: return this->onShortRelease(std::move(listenerName), std::move(func)); default: TODO("add error logging") diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index a651bab..32f540f 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -4,14 +4,14 @@ #include "pros/misc.h" #include -namespace Gamepad { -void Controller::updateButton(pros::controller_digital_e_t button_id) { - Button Controller::*button = Controller::button_to_ptr(button_id); +namespace gamepad { +void Gamepad::updateButton(pros::controller_digital_e_t button_id) { + Button Gamepad::*button = Gamepad::button_to_ptr(button_id); bool is_held = this->controller.get_digital(button_id); (this->*button).update(is_held); } -void Controller::update() { +void Gamepad::update() { for (int i = pros::E_CONTROLLER_DIGITAL_L1; i <= pros::E_CONTROLLER_DIGITAL_A; ++i) { this->updateButton(static_cast(i)); } @@ -22,11 +22,11 @@ void Controller::update() { this->m_RightY = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_Y); } -const Button& Controller::operator[](pros::controller_digital_e_t button) { - return this->*Controller::button_to_ptr(button); +const Button& Gamepad::operator[](pros::controller_digital_e_t button) { + return this->*Gamepad::button_to_ptr(button); } -float Controller::operator[](pros::controller_analog_e_t axis) { +float Gamepad::operator[](pros::controller_analog_e_t axis) { switch (axis) { case pros::E_CONTROLLER_ANALOG_LEFT_X: return this->LeftX; case pros::E_CONTROLLER_ANALOG_LEFT_Y: return this->LeftY; @@ -39,29 +39,29 @@ float Controller::operator[](pros::controller_analog_e_t axis) { } } -std::string Controller::unique_name() { +std::string Gamepad::unique_name() { static std::atomic i = 0; return std::to_string(i++) + "_internal"; } -Button Controller::*Controller::button_to_ptr(pros::controller_digital_e_t button) { +Button Gamepad::*Gamepad::button_to_ptr(pros::controller_digital_e_t button) { switch (button) { - case pros::E_CONTROLLER_DIGITAL_L1: return &Controller::m_L1; - case pros::E_CONTROLLER_DIGITAL_L2: return &Controller::m_L2; - case pros::E_CONTROLLER_DIGITAL_R1: return &Controller::m_R1; - case pros::E_CONTROLLER_DIGITAL_R2: return &Controller::m_R2; - case pros::E_CONTROLLER_DIGITAL_UP: return &Controller::m_Up; - case pros::E_CONTROLLER_DIGITAL_DOWN: return &Controller::m_Down; - case pros::E_CONTROLLER_DIGITAL_LEFT: return &Controller::m_Left; - case pros::E_CONTROLLER_DIGITAL_RIGHT: return &Controller::m_Right; - case pros::E_CONTROLLER_DIGITAL_X: return &Controller::m_X; - case pros::E_CONTROLLER_DIGITAL_B: return &Controller::m_B; - case pros::E_CONTROLLER_DIGITAL_Y: return &Controller::m_Y; - case pros::E_CONTROLLER_DIGITAL_A: return &Controller::m_A; + case pros::E_CONTROLLER_DIGITAL_L1: return &Gamepad::m_L1; + case pros::E_CONTROLLER_DIGITAL_L2: return &Gamepad::m_L2; + case pros::E_CONTROLLER_DIGITAL_R1: return &Gamepad::m_R1; + case pros::E_CONTROLLER_DIGITAL_R2: return &Gamepad::m_R2; + case pros::E_CONTROLLER_DIGITAL_UP: return &Gamepad::m_Up; + case pros::E_CONTROLLER_DIGITAL_DOWN: return &Gamepad::m_Down; + case pros::E_CONTROLLER_DIGITAL_LEFT: return &Gamepad::m_Left; + case pros::E_CONTROLLER_DIGITAL_RIGHT: return &Gamepad::m_Right; + case pros::E_CONTROLLER_DIGITAL_X: return &Gamepad::m_X; + case pros::E_CONTROLLER_DIGITAL_B: return &Gamepad::m_B; + case pros::E_CONTROLLER_DIGITAL_Y: return &Gamepad::m_Y; + case pros::E_CONTROLLER_DIGITAL_A: return &Gamepad::m_A; default: TODO("add error logging") errno = EINVAL; - return &Controller::Fake; + return &Gamepad::Fake; } } -} // namespace Gamepad +} // namespace gamepad diff --git a/src/main.cpp b/src/main.cpp index 2949002..e3da78e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,16 +18,16 @@ void leftShortRelease1() { printf("Left Short Release!\n"); } void initialize() { // We can register functions to run when buttons are pressed - Gamepad::master.Down.onPress("downPress1", downPress1); + gamepad::master.Down.onPress("downPress1", downPress1); // ...or when they're released - Gamepad::master.Up.onRelease("downRelease1", upRelease1); + gamepad::master.Up.onRelease("downRelease1", upRelease1); // There's also the longPress event - Gamepad::master.Left.onLongPress("leftLongPress1", leftLongPress1); + gamepad::master.Left.onLongPress("leftLongPress1", leftLongPress1); // We can have two functions on one button, // just remember to give them different names - Gamepad::master.Left.onShortRelease("leftShortRelease", leftShortRelease1); + gamepad::master.Left.onShortRelease("leftShortRelease", leftShortRelease1); // And we can use lambda's too - Gamepad::master.X.onShortRelease("xShortRelease1", []() { printf("X Short Release!\n"); }); + gamepad::master.X.onShortRelease("xShortRelease1", []() { printf("X Short Release!\n"); }); } /** @@ -80,10 +80,10 @@ void opcontrol() { while (true) { // Remember to ALWAYS call update at the start of your while loop! - Gamepad::master.update(); + gamepad::master.update(); // We'll use the arcade control scheme - int dir = Gamepad::master.LeftY; // Gets amount forward/backward from left joystick - int turn = Gamepad::master.RightX; // Gets the turn left/right from right joystick + int dir = gamepad::master.LeftY; // Gets amount forward/backward from left joystick + int turn = gamepad::master.RightX; // Gets the turn left/right from right joystick left_mg.move(dir - turn); // Sets left motor voltage right_mg.move(dir + turn); // Sets right motor voltage pros::delay(25); // Wait for 25 ms, then update the motor values again From d170d12e1bee21a2130ac8ecf7309c2405736511 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:02:50 -0700 Subject: [PATCH 4/5] fix: :bug: fix find-and-replace error --- include/gamepad/controller.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 0a0e1ea..320a1e4 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -95,7 +95,7 @@ class Gamepad { static std::string unique_name(); static Button Gamepad::*button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); - pros::Gamepad controller; + pros::Controller controller; }; inline Gamepad Gamepad::master {pros::E_CONTROLLER_MASTER}; From 30dab85b2040c69084cf84c0800cb34c3715211f Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 25 Sep 2024 20:07:35 +0000 Subject: [PATCH 5/5] style: :art: fix formatting issues --- include/gamepad/button.hpp | 2 +- include/gamepad/controller.hpp | 3 --- src/gamepad/button.cpp | 4 ++-- src/gamepad/controller.cpp | 4 +--- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp index d7cb114..f1126d3 100644 --- a/include/gamepad/button.hpp +++ b/include/gamepad/button.hpp @@ -166,4 +166,4 @@ class Button { mutable _impl::EventHandler onReleaseEvent {}; mutable _impl::EventHandler onShortReleaseEvent {}; }; -} \ No newline at end of file +} // namespace gamepad \ No newline at end of file diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 320a1e4..cd1aebc 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,13 +1,10 @@ #pragma once #include "pros/misc.h" -#include -#include #include #include "button.hpp" #include "pros/misc.hpp" -#include "pros/rtos.hpp" namespace gamepad { class Gamepad { diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp index 5709924..74dfc66 100644 --- a/src/gamepad/button.cpp +++ b/src/gamepad/button.cpp @@ -1,6 +1,6 @@ #include "gamepad/button.hpp" #include "gamepad/todo.hpp" -#include "pros/misc.h" +#include "pros/rtos.hpp" namespace gamepad { bool Button::onPress(std::string listenerName, std::function func) const { @@ -61,4 +61,4 @@ void Button::update(const bool is_held) { if (this->falling_edge) this->time_released = 0; this->last_update_time = pros::millis(); } -} \ No newline at end of file +} // namespace gamepad \ No newline at end of file diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 32f540f..fff75f2 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -22,9 +22,7 @@ void Gamepad::update() { this->m_RightY = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_Y); } -const Button& Gamepad::operator[](pros::controller_digital_e_t button) { - return this->*Gamepad::button_to_ptr(button); -} +const Button& Gamepad::operator[](pros::controller_digital_e_t button) { return this->*Gamepad::button_to_ptr(button); } float Gamepad::operator[](pros::controller_analog_e_t axis) { switch (axis) {