diff --git a/Peripherals/Inc/peripherals_button.hpp b/Peripherals/Inc/peripherals_button.hpp index 47fc354..8813b5f 100644 --- a/Peripherals/Inc/peripherals_button.hpp +++ b/Peripherals/Inc/peripherals_button.hpp @@ -42,7 +42,7 @@ public: Button() = delete; explicit Button(const gpio& sw) - : sw(sw) + : sw(sw), was_pressed_(sw.read()) {} void update() @@ -59,7 +59,15 @@ public: else if (!is_pressed && was_pressed_) { if (time_held > TIMEOUT_SHORT_PRESS && time_held <= TIMEOUT_LONG_PRESS) + { new_state = ButtonPress::SHORT_PRESS; + } + + if (state_exhausted_) + { + current_state_ = ButtonPress::NOT_PRESSED; + state_exhausted_ = false; + } time_pressed_down_ = 0; } @@ -70,12 +78,24 @@ public: } was_pressed_ = is_pressed; - current_state_ = new_state; + + if (!state_exhausted_) + current_state_ = new_state; } [[nodiscard]] ButtonPress getState() const { - return current_state_; + if (!state_exhausted_) + { + if (current_state_ != ButtonPress::NOT_PRESSED && current_state_ != ButtonPress::SHORT_PRESS) + state_exhausted_ = true; + + return current_state_; + } + else + { + return ButtonPress::NOT_PRESSED; + } } #ifdef SKULLC_WITH_CORO @@ -96,9 +116,10 @@ public: #endif private: - bool was_pressed_ = false; + bool was_pressed_; std::uint32_t time_pressed_down_ = 0; ButtonPress current_state_ = ButtonPress::NOT_PRESSED; + mutable bool state_exhausted_ = false; }; }// namespace Peripherals