forked from rweather/arduinolibs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genlookup.c
107 lines (98 loc) · 4.01 KB
/
genlookup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 LCD.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 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;
}