From 9e2f41a135146c7ea8df11b3c30ceeba07f13fd4 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Mon, 21 May 2012 12:49:15 +1000 Subject: [PATCH] Make LCD button input more efficient --- gen/genlookup.c | 107 ++++++++++++++++++++ libraries/FreetronicsLCD/FreetronicsLCD.cpp | 34 ++----- 2 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 gen/genlookup.c diff --git a/gen/genlookup.c b/gen/genlookup.c new file mode 100644 index 00000000..b5bc9714 --- /dev/null +++ b/gen/genlookup.c @@ -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 + +#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; +} diff --git a/libraries/FreetronicsLCD/FreetronicsLCD.cpp b/libraries/FreetronicsLCD/FreetronicsLCD.cpp index 0aa43467..3cfcf331 100644 --- a/libraries/FreetronicsLCD/FreetronicsLCD.cpp +++ b/libraries/FreetronicsLCD/FreetronicsLCD.cpp @@ -21,18 +21,12 @@ */ #include "FreetronicsLCD.h" +#include #include #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();