-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStateMachineInput.ino
More file actions
397 lines (365 loc) · 8.05 KB
/
StateMachineInput.ino
File metadata and controls
397 lines (365 loc) · 8.05 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Keypad Input Validation Using State Machine Programming
// Author: Tony Keith
//
// Description: Keypad Input Validation Example
// Input Device - This code uses a 16 button keypad as the input.
// Output Device - 4 X 20 LCD display
//
// See http://statemachineprogramming.blogspot.com/ for full details about code and project.
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Wire.h>
// Comment out to turn Serial debug OFF
#define SERIAL_DEBUG_ENABLED
// States
#define INITIAL 1
#define VALUE1 2
#define VALUE2 3
#define ATSIGN 4
#define HOUR1 5
#define HOUR2 6
#define COLON 7
#define MIN1 8
#define MIN2 9
#define EXECUTE 10
#define CLEAR 11
// 16 button keypad
const byte ROWS = 4; // define four rows
const byte COLS = 4; // define four
char keys [ROWS] [COLS] = {
{'1', '2', '3','@'},
{'4', '5', '6','B'},
{'7', '8', '9','C'},
{':', '0', '#','D'}
};
// Alpha-Numeric Key Mappings:
// A = @ (at sign)
// B = NOT USED
// D = NOT USED
// C = Clear
// * = : (colon)
// # = Execute (accept or enter or execute) the command
// 4x4 16 button - membrane keypad
// Pin R/C Port
// 8 C4 13
// 7 C3 12
// 6 C2 11
// 5 C1 10
// 4 R4 9
// 3 R3 8
// 2 R2 7
// 1 R1 6
// Connect 4 * 4 keypad row-bit port, the corresponding digital IO ports panel
byte rowPins [ROWS] = {6,7,8,9};
// Connect 4 * 4 buttons faithfully port, the corresponding digital IO ports panel
byte colPins [COLS] = {10,11,12,13};
// Call the function library function Keypad
Keypad keypad = Keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS);
// Initialize the display
LiquidCrystal_I2C lcd(0x3F,20,4);
#define MAXCMDBUFFER 15
static char buffer[MAXCMDBUFFER];
static int index=0;
static int pos = 0;
static int state=INITIAL;
void setup () {
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("State Machine ");
lcd.setCursor(0,1);
lcd.print("Enter Cmd:");
// Serial
#ifdef SERIAL_DEBUG_ENABLED
Serial.begin (9600);
#endif
} // setup()
void loop () {
char key = keypad.getKey ();
if (key != NO_KEY) {
// Clear
if(key == 'C') {
state = CLEAR;
}
displayState(state);
switch(state) {
case INITIAL:
// X
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = VALUE1;
} else {
invalidFormat();
}
break;
case VALUE1:
// XX
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = VALUE2;
} else if (validAtSign(key)) {
// X@
pos = displayKey(key, pos);
state = ATSIGN;
} else {
invalidFormat();
}
break;
case VALUE2:
// XX@
if(validAtSign(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = ATSIGN;
} else {
invalidFormat();
}
break;
case ATSIGN:
// X@H, HH@H
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = HOUR1;
} else {
invalidFormat();
}
break;
case HOUR1:
// X@HH, XX@HH
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = HOUR2;
} else if (validColon(key)) {
// X@HH:, XX@H:
pos = displayKey(key, pos);
state = COLON;
} else {
invalidFormat();
}
break;
case HOUR2:
// X@HH:, XX@HH:
if (validColon(key)) {
// X@HH:, XX@H:
pos = displayKey(key, pos);
state = COLON;
} else {
invalidFormat();
}
break;
case COLON:
// X@HH:M, XX@HH:M
if(validKeys0to5(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = MIN1;
} else {
invalidFormat();
}
break;
case MIN1:
// X@HH:MM, XX@HH:MM
if(validKeys0to9(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = MIN2;
} else {
invalidFormat();
}
break;
case MIN2:
// X@HH:MM#, XX@HH:MM#
if(validPoundSign(key)) {
pos = displayKey(key, pos);
storeKey(key);
state = EXECUTE;
} else {
invalidFormat();
}
break;
case CLEAR:
clearAll();
break;
default:
break;
} // switch
#ifdef SERIAL_DEBUG_ENABLED
Serial.print("Key:");
Serial.print(key);
Serial.print(" Idx:");
Serial.println(index);
#endif
// EXECUTE
if(state == EXECUTE) {
executeCmd();
clearAll();
}
} // if
} // loop
// Clears resets everything
void clearAll(void) {
buffer[0] = '\0';
index=0;
pos=0;
state = INITIAL;
clearDisplay();
} // clearAll()
// Clear the Cmd and execute area of the display
void clearDisplay(void) {
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
} // clearDisplay()
// Display format error
void invalidFormat(void) {
lcd.setCursor(0,3);
lcd.print("Invalid Format:");
delay(1000);
lcd.setCursor(0,3);
lcd.print("XX@HH:MM# ");
delay(1000);
lcd.setCursor(0,3);
lcd.print(" ");
} // invalidFormat()
// executeCmd
void executeCmd(void) {
lcd.setCursor(0,3);
lcd.print("Command Accepted");
delay(1000);
clearDisplay();
}
// Display key on Cmd line
int displayKey(char key, int pos) {
lcd.setCursor(pos,2);
lcd.print(key);
pos++;
return(pos);
} // displayKey()
// Store key in buffer
void storeKey(char key) {
buffer[index] = key;
index++;
} // storeKey()
// Clear buffer
void clearBuffer(void) {
buffer[0] = '\0';
index=0;
} // clearBuffer()
// Display state name
void displayState(int state) {
#ifdef SERIAL_DEBUG_ENABLED
char stateName[15];
switch(state) {
case INITIAL:
strcpy(stateName, "INITIAL");
break;
case VALUE1:
strcpy(stateName, "VALUE1");
break;
case VALUE2:
strcpy(stateName, "VALUE2");
break;
case ATSIGN:
strcpy(stateName, "ATSIGN");
break;
case HOUR1:
strcpy(stateName, "HOUR1");
break;
case HOUR2:
strcpy(stateName, "HOUR2");
break;
case COLON:
strcpy(stateName, "COLON");
break;
case MIN1:
strcpy(stateName, "MIN1");
break;
case MIN2:
strcpy(stateName, "MIN2");
break;
case EXECUTE:
strcpy(stateName, "EXECUTE");
break;
default:
strcpy(stateName, "UNKNOWN");
break;
} // switch
Serial.print("State:");
Serial.println(stateName);
#endif
} // displayState()
// Validate keys: : (colon)
// return true if valid, else false
int validColon(int key) {
int valid=0;
switch(key) {
case ':':
valid=1;
break;
}
return valid;
} // validColon()
// Validate keys: @ (at sign)
// return true if valid, else false
int validAtSign(int key) {
int valid=0;
switch(key) {
case '@':
valid=1;
break;
}
return valid;
} // validAtSign()
// Validate keys: # (pound sign)
// return true if valid, else false
int validPoundSign(int key) {
int valid=0;
switch(key) {
case '#':
valid=1;
break;
}
return valid;
}
// Validate keys: 0-9
// return true if valid, else false
int validKeys0to9(int key) {
int valid=0;
switch(key) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
valid=1;
break;
}
return valid;
} // validKey0to9()
// Validate keys: 0-5
// return true if valid, else false
int validKeys0to5(int key) {
int valid=0;
switch(key) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
valid=1;
break;
}
return valid;
} // validKey0to5()
// END OF FILE