From af8e66d18a2aab533533c1f899e49e4f7d94e1f2 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Tue, 28 May 2024 22:05:39 +0000 Subject: [PATCH 01/10] implement nifty counter for Controller --- include/gamepad/controller.hpp | 13 +++++++++++-- src/gamepad/controller.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 2c520e0..4758b38 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -45,8 +45,8 @@ class Button { }; class Controller { + friend struct ControllerInit; 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. @@ -70,8 +70,17 @@ class Controller { X{}, B{}, Y{}, A{}; float LeftX = 0, LeftY = 0, RightX = 0, RightY = 0; private: + explicit Controller(pros::controller_id_e_t id): controller(id) {} 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 +}; + +static struct ControllerInit { + ControllerInit(); + ~ControllerInit(); +} _controllerInit; + +extern Controller& master; +extern Controller& partner; } \ No newline at end of file diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 4472527..4d96a9d 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -1,8 +1,35 @@ #include "gamepad/controller.hpp" #include "gamepad/todo.hpp" +#include "pros/misc.h" + +#include +#include +#include namespace Gamepad { +static int nifty_counter; // zero initialized at load time +static std::array master_buf alignas(Controller); +Controller& master = *reinterpret_cast (&*master_buf.begin()); +static std::array partner_buf alignas(Controller); +Controller& partner = *reinterpret_cast (&*partner_buf.begin()); + +ControllerInit::ControllerInit() { + if(nifty_counter == 0) { + new (&*master_buf.begin()) Controller(pros::E_CONTROLLER_MASTER); + new (&*partner_buf.begin()) Controller(pros::E_CONTROLLER_PARTNER); + } + ++nifty_counter; +} + +ControllerInit::~ControllerInit() { + --nifty_counter; + if(nifty_counter == 0) { + master.~Controller(); + partner.~Controller(); + } +} + uint32_t Button::onPress(std::function func) { return this->onPressEvent.add_listener(std::move(func)); } From 3dc4a6712ce4d0f64b997f2bf3bca3a185f4ea07 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Tue, 28 May 2024 22:05:52 +0000 Subject: [PATCH 02/10] use Gamepad in main.cpp --- src/main.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1cc258a..381e9b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "main.h" +#include "gamepad/api.hpp" /** * A callback function for LLEMU's center button. @@ -74,19 +75,15 @@ void autonomous() {} * task, not resume it from where it left off. */ void opcontrol() { - pros::Controller master(pros::E_CONTROLLER_MASTER); + pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 4 and reversed ports 4 & 6 while (true) { - pros::lcd::print(0, "%d %d %d", (pros::lcd::read_buttons() & LCD_BTN_LEFT) >> 2, - (pros::lcd::read_buttons() & LCD_BTN_CENTER) >> 1, - - (pros::lcd::read_buttons() & LCD_BTN_RIGHT) >> 0); // Prints status of the emulated screen LCDs - + Gamepad::master.update(); // Arcade control scheme - int dir = master.get_analog(ANALOG_LEFT_Y); // Gets amount forward/backward from left joystick - int turn = master.get_analog(ANALOG_RIGHT_X); // 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(20); // Run for 20 ms then update From 4ce005773d68544626e8c210b9765b3604aa015e Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 29 May 2024 16:57:35 +0000 Subject: [PATCH 03/10] use union instead of array for Controller buffer --- src/gamepad/controller.cpp | 59 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 4d96a9d..d151483 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -3,36 +3,43 @@ #include "pros/misc.h" #include -#include -#include namespace Gamepad { -static int nifty_counter; // zero initialized at load time -static std::array master_buf alignas(Controller); -Controller& master = *reinterpret_cast (&*master_buf.begin()); -static std::array partner_buf alignas(Controller); -Controller& partner = *reinterpret_cast (&*partner_buf.begin()); +union ControllerBuf { + // this is here to allow us to control when we initialize the Controller + char c; + Controller as_controller; + + // empty destructor, the Controller must be manually destroyed by ControllerInit + ~ControllerBuf() {} +}; + +static uint32_t nifty_counter; // zero initialized at load time +ControllerBuf master_buf {}; +Controller& master = master_buf.as_controller; +ControllerBuf partner_buf {}; +Controller& partner = partner_buf.as_controller; ControllerInit::ControllerInit() { - if(nifty_counter == 0) { - new (&*master_buf.begin()) Controller(pros::E_CONTROLLER_MASTER); - new (&*partner_buf.begin()) Controller(pros::E_CONTROLLER_PARTNER); + // only initialize once, if we're the first ControllerInit instance + if (nifty_counter == 0) { + new (&master_buf.as_controller) Controller(pros::E_CONTROLLER_MASTER); + new (&partner_buf.as_controller) Controller(pros::E_CONTROLLER_PARTNER); } ++nifty_counter; } ControllerInit::~ControllerInit() { --nifty_counter; - if(nifty_counter == 0) { + // only destroy if we're the last ControllerInit instance + if (nifty_counter == 0) { master.~Controller(); partner.~Controller(); } } -uint32_t Button::onPress(std::function func) { - return this->onPressEvent.add_listener(std::move(func)); -} +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)); @@ -53,12 +60,8 @@ void Button::update(const bool is_held) { } 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) { this->time_held = 0; } + if (this->falling_edge) { this->time_released = 0; } if (this->rising_edge) { onPressEvent.fire(); @@ -70,15 +73,13 @@ void Button::update(const bool is_held) { } 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); @@ -95,13 +96,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; @@ -114,9 +114,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 From 3ded29687561ec4ba78f946ff9c437e615ec83ea Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 29 May 2024 17:06:22 +0000 Subject: [PATCH 04/10] hide controllerInit in _impl namespace --- include/gamepad/controller.hpp | 62 ++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 4758b38..f00baf2 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -19,33 +19,32 @@ 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); - 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); + 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); - void update(bool is_held); - - EventHandler<> onPressEvent; - EventHandler<> onLongPressEvent; - EventHandler<> onReleaseEvent; + EventHandler<> onPressEvent; + EventHandler<> onLongPressEvent; + EventHandler<> onReleaseEvent; }; class Controller { - friend struct ControllerInit; + friend struct ControllerInit; public: /** * Updates the state of the gamepad (all joysticks and buttons), and also runs @@ -65,22 +64,25 @@ 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: - explicit Controller(pros::controller_id_e_t id): controller(id) {} - static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); + explicit Controller(pros::controller_id_e_t id) + : controller(id) {} + + static Button Controller::*button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); pros::Controller controller; }; +extern Controller& master; +extern Controller& partner; + +namespace _impl { static struct ControllerInit { - ControllerInit(); - ~ControllerInit(); + ControllerInit(); + ~ControllerInit(); } _controllerInit; +} // namespace _impl -extern Controller& master; -extern Controller& partner; -} \ No newline at end of file +} // namespace Gamepad \ No newline at end of file From 6321e53cf5b59671463f7c98040714ef11bf138e Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 29 May 2024 17:09:05 +0000 Subject: [PATCH 05/10] fix compile error, move ControllerInit ctor and dtor to _impl namespace --- src/gamepad/controller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index d151483..5754b54 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -21,7 +21,7 @@ Controller& master = master_buf.as_controller; ControllerBuf partner_buf {}; Controller& partner = partner_buf.as_controller; -ControllerInit::ControllerInit() { +_impl::ControllerInit::ControllerInit() { // only initialize once, if we're the first ControllerInit instance if (nifty_counter == 0) { new (&master_buf.as_controller) Controller(pros::E_CONTROLLER_MASTER); @@ -30,7 +30,7 @@ ControllerInit::ControllerInit() { ++nifty_counter; } -ControllerInit::~ControllerInit() { +_impl::ControllerInit::~ControllerInit() { --nifty_counter; // only destroy if we're the last ControllerInit instance if (nifty_counter == 0) { From fd24a3f09880d07afdd36d748dc2f95183dcb56d Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 29 May 2024 17:12:00 +0000 Subject: [PATCH 06/10] fixed more compile issues, fixed all declarations to be in correct namespace --- include/gamepad/controller.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index f00baf2..acc7a9e 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -43,8 +43,15 @@ class Button { EventHandler<> onReleaseEvent; }; +namespace _impl { +static struct ControllerInit { + ControllerInit(); + ~ControllerInit(); +} _controllerInit; +} // namespace _impl + class Controller { - friend struct ControllerInit; + friend struct _impl::ControllerInit; public: /** * Updates the state of the gamepad (all joysticks and buttons), and also runs @@ -78,11 +85,4 @@ class Controller { extern Controller& master; extern Controller& partner; -namespace _impl { -static struct ControllerInit { - ControllerInit(); - ~ControllerInit(); -} _controllerInit; -} // namespace _impl - } // namespace Gamepad \ No newline at end of file From 8f260677f43f9e76bb2585381e81c468e8540585 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Tue, 13 Aug 2024 17:30:14 +0000 Subject: [PATCH 07/10] style: :art: Fix formatting --- include/gamepad/event_handler.hpp | 70 ++++++++++++++++--------------- include/gamepad/todo.hpp | 6 +-- 2 files changed, 39 insertions(+), 37 deletions(-) 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 From 1b5c5f89991717d1710f53840bab3be1362b6682 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:14:58 +0000 Subject: [PATCH 08/10] refactor: :recycle: Use inline variable instead of nifty counter --- include/gamepad/controller.hpp | 21 ++++----- src/gamepad/controller.cpp | 78 +++++++++------------------------- 2 files changed, 32 insertions(+), 67 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index acc7a9e..12667ae 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,5 +1,6 @@ #pragma once +#include "pros/misc.h" #include #include #ifndef PROS_USE_SIMPLE_NAMES @@ -43,15 +44,7 @@ class Button { EventHandler<> onReleaseEvent; }; -namespace _impl { -static struct ControllerInit { - ControllerInit(); - ~ControllerInit(); -} _controllerInit; -} // namespace _impl - class Controller { - friend struct _impl::ControllerInit; public: /** * Updates the state of the gamepad (all joysticks and buttons), and also runs @@ -73,6 +66,10 @@ class Controller { 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; + /// The master controller, same as @ref Gamepad::master + static Controller master; + /// The partner controller, same as @ref Gamepad::partner + static Controller partner; private: explicit Controller(pros::controller_id_e_t id) : controller(id) {} @@ -82,7 +79,11 @@ class Controller { pros::Controller controller; }; -extern Controller& master; -extern Controller& partner; +inline Controller Controller::master {pros::E_CONTROLLER_MASTER}; +inline Controller Controller::partner {pros::E_CONTROLLER_PARTNER}; +/// The master controller +inline Controller& master = Controller::master; +/// The partner controller +inline Controller& partner = Controller::partner; } // namespace Gamepad \ No newline at end of file diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 5754b54..c1a6da2 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -2,43 +2,7 @@ #include "gamepad/todo.hpp" #include "pros/misc.h" -#include - namespace Gamepad { - -union ControllerBuf { - // this is here to allow us to control when we initialize the Controller - char c; - Controller as_controller; - - // empty destructor, the Controller must be manually destroyed by ControllerInit - ~ControllerBuf() {} -}; - -static uint32_t nifty_counter; // zero initialized at load time -ControllerBuf master_buf {}; -Controller& master = master_buf.as_controller; -ControllerBuf partner_buf {}; -Controller& partner = partner_buf.as_controller; - -_impl::ControllerInit::ControllerInit() { - // only initialize once, if we're the first ControllerInit instance - if (nifty_counter == 0) { - new (&master_buf.as_controller) Controller(pros::E_CONTROLLER_MASTER); - new (&partner_buf.as_controller) Controller(pros::E_CONTROLLER_PARTNER); - } - ++nifty_counter; -} - -_impl::ControllerInit::~ControllerInit() { - --nifty_counter; - // only destroy if we're the last ControllerInit instance - if (nifty_counter == 0) { - master.~Controller(); - partner.~Controller(); - } -} - uint32_t Button::onPress(std::function func) { return this->onPressEvent.add_listener(std::move(func)); } uint32_t Button::onLongPress(std::function func) { @@ -79,12 +43,12 @@ void Controller::updateButton(pros::controller_digital_e_t button_id) { } void Controller::update() { - for (int i = DIGITAL_L1; i != DIGITAL_A; ++i) { this->updateButton(static_cast(i)); } + for (int i = pros::E_CONTROLLER_DIGITAL_L1; i != pros::E_CONTROLLER_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); + this->LeftX = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_X); + this->LeftY = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y); + this->RightX = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_X); + this->RightY = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_Y); } const Button& Controller::operator[](pros::controller_digital_e_t button) { @@ -93,28 +57,28 @@ const Button& Controller::operator[](pros::controller_digital_e_t 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") + case pros::E_CONTROLLER_ANALOG_LEFT_X: return this->LeftX; + case pros::E_CONTROLLER_ANALOG_LEFT_Y: return this->LeftY; + case pros::E_CONTROLLER_ANALOG_RIGHT_X: return this->RightX; + case pros::E_CONTROLLER_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") + case pros::E_CONTROLLER_DIGITAL_L1: return &Controller::L1; + case pros::E_CONTROLLER_DIGITAL_L2: return &Controller::L2; + case pros::E_CONTROLLER_DIGITAL_R1: return &Controller::R1; + case pros::E_CONTROLLER_DIGITAL_R2: return &Controller::R2; + case pros::E_CONTROLLER_DIGITAL_UP: return &Controller::Up; + case pros::E_CONTROLLER_DIGITAL_DOWN: return &Controller::Down; + case pros::E_CONTROLLER_DIGITAL_LEFT: return &Controller::Left; + case pros::E_CONTROLLER_DIGITAL_RIGHT: return &Controller::Right; + case pros::E_CONTROLLER_DIGITAL_X: return &Controller::X; + case pros::E_CONTROLLER_DIGITAL_B: return &Controller::B; + case pros::E_CONTROLLER_DIGITAL_Y: return &Controller::Y; + case pros::E_CONTROLLER_DIGITAL_A: return &Controller::A; TODO("change handling for default") default: std::exit(1); } } From 641917bb23b146d92c1705ff979277f021094ecf Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:05:33 +0000 Subject: [PATCH 09/10] style: :art: Format controller.cpp --- src/gamepad/controller.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index c1a6da2..9a601d9 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -43,7 +43,9 @@ void Controller::updateButton(pros::controller_digital_e_t button_id) { } void Controller::update() { - for (int i = pros::E_CONTROLLER_DIGITAL_L1; i != pros::E_CONTROLLER_DIGITAL_A; ++i) { this->updateButton(static_cast(i)); } + for (int i = pros::E_CONTROLLER_DIGITAL_L1; i != pros::E_CONTROLLER_DIGITAL_A; ++i) { + this->updateButton(static_cast(i)); + } this->LeftX = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_X); this->LeftY = this->controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y); From 8a70ae0f60c318cb05ba566308f07964f89d3f6b Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 21 Aug 2024 16:53:33 +0000 Subject: [PATCH 10/10] style: :art: Fix formatting in main.cpp --- src/main.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ab94d8b..30d56e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,17 +75,16 @@ void autonomous() {} * task, not resume it from where it left off. */ void opcontrol() { - - pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 - pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 4 and reversed ports 4 & 6 + pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 + pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 4 and reversed ports 4 & 6 - while (true) { - Gamepad::master.update(); - // 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 - left_mg.move(dir - turn); // Sets left motor voltage - right_mg.move(dir + turn); // Sets right motor voltage - pros::delay(20); // Run for 20 ms then update - } + while (true) { + Gamepad::master.update(); + // 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 + left_mg.move(dir - turn); // Sets left motor voltage + right_mg.move(dir + turn); // Sets right motor voltage + pros::delay(25); // Run for 25 ms then update + } } \ No newline at end of file