diff options
author | Daniel Friesel <derf@finalrewind.org> | 2016-03-17 18:56:01 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2016-03-17 18:56:01 +0100 |
commit | 86c7c308e85bbde06fe298414bb23963f44b6cf5 (patch) | |
tree | cc614d7c6e122f347abdc5b43c6cb1f99352b514 /src | |
parent | 092f52f813dbec9269e0b35bd984209afaf1a8d5 (diff) |
add software debouncing in the animation switcher
Diffstat (limited to 'src')
-rw-r--r-- | src/system.cc | 53 | ||||
-rw-r--r-- | 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 |