From 30e329fbf7016a6a457345aaae6030092d8bde33 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Thu, 23 May 2024 19:38:44 +0000 Subject: [PATCH] Clear stable branch --- include/gamepad/api.hpp | 4 -- include/gamepad/controller.hpp | 77 ------------------------- include/gamepad/event_handler.hpp | 55 ------------------ include/gamepad/todo.hpp | 5 -- src/gamepad/controller.cpp | 95 ------------------------------- 5 files changed, 236 deletions(-) delete mode 100644 include/gamepad/api.hpp delete mode 100644 include/gamepad/controller.hpp delete mode 100644 include/gamepad/event_handler.hpp delete mode 100644 include/gamepad/todo.hpp delete mode 100644 src/gamepad/controller.cpp diff --git a/include/gamepad/api.hpp b/include/gamepad/api.hpp deleted file mode 100644 index 835385a..0000000 --- a/include/gamepad/api.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include "gamepad/event_handler.hpp" // IWYU pragma: export -#include "gamepad/controller.hpp" // IWYU pragma: export \ No newline at end of file diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp deleted file mode 100644 index 2c520e0..0000000 --- a/include/gamepad/controller.hpp +++ /dev/null @@ -1,77 +0,0 @@ -#pragma once - -#include -#include -#ifndef PROS_USE_SIMPLE_NAMES -#define PROS_USE_SIMPLE_NAMES -#endif - -#include "event_handler.hpp" -#include "pros/misc.hpp" -#include "pros/rtos.hpp" - -namespace Gamepad { - -enum EventType { - ON_PRESS, - ON_LONG_PRESS, - ON_RELEASE, -}; - -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 func); - uint32_t onLongPress(std::function func); - uint32_t onRelease(std::function func); - uint32_t addListener(EventType event, std::function func); - bool removeListener(uint32_t id); - private: - - void update(bool is_held); - - EventHandler<> onPressEvent; - EventHandler<> onLongPressEvent; - EventHandler<> onReleaseEvent; -}; - -class Controller { - public: - 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. - * @note This function should be called at the beginning of every loop iteration. - * @note Create a separate instance for each task. - */ - void update(); - /** - * Get the state of a button on the controller. - * @param button Which button's state you want. - */ - const Button& operator[](pros::controller_digital_e_t button); - /** - * Get the value of a joystick axis on the 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{}; - float LeftX = 0, LeftY = 0, RightX = 0, RightY = 0; - private: - 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 diff --git a/include/gamepad/event_handler.hpp b/include/gamepad/event_handler.hpp deleted file mode 100644 index 6bdc188..0000000 --- a/include/gamepad/event_handler.hpp +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -#include "gamepad/todo.hpp" -#include "pros/rtos.hpp" - -namespace Gamepad { - -class MonotonicCounter { - template friend class EventHandler; - static uint32_t next_value() { - static std::atomic counter = 0; - return ++counter; - } -}; - -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; - } - 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...); - } - } - private: - std::map listeners; - pros::Mutex mutex; -}; -} \ No newline at end of file diff --git a/include/gamepad/todo.hpp b/include/gamepad/todo.hpp deleted file mode 100644 index 6d61cf6..0000000 --- a/include/gamepad/todo.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#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 diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp deleted file mode 100644 index 4472527..0000000 --- a/src/gamepad/controller.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "gamepad/controller.hpp" -#include "gamepad/todo.hpp" - -namespace Gamepad { - -uint32_t Button::onPress(std::function func) { - return this->onPressEvent.add_listener(std::move(func)); -} - -uint32_t Button::onLongPress(std::function func) { - return this->onLongPressEvent.add_listener(std::move(func)); -} - -uint32_t Button::onRelease(std::function 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).update(is_held); -} - -void Controller::update() { - 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); - this->RightX = this->controller.get_analog(ANALOG_RIGHT_X); - this->RightY = this->controller.get_analog(ANALOG_RIGHT_Y); -} - -const Button& Controller::operator[](pros::controller_digital_e_t button) { - return this->*Controller::button_to_ptr(button); -} - -float Controller::operator[](pros::controller_analog_e_t axis) { - switch (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") - default: std::exit(1); - } -} - -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; - case DIGITAL_R1: return &Controller::R1; - case DIGITAL_R2: return &Controller::R2; - case DIGITAL_UP: return &Controller::Up; - case DIGITAL_DOWN: return &Controller::Down; - case DIGITAL_LEFT: return &Controller::Left; - case DIGITAL_RIGHT: return &Controller::Right; - 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") - default: std::exit(1); - } -} -} \ No newline at end of file