Skip to content

Commit

Permalink
fix onLongPress so it does not repeatedly fire
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Jun 2, 2024
1 parent c397474 commit 8613f8f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/gamepad/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Button {

uint32_t last_update_time = pros::millis();
mutable EventHandler<> onPressEvent {};
uint32_t last_long_press_time = 0;
mutable EventHandler<> onLongPressEvent {};
mutable EventHandler<> onReleaseEvent {};
};
Expand Down
4 changes: 3 additions & 1 deletion src/gamepad/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ void Button::update(const bool is_held) {

if (this->rising_edge) {
this->onPressEvent.fire();
} else if (this->is_pressed && this->time_held >= this->long_press_threshold) {
} else if (this->is_pressed && this->time_held >= this->long_press_threshold &&
this->last_long_press_time > pros::millis() - this->time_held) {
TODO("change onLongPress handling if onPress is present")
this->onLongPressEvent.fire();
this->last_long_press_time = pros::millis();
} else if (this->falling_edge) {
this->onReleaseEvent.fire();
}
Expand Down

0 comments on commit 8613f8f

Please sign in to comment.