diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index d3944a0..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 @@ -45,9 +46,6 @@ class Button { 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. @@ -68,9 +66,24 @@ 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) {} + 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 +}; + +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 9b06ea4..9a601d9 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -1,8 +1,8 @@ #include "gamepad/controller.hpp" #include "gamepad/todo.hpp" +#include "pros/misc.h" namespace Gamepad { - uint32_t Button::onPress(std::function func) { return this->onPressEvent.add_listener(std::move(func)); } uint32_t Button::onLongPress(std::function func) { @@ -43,12 +43,14 @@ 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) { @@ -57,28 +59,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); } } diff --git a/src/main.cpp b/src/main.cpp index 3085d03..30d56e9 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,21 +75,16 @@ 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 + pros::delay(25); // Run for 25 ms then update } } \ No newline at end of file