mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
Debounce the button state in FreetronicsLCD class
This commit is contained in:
parent
45becdfc0e
commit
3487e6ec61
@ -33,6 +33,8 @@
|
|||||||
#define LCD_BUTTON_LEFT_VALUE 505
|
#define LCD_BUTTON_LEFT_VALUE 505
|
||||||
#define LCD_BUTTON_SELECT_VALUE 741
|
#define LCD_BUTTON_SELECT_VALUE 741
|
||||||
|
|
||||||
|
#define DEBOUNCE_DELAY 10 // Delay in ms to debounce buttons
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \class FreetronicsLCD FreetronicsLCD.h <FreetronicsLCD.h>
|
* \class FreetronicsLCD FreetronicsLCD.h <FreetronicsLCD.h>
|
||||||
* \brief Enhanced library for Freetronics 16x2 LCD shields
|
* \brief Enhanced library for Freetronics 16x2 LCD shields
|
||||||
@ -123,6 +125,8 @@ void FreetronicsLCD::init()
|
|||||||
pinMode(LCD_BUTTON_PIN, INPUT);
|
pinMode(LCD_BUTTON_PIN, INPUT);
|
||||||
digitalWrite(LCD_BUTTON_PIN, LOW);
|
digitalWrite(LCD_BUTTON_PIN, LOW);
|
||||||
prevButton = LCD_BUTTON_NONE;
|
prevButton = LCD_BUTTON_NONE;
|
||||||
|
debounceButton = LCD_BUTTON_NONE;
|
||||||
|
lastDebounce = 0;
|
||||||
eatRelease = false;
|
eatRelease = false;
|
||||||
|
|
||||||
// Initialize screen saver.
|
// Initialize screen saver.
|
||||||
@ -224,10 +228,14 @@ void FreetronicsLCD::disableScreenSaver()
|
|||||||
* will "eat" the button press and return LCD_BUTTON_NONE. The scrren saver
|
* will "eat" the button press and return LCD_BUTTON_NONE. The scrren saver
|
||||||
* can also be deactivated under program control by calling display()
|
* can also be deactivated under program control by calling display()
|
||||||
*
|
*
|
||||||
|
* This function debounces the button state automatically so there is no
|
||||||
|
* need for the caller to worry about spurious button events.
|
||||||
|
*
|
||||||
* \sa enableScreenSaver(), display(), Form::dispatch()
|
* \sa enableScreenSaver(), display(), Form::dispatch()
|
||||||
*/
|
*/
|
||||||
int FreetronicsLCD::getButton()
|
int FreetronicsLCD::getButton()
|
||||||
{
|
{
|
||||||
|
// Read the currently pressed button.
|
||||||
int value = analogRead(LCD_BUTTON_PIN);
|
int value = analogRead(LCD_BUTTON_PIN);
|
||||||
int button;
|
int button;
|
||||||
if (value < (LCD_BUTTON_RIGHT_VALUE + LCD_BUTTON_VALUE_GAP))
|
if (value < (LCD_BUTTON_RIGHT_VALUE + LCD_BUTTON_VALUE_GAP))
|
||||||
@ -246,6 +254,16 @@ int FreetronicsLCD::getButton()
|
|||||||
button = LCD_BUTTON_SELECT;
|
button = LCD_BUTTON_SELECT;
|
||||||
else
|
else
|
||||||
button = LCD_BUTTON_NONE;
|
button = LCD_BUTTON_NONE;
|
||||||
|
|
||||||
|
// Debounce the button state.
|
||||||
|
unsigned long currentTime = millis();
|
||||||
|
if (button != debounceButton)
|
||||||
|
lastDebounce = currentTime;
|
||||||
|
debounceButton = button;
|
||||||
|
if ((currentTime - lastDebounce) < DEBOUNCE_DELAY)
|
||||||
|
button = prevButton;
|
||||||
|
|
||||||
|
// Process the button event if the state has changed.
|
||||||
if (prevButton == LCD_BUTTON_NONE && button != LCD_BUTTON_NONE) {
|
if (prevButton == LCD_BUTTON_NONE && button != LCD_BUTTON_NONE) {
|
||||||
prevButton = button;
|
prevButton = button;
|
||||||
if (screenSaved) {
|
if (screenSaved) {
|
||||||
@ -255,12 +273,12 @@ int FreetronicsLCD::getButton()
|
|||||||
return LCD_BUTTON_NONE;
|
return LCD_BUTTON_NONE;
|
||||||
}
|
}
|
||||||
eatRelease = false;
|
eatRelease = false;
|
||||||
lastRestore = millis();
|
lastRestore = currentTime;
|
||||||
return button;
|
return button;
|
||||||
} else if (prevButton != LCD_BUTTON_NONE && button == LCD_BUTTON_NONE) {
|
} else if (prevButton != LCD_BUTTON_NONE && button == LCD_BUTTON_NONE) {
|
||||||
button = -prevButton;
|
button = -prevButton;
|
||||||
prevButton = LCD_BUTTON_NONE;
|
prevButton = LCD_BUTTON_NONE;
|
||||||
lastRestore = millis();
|
lastRestore = currentTime;
|
||||||
if (eatRelease) {
|
if (eatRelease) {
|
||||||
eatRelease = false;
|
eatRelease = false;
|
||||||
return LCD_BUTTON_NONE;
|
return LCD_BUTTON_NONE;
|
||||||
@ -268,7 +286,7 @@ int FreetronicsLCD::getButton()
|
|||||||
return button;
|
return button;
|
||||||
} else {
|
} else {
|
||||||
if (!screenSaved && prevButton == LCD_BUTTON_NONE &&
|
if (!screenSaved && prevButton == LCD_BUTTON_NONE &&
|
||||||
timeout != 0 && (millis() - lastRestore) >= timeout)
|
timeout != 0 && (currentTime - lastRestore) >= timeout)
|
||||||
noDisplay(); // Activate screen saver.
|
noDisplay(); // Activate screen saver.
|
||||||
return LCD_BUTTON_NONE;
|
return LCD_BUTTON_NONE;
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,10 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
int prevButton;
|
int prevButton;
|
||||||
|
int debounceButton;
|
||||||
unsigned long timeout;
|
unsigned long timeout;
|
||||||
unsigned long lastRestore;
|
unsigned long lastRestore;
|
||||||
|
unsigned long lastDebounce;
|
||||||
bool screenSaved;
|
bool screenSaved;
|
||||||
bool eatRelease;
|
bool eatRelease;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user