1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Make LCD button input more efficient

This commit is contained in:
Rhys Weatherley
2012-05-21 12:49:15 +10:00
parent e627f5642f
commit 9e2f41a135
2 changed files with 116 additions and 25 deletions

View File

@@ -21,18 +21,12 @@
*/
#include "FreetronicsLCD.h"
#include <avr/pgmspace.h>
#include <WProgram.h>
#define LCD_BACK_LIGHT 3 // LCD backlight is on D3
#define LCD_BUTTON_PIN A0 // Button state is on A0
#define LCD_BUTTON_VALUE_GAP 10
#define LCD_BUTTON_RIGHT_VALUE 0
#define LCD_BUTTON_UP_VALUE 145
#define LCD_BUTTON_DOWN_VALUE 329
#define LCD_BUTTON_LEFT_VALUE 505
#define LCD_BUTTON_SELECT_VALUE 741
#define DEBOUNCE_DELAY 10 // Delay in ms to debounce buttons
/**
@@ -261,6 +255,13 @@ void FreetronicsLCD::disableScreenSaver()
* \sa enableScreenSaver()
*/
// Button mapping table generated by genlookup.c
static prog_uint8_t const buttonMappings[] PROGMEM = {
2, 0, 0, 0, 3, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0
};
#define mapButton(value) (pgm_read_byte(&(buttonMappings[(value) >> 5])))
/**
* \brief Gets the next button press, release, or idle event.
*
@@ -289,24 +290,7 @@ void FreetronicsLCD::disableScreenSaver()
int FreetronicsLCD::getButton()
{
// Read the currently pressed button.
int value = analogRead(LCD_BUTTON_PIN);
int button;
if (value < (LCD_BUTTON_RIGHT_VALUE + LCD_BUTTON_VALUE_GAP))
button = LCD_BUTTON_RIGHT;
else if (value >= (LCD_BUTTON_UP_VALUE - LCD_BUTTON_VALUE_GAP) &&
value <= (LCD_BUTTON_UP_VALUE + LCD_BUTTON_VALUE_GAP))
button = LCD_BUTTON_UP;
else if (value >= (LCD_BUTTON_DOWN_VALUE - LCD_BUTTON_VALUE_GAP) &&
value <= (LCD_BUTTON_DOWN_VALUE + LCD_BUTTON_VALUE_GAP))
button = LCD_BUTTON_DOWN;
else if (value >= (LCD_BUTTON_LEFT_VALUE - LCD_BUTTON_VALUE_GAP) &&
value <= (LCD_BUTTON_LEFT_VALUE + LCD_BUTTON_VALUE_GAP))
button = LCD_BUTTON_LEFT;
else if (value >= (LCD_BUTTON_SELECT_VALUE - LCD_BUTTON_VALUE_GAP) &&
value <= (LCD_BUTTON_SELECT_VALUE + LCD_BUTTON_VALUE_GAP))
button = LCD_BUTTON_SELECT;
else
button = LCD_BUTTON_NONE;
int button = mapButton(analogRead(LCD_BUTTON_PIN));
// Debounce the button state.
unsigned long currentTime = millis();