forked from rjbatista/tm1638-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayKeypadUI.cpp
250 lines (211 loc) · 6.74 KB
/
DisplayKeypadUI.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "DisplayKeypadUI.h"
#define DEBUG
// use this to update
byte DisplayKeypadUI::debugInputStatus(byte newStatus)
{
#ifdef DEBUG
Serial.println("Input - Status:" + String(newStatus));
#endif
return newStatus;
}
String DisplayKeypadUI::enterText( const String label, uint8_t minLength, uint8_t maxLength, bool allowAlphaNumeric, String edit)
{
#define INPUT_STATUS_LABEL 1 // display label
#define INPUT_STATUS_INPROGRESS 2 // awaiting further input
#define INPUT_STATUS_SUCCESS 3 // input minimum achieved
#define INPUT_STATUS_ABORT 4 // aborted
#define INPUT_KEY_CYCLE_MILLIS_MAX 1000
#define INPUT_KEY_CYCLE_ARRAY_SIZE 12
String keyCycleArray[INPUT_KEY_CYCLE_ARRAY_SIZE] = {
"1-.", "2abcABC", "3defDEF",
"4ghiGHI", "5jklJKL", "6mnoMNO",
"7pqrsPQRS", "8tuvTUV", "9wxyzWXYZ",
"*", "0", "#"};
// Test: 33 555 9999 307 777 222 77 839
uint8_t status = debugInputStatus(INPUT_STATUS_LABEL);
byte scrollPos = 0;
unsigned long scrollMillis = 0;
unsigned long currentMillis = 0;
unsigned long keyMillis = 0;
unsigned long lastKeyMillis = 0;
char key = NO_KEY;
char lastKey;
_dispKpd.clearDisplay();
String input = edit;
bool editMode = (input != "");
do
{
currentMillis = millis();
char newKey = _kpd.getKey();
switch (_kpd.getState())
{
case RELEASED:
lastKeyMillis = keyMillis;
keyMillis = currentMillis;
lastKey = key;
key = newKey;
break;
default:
newKey = NO_KEY;
}
if (status == INPUT_STATUS_LABEL)
{
switch (newKey)
{
case NO_KEY:
_dispKpd.scrollDisplayToString(label, &scrollPos, &scrollMillis);
break;
case INPUT_ABBORT_CHAR:
status = INPUT_STATUS_ABORT;
input = "";
break;
case INPUT_CONFIRM_CHAR:
if (input.length() >= minLength)
status = debugInputStatus(INPUT_STATUS_SUCCESS);
break;
default:
_dispKpd.clearDisplay();
status = debugInputStatus(INPUT_STATUS_INPROGRESS);
}
}
if (status == INPUT_STATUS_INPROGRESS)
{
switch (newKey)
{
case NO_KEY:
break;
case INPUT_CONFIRM_CHAR:
if (input.length() >= minLength)
{
status = debugInputStatus(INPUT_STATUS_SUCCESS);
break;
}
case INPUT_ABBORT_CHAR:
if (input.length() > 0)
input.remove(input.length() - 1);
else
status = debugInputStatus(INPUT_STATUS_ABORT);
break;
default:
// in edit mode, first key is not recognized
if (editMode)
editMode = false;
else if (allowAlphaNumeric && (newKey == lastKey) && (keyMillis - lastKeyMillis < INPUT_KEY_CYCLE_MILLIS_MAX))
{
char currentInput = input.charAt(input.length() - 1);
for (int i = 0; i < INPUT_KEY_CYCLE_ARRAY_SIZE; ++i)
{
String keyMap = keyCycleArray[i];
if (keyMap.startsWith(String(newKey)))
{ // first character
int c = keyMap.indexOf(currentInput);
if (c < keyMap.length())
c++;
else
c = 0;
currentInput = keyMap.charAt(c);
break;
}
}
input.remove(input.length() - 1); // remove last char
input.concat(currentInput); // add current
}
else
input.concat(newKey); // input.length() starts with 1
}
// update display if there was a key event
if (newKey != NO_KEY)
{
// show last _dispKpd.getDisplayDigits() of entered intput (scroll to right)
unsigned int start = max(0, int(input.length() - _dispKpd.getDisplayDigits()));
unsigned int end = start + min(int(input.length()), _dispKpd.getDisplayDigits());
String txt = input.substring(start, end);
_dispKpd.setDisplayToString(txt);
}
}
// allow background tasks to be processed
yield();
delay(10); // or yield() could be called as well but not generally supported for all arduino platforms
} while ((status != INPUT_STATUS_SUCCESS) && (status != INPUT_STATUS_ABORT));
_dispKpd.clearDisplay();
return input;
}
// selections[0] is label; returns selection 1-numSelections; return 0 is abort/not selected
uint8_t DisplayKeypadUI::selectOptions( String selections[], uint8_t numSelections, uint8_t selected)
{
#define INPUT_LABEL_MAX_LENGTH 32
#define INPUT_STATUS_LABEL 1 // display label
#define INPUT_STATUS_INPROGRESS 2 // awaiting further input
#define INPUT_STATUS_SUCCESS 3 // input minimum achieved
#define INPUT_STATUS_ABORT 4 // aborted
int8_t curSel = 0; // show label
uint8_t status = debugInputStatus(INPUT_STATUS_LABEL);
unsigned long previousMillis = 0;
uint8_t scrollPos = 0;
_dispKpd.clearDisplay();
bool showSelectedFirst = (selected != 0);
Serial.println("curSel=" + String(curSel) + " numSelections=" + String(numSelections));
do
{
_dispKpd.scrollDisplayToString(selections[curSel], &scrollPos, &previousMillis);
char key = _kpd.getKey();
if (key)
{
KeyState state = _kpd.getState();
if (state) // Only find keys that have changed state.
{
switch (state)
{ // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
case PRESSED:
break;
case HOLD:
break;
case RELEASED:
switch (key)
{
case INPUT_ABBORT_CHAR:
status = debugInputStatus(INPUT_STATUS_ABORT);
break;
case INPUT_CONFIRM_CHAR:
if (curSel > 0)
{
status = debugInputStatus(INPUT_STATUS_SUCCESS);
}
break;
default:
status = debugInputStatus(INPUT_STATUS_INPROGRESS);
if (showSelectedFirst)
{
Serial.println("showSelectFirst=" + String(selected));
showSelectedFirst = false;
curSel = selected;
}
else
{
// display next selection or start over with first selection
if (numSelections > 0) // if numSelections is 0 it keeps showing label until abbort
{
if (curSel < numSelections)
curSel++;
else
curSel = 1;
}
Serial.println(curSel);
}
// start scrolling next entry
_dispKpd.clearDisplay();
scrollPos = 0;
previousMillis = 0;
}
break;
case IDLE:
break;
}
}
}
delay(10);
yield();
} while ((status != INPUT_STATUS_SUCCESS) && (status != INPUT_STATUS_ABORT));
_dispKpd.clearDisplay();
return curSel;
}