Improvement on the button responsiveness. The buttons now work better when pressed during a measurement.

This commit is contained in:
Maurice Makaay 2020-07-18 16:57:56 +02:00
parent 09f3be6dcb
commit 4233764120
2 changed files with 7 additions and 17 deletions

View File

@ -16,15 +16,12 @@ namespace Dough
// //
// // Construct the button instance. // // Construct the button instance.
// Button myButton(MYBUTTON_PIN, myButtonISR); // Button myButton(MYBUTTON_PIN, myButtonISR);
Button::Button(int pin, ButtonISR isr) Button::Button(int pin, ButtonISR isr) : _pin(pin), _isr(isr) {}
{
_pin = pin;
attachInterrupt(digitalPinToInterrupt(_pin), isr, CHANGE);
}
void Button::setup() void Button::setup()
{ {
pinMode(_pin, INPUT_PULLUP); pinMode(_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(_pin), _isr, CHANGE);
} }
// Assign an event handler for short and long button presses. // Assign an event handler for short and long button presses.
@ -95,7 +92,7 @@ namespace Dough
bool buttonIsUp = !buttonIsDown; bool buttonIsUp = !buttonIsDown;
// When the button state has changed since the last time, then // When the button state has changed since the last time, then
// start the debounce timer. // restart the debounce timer.
if (buttonIsDown != _debounceState) if (buttonIsDown != _debounceState)
{ {
_debounceTimer = millis(); _debounceTimer = millis();
@ -104,14 +101,6 @@ namespace Dough
unsigned long interval = (millis() - _debounceTimer); 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. // Handle button state changes.
if (_state == READY_FOR_NEXT_PRESS && buttonIsUp) if (_state == READY_FOR_NEXT_PRESS && buttonIsUp)
{ {
@ -125,7 +114,7 @@ namespace Dough
{ {
_state = DOWN_LONG; _state = DOWN_LONG;
} }
else if (_state == DOWN && buttonIsUp) else if (_state == DOWN && buttonIsUp && interval > BUTTON_DEBOUNCE_DELAY)
{ {
_state = UP_AFTER_SHORT; _state = UP_AFTER_SHORT;
} }

View File

@ -1,8 +1,8 @@
#ifndef DOUGH_BUTTON_H #ifndef DOUGH_BUTTON_H
#define DOUGH_BUTTON_H #define DOUGH_BUTTON_H
#define BUTTON_DEBOUNCE_DELAY 50 #define BUTTON_DEBOUNCE_DELAY 10
#define BUTTON_LONGPRESS_DELAY 1000 #define BUTTON_LONGPRESS_DELAY 500
#include <Arduino.h> #include <Arduino.h>
@ -41,6 +41,7 @@ namespace Dough
private: private:
int _pin; int _pin;
ButtonISR _isr;
ButtonISR _pressHandler = nullptr; ButtonISR _pressHandler = nullptr;
ButtonISR _shortPressHandler = nullptr; ButtonISR _shortPressHandler = nullptr;
ButtonISR _longPressHandler = nullptr; ButtonISR _longPressHandler = nullptr;