Skip to content

Commit

Permalink
Format all files
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed May 24, 2024
1 parent c3fa979 commit 565ade4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 90 deletions.
56 changes: 27 additions & 29 deletions include/gamepad/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@ 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<void(void)> func) const;
uint32_t onLongPress(std::function<void(void)> func) const;
uint32_t onRelease(std::function<void(void)> func) const;
uint32_t addListener(EventType event, std::function<void(void)> func) const;
bool removeListener(uint32_t id) const;
explicit operator bool() const {
return is_pressed;
}
private:
uint32_t onPress(std::function<void(void)> func) const;
uint32_t onLongPress(std::function<void(void)> func) const;
uint32_t onRelease(std::function<void(void)> func) const;
uint32_t addListener(EventType event, std::function<void(void)> func) const;
bool removeListener(uint32_t id) const;

void update(bool is_held);
explicit operator bool() const { return is_pressed; }
private:
void update(bool is_held);

uint32_t last_update_time = pros::millis();
mutable EventHandler<> onPressEvent{};
mutable EventHandler<> onLongPressEvent{};
mutable EventHandler<> onReleaseEvent{};
uint32_t last_update_time = pros::millis();
mutable EventHandler<> onPressEvent {};
mutable EventHandler<> onLongPressEvent {};
mutable EventHandler<> onReleaseEvent {};
};

class Controller {
public:
explicit Controller(pros::controller_id_e_t id): controller(id) {}
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 @@ -69,13 +69,11 @@ 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:
static Button Controller::* button_to_ptr(pros::controller_digital_e_t button);
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
}
} // namespace Gamepad
70 changes: 36 additions & 34 deletions include/gamepad/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,46 @@
namespace Gamepad {

class MonotonicCounter {
template<typename... Args> friend class EventHandler;
static uint32_t next_value() {
static std::atomic<uint32_t> counter = 0;
return ++counter;
}
template <typename... Args> friend class EventHandler;

static uint32_t next_value() {
static std::atomic<uint32_t> counter = 0;
return ++counter;
}
};

template <typename... Args>
class EventHandler {
template <typename... Args> class EventHandler {
public:
using Listener = std::function<void(Args...)>;
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<void(Args...)>;

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<uint32_t, Listener> listeners;
pros::Mutex mutex;
std::map<uint32_t, Listener> listeners;
pros::Mutex mutex;
};
}
} // namespace Gamepad
6 changes: 3 additions & 3 deletions include/gamepad/todo.hpp
Original file line number Diff line number Diff line change
@@ -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))
#define DO_PRAGMA(x) _Pragma(#x)
#define TODO(x) DO_PRAGMA(message("TODO - " #x))
#define FIXME(x) DO_PRAGMA(warning("FIXME - " #x))
38 changes: 14 additions & 24 deletions src/gamepad/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ uint32_t Button::onRelease(std::function<void(void)> func) const {

uint32_t Button::addListener(EventType event, std::function<void(void)> func) const {
switch (event) {
case Gamepad::EventType::ON_PRESS:
this->onPress(std::move(func));
case Gamepad::EventType::ON_LONG_PRESS:
this->onLongPress(std::move(func));
case Gamepad::EventType::ON_RELEASE:
this->onRelease(std::move(func));
case Gamepad::EventType::ON_PRESS: this->onPress(std::move(func));
case Gamepad::EventType::ON_LONG_PRESS: this->onLongPress(std::move(func));
case Gamepad::EventType::ON_RELEASE: this->onRelease(std::move(func));
}
}

bool Button::removeListener(uint32_t id) const {
return this->onPressEvent.remove_listener(id) || this->onLongPressEvent.remove_listener(id) || this->onReleaseEvent.remove_listener(id);
return this->onPressEvent.remove_listener(id) || this->onLongPressEvent.remove_listener(id) ||
this->onReleaseEvent.remove_listener(id);
}

void Button::update(const bool is_held) {
Expand All @@ -39,34 +37,28 @@ void Button::update(const bool is_held) {
} else {
this->time_released += pros::millis() - this->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) {
this->onPressEvent.fire();
} else if (this->is_pressed && this->time_held >= this->long_press_threshold) {
TODO("change onLongPress handling if onPress is present")
this->onLongPressEvent.fire();
}else if (this->falling_edge) {
} else if (this->falling_edge) {
this->onReleaseEvent.fire();
}
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);
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<pros::controller_digital_e_t>(i));
}
for (int i = DIGITAL_L1; i != 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);
Expand All @@ -83,13 +75,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;
Expand All @@ -102,9 +93,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);
}
}
}
} // namespace Gamepad

0 comments on commit 565ade4

Please sign in to comment.