Skip to content

Commit

Permalink
add more event handling methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed May 24, 2024
1 parent 6f8b5fd commit 7c5ac05
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
21 changes: 11 additions & 10 deletions include/gamepad/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ class Button {
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 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);
uint32_t onLongPress(std::function<void(void)> func);
uint32_t onRelease(std::function<void(void)> func);
uint32_t addListener(EventType event, std::function<void(void)> func);
bool removeListener(uint32_t id);
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;
private:

void update(bool is_held);

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

class Controller {
Expand Down
39 changes: 27 additions & 12 deletions src/gamepad/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@

namespace Gamepad {

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

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

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

void Button::update(const bool is_held) {
static uint32_t last_update_time = pros::millis();
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));
}
}

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

void Button::update(const bool is_held) {
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;
this->time_held += pros::millis() - this->last_update_time;
} else {
this->time_released += pros::millis() - last_update_time;
this->time_released += pros::millis() - this->last_update_time;
}
if (this->rising_edge) {
this->time_held = 0;
Expand All @@ -34,12 +47,14 @@ void Button::update(const bool is_held) {
}

if (this->rising_edge) {
onPressEvent.fire();
} else if (this->falling_edge) {
onReleaseEvent.fire();
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) {
this->onReleaseEvent.fire();
}
TODO("implement longPress");
last_update_time = pros::millis();
this->last_update_time = pros::millis();
}

void Controller::updateButton(pros::controller_digital_e_t button_id) {
Expand Down

0 comments on commit 7c5ac05

Please sign in to comment.