Skip to content

Commit

Permalink
Fixed debounce code to handle noisy switch transitions #989 (#1030)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
NotherNgineer committed May 22, 2023
1 parent be1b271 commit 4d12690
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 15 additions & 8 deletions firmware/application/hw/debounce.cpp
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions firmware/application/hw/debounce.hpp
Expand Up @@ -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);
Expand Down

0 comments on commit 4d12690

Please sign in to comment.