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:
parent
e627f5642f
commit
9e2f41a135
107
gen/genlookup.c
Normal file
107
gen/genlookup.c
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2012 Southern Storm Software, Pty Ltd.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
|
* to deal in the Software without restriction, including without limitation
|
||||||
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
* DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Utility for generating the button mapping table in FreetronicsLCD.cpp.
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define LCD_BUTTON_NONE 0
|
||||||
|
#define LCD_BUTTON_LEFT 1
|
||||||
|
#define LCD_BUTTON_RIGHT 2
|
||||||
|
#define LCD_BUTTON_UP 3
|
||||||
|
#define LCD_BUTTON_DOWN 4
|
||||||
|
#define LCD_BUTTON_SELECT 5
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char rawTest[1024];
|
||||||
|
int value, value2, value3, bits;
|
||||||
|
char button;
|
||||||
|
|
||||||
|
// Determine the actual values for each of the 1024 inputs.
|
||||||
|
for (value = 0; value < 1024; ++value) {
|
||||||
|
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;
|
||||||
|
rawTest[value] = button;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dump the accuracy of different bit truncations.
|
||||||
|
for (bits = 4; bits < 10; ++bits) {
|
||||||
|
int count = 0;
|
||||||
|
for (value = 0; value < 1024; ++value) {
|
||||||
|
value2 = value & (((1 << bits) - 1) << (10 - bits));
|
||||||
|
if (rawTest[value] == rawTest[value2])
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
printf("bits = %d, accuracy = %g%%\n", bits, 100.0 * count / 1024.0);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Dump the button mapping table for the selected bit count.
|
||||||
|
bits = 5;
|
||||||
|
printf("static prog_uint8_t const buttonMappings[] PROGMEM = {\n");
|
||||||
|
for (value2 = 0; value2 < (1 << bits); ++value2) {
|
||||||
|
value = value2 << (10 - bits);
|
||||||
|
value3 = value + (1 << (10 - bits)) - 1;
|
||||||
|
button = 0;
|
||||||
|
while (!button && value <= value3) {
|
||||||
|
button = rawTest[value];
|
||||||
|
++value;
|
||||||
|
}
|
||||||
|
if ((value2 & 0x0F) != 0)
|
||||||
|
printf(", ");
|
||||||
|
else
|
||||||
|
printf(" ");
|
||||||
|
printf("%d", button);
|
||||||
|
if ((value2 & 0x0F) == 0x0F) {
|
||||||
|
if (value2 < ((1 << bits - 1)))
|
||||||
|
printf(",\n");
|
||||||
|
else
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("};\n");
|
||||||
|
printf("#define mapButton(value) (pgm_read_byte(&(buttonMappings[(value) >> %d])))\n", 10 - bits);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -21,18 +21,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "FreetronicsLCD.h"
|
#include "FreetronicsLCD.h"
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
#include <WProgram.h>
|
#include <WProgram.h>
|
||||||
|
|
||||||
#define LCD_BACK_LIGHT 3 // LCD backlight is on D3
|
#define LCD_BACK_LIGHT 3 // LCD backlight is on D3
|
||||||
#define LCD_BUTTON_PIN A0 // Button state is on A0
|
#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
|
#define DEBOUNCE_DELAY 10 // Delay in ms to debounce buttons
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -261,6 +255,13 @@ void FreetronicsLCD::disableScreenSaver()
|
|||||||
* \sa enableScreenSaver()
|
* \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.
|
* \brief Gets the next button press, release, or idle event.
|
||||||
*
|
*
|
||||||
@ -289,24 +290,7 @@ void FreetronicsLCD::disableScreenSaver()
|
|||||||
int FreetronicsLCD::getButton()
|
int FreetronicsLCD::getButton()
|
||||||
{
|
{
|
||||||
// Read the currently pressed button.
|
// Read the currently pressed button.
|
||||||
int value = analogRead(LCD_BUTTON_PIN);
|
int button = mapButton(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;
|
|
||||||
|
|
||||||
// Debounce the button state.
|
// Debounce the button state.
|
||||||
unsigned long currentTime = millis();
|
unsigned long currentTime = millis();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user