diff --git a/firmware/application/hw/debounce.cpp b/firmware/application/hw/debounce.cpp index 2e338a102..7caf4ed22 100644 --- a/firmware/application/hw/debounce.cpp +++ b/firmware/application/hw/debounce.cpp @@ -23,17 +23,24 @@ #include "utility.hpp" +// Returns TRUE if button state changed (after debouncing) bool Debounce::feed(const uint8_t bit) { history_ = (history_ << 1) | (bit & 1); - if (history_ == 0b00001111) { - state_ = 1; - return true; + if (state_ == 0) { + // Previous button state was 0 (released); + // Has button been held for DEBOUNCE_COUNT ticks? + if ((history_ & DEBOUNCE_MASK) == DEBOUNCE_MASK) { + state_ = 1; + return true; + } + } else { + // Previous button state was 1 (pressed); + // Has button been released for DEBOUNCE_COUNT ticks? + if ((history_ & DEBOUNCE_MASK) == 0) { + state_ = 0; + return true; + } } - if (history_ == 0b11110000) { - state_ = 0; - return true; - } - return false; } diff --git a/firmware/application/hw/debounce.hpp b/firmware/application/hw/debounce.hpp index 0d2ba1cd7..52dfe1061 100644 --- a/firmware/application/hw/debounce.hpp +++ b/firmware/application/hw/debounce.hpp @@ -24,6 +24,10 @@ #include +// consecutive # of times button input must be same (<=8) +#define DEBOUNCE_COUNT 4 +#define DEBOUNCE_MASK ((1 << DEBOUNCE_COUNT) - 1) + class Debounce { public: bool feed(const uint8_t bit);