Fixed debounce code to handle noisy switch transitions #989 (#1030)

* Fix debounce to handle noisy switch transitions

* Fix debounce to handle noisy switch transitions

* Fix debounce to handle noisy switch transitions

* Removed inline comment to see if Clang checker will be happy

* Test fix for supposed Clang formatting issue
This commit is contained in:
Mark Thompson 2023-05-21 19:06:37 -05:00 committed by GitHub
parent be1b2716d6
commit 4d1269051b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -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) {
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;
}
if (history_ == 0b11110000) {
} else {
// Previous button state was 1 (pressed);
// Has button been released for DEBOUNCE_COUNT ticks?
if ((history_ & DEBOUNCE_MASK) == 0) {
state_ = 0;
return true;
}
}
return false;
}

View File

@ -24,6 +24,10 @@
#include <cstdint>
// 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);