Skip to content

Commit

Permalink
Fix button debounce code.
Browse files Browse the repository at this point in the history
  • Loading branch information
veroxzik committed Mar 7, 2021
1 parent 912c599 commit e0bf811
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions roxy/button_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,23 @@ class Button_Manager {
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
if (enabled[i]) {
bool read = button_inputs[i].get();
if ((Time::time() - button_time[i]) >= config.debounce_time)
{
if (last_state[i] != read)

// If state is now different
if (last_state[i] != read) {
// Check if debounce has passed
if ((Time::time() - button_time[i]) >= config.debounce_time)
{
// If yes, set new time and set new state
button_time[i] = Time::time();
last_state[i] = read;
}
}

if (!read) {
uint8_t shift = mapping[i] > 0 ? mapping[i] - 1 : i;
// State reported is always the last state
if (!last_state[i]) {
uint8_t shift = mapping[i] > 0 ? mapping[i] - 1 : i;
buttons |= 1 << shift;
}
last_state[i] = read;
}
} else {
// Clear bit if button is disabled
buttons &= ~(1 << i);
}
}

Expand Down

0 comments on commit e0bf811

Please sign in to comment.