From 42337641204909a79770492529d29a9e37482242 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Sat, 18 Jul 2020 16:57:56 +0200 Subject: [PATCH] Improvement on the button responsiveness. The buttons now work better when pressed during a measurement. --- src/UI/Button.cpp | 19 ++++--------------- src/UI/Button.h | 5 +++-- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/UI/Button.cpp b/src/UI/Button.cpp index ec4d9b2..c89ab30 100644 --- a/src/UI/Button.cpp +++ b/src/UI/Button.cpp @@ -16,15 +16,12 @@ namespace Dough // // // Construct the button instance. // Button myButton(MYBUTTON_PIN, myButtonISR); - Button::Button(int pin, ButtonISR isr) - { - _pin = pin; - attachInterrupt(digitalPinToInterrupt(_pin), isr, CHANGE); - } + Button::Button(int pin, ButtonISR isr) : _pin(pin), _isr(isr) {} void Button::setup() { pinMode(_pin, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(_pin), _isr, CHANGE); } // Assign an event handler for short and long button presses. @@ -95,7 +92,7 @@ namespace Dough bool buttonIsUp = !buttonIsDown; // When the button state has changed since the last time, then - // start the debounce timer. + // restart the debounce timer. if (buttonIsDown != _debounceState) { _debounceTimer = millis(); @@ -104,14 +101,6 @@ namespace Dough unsigned long interval = (millis() - _debounceTimer); - // Only when the last state change has been stable for longer than the - // configured debounce delay, then we accept the current state as - // a stabilized button state. - if (interval < BUTTON_DEBOUNCE_DELAY) - { - return; - } - // Handle button state changes. if (_state == READY_FOR_NEXT_PRESS && buttonIsUp) { @@ -125,7 +114,7 @@ namespace Dough { _state = DOWN_LONG; } - else if (_state == DOWN && buttonIsUp) + else if (_state == DOWN && buttonIsUp && interval > BUTTON_DEBOUNCE_DELAY) { _state = UP_AFTER_SHORT; } diff --git a/src/UI/Button.h b/src/UI/Button.h index 57cde4d..f1eabb4 100644 --- a/src/UI/Button.h +++ b/src/UI/Button.h @@ -1,8 +1,8 @@ #ifndef DOUGH_BUTTON_H #define DOUGH_BUTTON_H -#define BUTTON_DEBOUNCE_DELAY 50 -#define BUTTON_LONGPRESS_DELAY 1000 +#define BUTTON_DEBOUNCE_DELAY 10 +#define BUTTON_LONGPRESS_DELAY 500 #include @@ -41,6 +41,7 @@ namespace Dough private: int _pin; + ButtonISR _isr; ButtonISR _pressHandler = nullptr; ButtonISR _shortPressHandler = nullptr; ButtonISR _longPressHandler = nullptr;