From 86c7c308e85bbde06fe298414bb23963f44b6cf5 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 17 Mar 2016 18:56:01 +0100 Subject: add software debouncing in the animation switcher --- src/system.cc | 53 ++++++++++++++++++++++++++++++----------------------- src/system.h | 3 ++- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/system.cc b/src/system.cc index e7c12ff..2d019e1 100644 --- a/src/system.cc +++ b/src/system.cc @@ -241,30 +241,37 @@ void System::loop() want_shutdown = 0; } - if ((PINC & _BV(PC3)) == 0) { - btnMask = (ButtonMask)(btnMask | BUTTON_RIGHT); - } - if ((PINC & _BV(PC7)) == 0) { - btnMask = (ButtonMask)(btnMask | BUTTON_LEFT); - } - - /* - * Only handle button presses when they are released to avoid - * double actions, such as switching to the next/previous pattern - * when the user actually wants to press the shutdown combo. - */ - if ((PINC & (_BV(PC3) | _BV(PC7))) == (_BV(PC3) | _BV(PC7))) { - if (btnMask == BUTTON_RIGHT) { - current_anim_no = (current_anim_no + 1) % storage.numPatterns(); - loadPattern(current_anim_no); - } else if (btnMask == BUTTON_LEFT) { - if (current_anim_no == 0) - current_anim_no = storage.numPatterns() - 1; - else - current_anim_no--; - loadPattern(current_anim_no); + if (btn_debounce == 0) { + if ((PINC & _BV(PC3)) == 0) { + btnMask = (ButtonMask)(btnMask | BUTTON_RIGHT); + } + if ((PINC & _BV(PC7)) == 0) { + btnMask = (ButtonMask)(btnMask | BUTTON_LEFT); } - btnMask = BUTTON_NONE; + /* + * Only handle button presses when they are released to avoid + * double actions, such as switching to the next/previous pattern + * when the user actually wants to press the shutdown combo. + */ + if ((PINC & (_BV(PC3) | _BV(PC7))) == (_BV(PC3) | _BV(PC7))) { + if (btnMask == BUTTON_RIGHT) { + current_anim_no = (current_anim_no + 1) % storage.numPatterns(); + loadPattern(current_anim_no); + } else if (btnMask == BUTTON_LEFT) { + if (current_anim_no == 0) + current_anim_no = storage.numPatterns() - 1; + else + current_anim_no--; + loadPattern(current_anim_no); + } + btnMask = BUTTON_NONE; + /* + * Ignore keypresses for 25ms to work around bouncing buttons + */ + btn_debounce = 100; + } + } else { + btn_debounce--; } while (modem.buffer_available()) { diff --git a/src/system.h b/src/system.h index 870ec35..e5ba9ef 100644 --- a/src/system.h +++ b/src/system.h @@ -19,6 +19,7 @@ class System { private: uint16_t want_shutdown; + uint8_t btn_debounce; uint8_t current_anim_no; void shutdown(void); void receive(void); @@ -57,7 +58,7 @@ class System { ButtonMask btnMask; public: - System() { want_shutdown = 0; rxExpect = START1; current_anim_no = 0; btnMask = BUTTON_NONE;}; + System() { want_shutdown = 0; rxExpect = START1; current_anim_no = 0; btnMask = BUTTON_NONE; btn_debounce = 0;}; /** * Initial MCU setup. Turns off unused peripherals to save power -- cgit v1.2.3