Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Global instances of gamepad #7

Merged
merged 12 commits into from
Aug 21, 2024
21 changes: 17 additions & 4 deletions include/gamepad/controller.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "pros/misc.h"
#include <cstdint>
#include <functional>
#ifndef PROS_USE_SIMPLE_NAMES
Expand Down Expand Up @@ -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.
Expand All @@ -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
46 changes: 24 additions & 22 deletions src/gamepad/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "gamepad/controller.hpp"
#include "gamepad/todo.hpp"
#include "pros/misc.h"

namespace Gamepad {

uint32_t Button::onPress(std::function<void(void)> func) { return this->onPressEvent.add_listener(std::move(func)); }

uint32_t Button::onLongPress(std::function<void(void)> func) {
Expand Down Expand Up @@ -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<pros::controller_digital_e_t>(i)); }
for (int i = pros::E_CONTROLLER_DIGITAL_L1; i != pros::E_CONTROLLER_DIGITAL_A; ++i) {
this->updateButton(static_cast<pros::controller_digital_e_t>(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) {
Expand All @@ -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")
SizzinSeal marked this conversation as resolved.
Show resolved Hide resolved
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")
SizzinSeal marked this conversation as resolved.
Show resolved Hide resolved
default: std::exit(1);
}
}
Expand Down
29 changes: 13 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "main.h"
#include "gamepad/api.hpp"

/**
* A callback function for LLEMU's center button.
Expand Down Expand Up @@ -74,21 +75,17 @@ 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
ion098 marked this conversation as resolved.
Show resolved Hide resolved
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

// 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
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(20); // Run for 20 ms then update
ion098 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading