From a0639ec3f1e4ac4f136b9e6d2e18c6337d9cf4b9 Mon Sep 17 00:00:00 2001 From: Erki Date: Fri, 4 Nov 2022 00:17:25 +0200 Subject: [PATCH] Peripherals: Better button logic Long press is now registered while the button is still held down --- Peripherals/Inc/peripherals_button.hpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Peripherals/Inc/peripherals_button.hpp b/Peripherals/Inc/peripherals_button.hpp index 954769a..37d4e03 100644 --- a/Peripherals/Inc/peripherals_button.hpp +++ b/Peripherals/Inc/peripherals_button.hpp @@ -42,19 +42,24 @@ public: const bool is_pressed = sw.read(); ButtonPress new_state = ButtonPress::NOT_PRESSED; + const std::uint32_t time_held = hal::getMillis() - time_pressed_down_; + if (is_pressed && !was_pressed_) { time_pressed_down_ = hal::getMillis(); - } else if (!is_pressed && was_pressed_) + } + else if (!is_pressed && was_pressed_) { - const std::uint32_t time_held = hal::getMillis() - time_pressed_down_; - if (time_held > TIMEOUT_LONG_PRESS) - new_state = ButtonPress::LONG_PRESS; - else if (time_held > TIMEOUT_SHORT_PRESS) + if (time_held > TIMEOUT_SHORT_PRESS) new_state = ButtonPress::SHORT_PRESS; time_pressed_down_ = 0; } + else if (is_pressed && was_pressed_) + { + if (current_state_ == ButtonPress::NOT_PRESSED && time_held > TIMEOUT_LONG_PRESS) + new_state = ButtonPress::LONG_PRESS; + } was_pressed_ = is_pressed; current_state_ = new_state;