From 8613f8fe4946529b0ef51e9ef9c4dc49efdf458c Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Jun 2024 12:58:49 -0700 Subject: [PATCH] fix onLongPress so it does not repeatedly fire --- include/gamepad/controller.hpp | 1 + src/gamepad/controller.cpp | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 858a45c..b4ef6f4 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -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 {}; }; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 33e1412..0ab9851 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -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(); }