-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ino
More file actions
421 lines (392 loc) · 11 KB
/
commands.ino
File metadata and controls
421 lines (392 loc) · 11 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* NAME: commands.ino
*
* WHAT:
* Commands for serial command line I/F.
*
* SPECIAL CONSIDERATIONS:
* None
*
* AUTHOR:
* D.L. Karmann
*
* MODIFIED:
*
*/
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int32_t Input_Value = 0;
// local function prototypes
int8_t Cmd_help(int8_t argc, char * argv[]);
int8_t Cmd_led(int8_t argc, char * argv[]);
int8_t Cmd_show(int8_t argc, char * argv[]);
int8_t Cmd_input(int8_t argc, char * argv[]);
int8_t Cmd_errs(int8_t argc, char * argv[]);
int8_t Cmd_verb(int8_t argc, char * argv[]);
//*****************************************************************************
//
// These are the command names and brief descriptions.
//
// To add a menu item: (to keep all items in Flash)
// 1) add the command string to the 'MenuCmd#' item
// 2) add the command help string to the 'MenuHelp#' item
// 3) add the function prototype for the command's function above
// 4) add the 'MenuCmd#', function's name, and 'MenuHelp#' to the 'g_sCmdTable[]' array
// 5) add the function for processing the command to this file
//
//*****************************************************************************
// menu items individual command name strings
const char MenuCmdHelp1[] PROGMEM = "help";
const char MenuCmdHelp2[] PROGMEM = "h";
const char MenuCmdHelp3[] PROGMEM = "?";
const char MenuCmdLed[] PROGMEM = "led";
const char MenuCmdShow[] PROGMEM = "show";
const char MenuCmdInput[] PROGMEM = "input";
const char MenuCmdErrs[] PROGMEM = "errs";
const char MenuCmdVerb[] PROGMEM = "verb";
// menu items individual command help strings
const char MenuHelp1[] PROGMEM = " [<cls>] : Display list of commands (clear screen)";
const char MenuHelp2[] PROGMEM = " : alias for help";
const char MenuHelpLed[] PROGMEM = " [<on | off | hb>] : Show/control the LED";
const char MenuHelpShow[] PROGMEM = " [params] : Show command line parameters";
const char MenuHelpInput[] PROGMEM = " [vals] : Show/set command line numeric value";
const char MenuHelpErrs[] PROGMEM = " errnum : Check custom error responses";
const char MenuHelpVerb[] PROGMEM = " [<on | off>] : Show/set verbose error responses flag";
//*****************************************************************************
//
// This is the table that holds the command names, implementing functions,
// and brief description. (Required by the 'CommandLine' command processor.)
//
//*****************************************************************************
const tCmdLineEntry g_sCmdTable[] PROGMEM =
{
// command function help info
{ MenuCmdHelp1, Cmd_help, MenuHelp1 },
{ MenuCmdHelp2, Cmd_help, MenuHelp2 },
{ MenuCmdHelp3, Cmd_help, MenuHelp2 },
{ MenuCmdLed, Cmd_led, MenuHelpLed },
{ MenuCmdShow, Cmd_show, MenuHelpShow },
{ MenuCmdInput, Cmd_input, MenuHelpInput },
{ MenuCmdErrs, Cmd_errs, MenuHelpErrs },
{ MenuCmdVerb, Cmd_verb, MenuHelpVerb },
{ 0, 0, 0 } // end of commands
};
/*
* NAME:
* int8_t Cmd_led(int8_t argc, char * argv[])
*
* PARAMETERS:
* int8_t argc = number of command line arguments for the command
* char * argv[] = pointer to array of parameters associated with the command
*
* WHAT:
* Implements the "led" command to query status of and turn the on-board LED on or
* off or heartbeat.
*
* RETURN VALUES:
* int8_t = 0 = command successfully processed
*
* SPECIAL CONSIDERATIONS:
* None.
*/
int8_t Cmd_led(int8_t argc, char * argv[])
{
if (argc > 2)
{
return CMDLINE_TOO_MANY_ARGS;
}
else if (argc > 1) // has a command argument
{
if (strcmp_P(argv[ARG1], PSTR("on")) == 0)
{
LED_on();
LedState = LED_ON;
}
else if (strcmp_P(argv[ARG1], PSTR("off")) == 0)
{
LED_off();
LedState = LED_OFF;
}
else if (strcmp_P(argv[ARG1], PSTR("hb")) == 0)
{
LedState = LED_HB;
}
else // unknown/invalid argument
{
return CMDLINE_BAD_CMD;
}
}
Serial.print(F("On-Board LED: "));
switch (LedState)
{
case LED_ON:
Serial.println(F("on"));
break;
case LED_OFF:
Serial.println(F("off"));
break;
case LED_HB:
Serial.println(F("HB"));
break;
}
// Return success.
return 0;
}
/*
* NAME:
* int8_t Cmd_show(int8_t argc, char * argv[])
*
* PARAMETERS:
* int8_t argc = number of command line arguments for the command
* char * argv[] = pointer to array of parameters associated with the command
*
* WHAT:
* Implements the "show" command to to show example command line items.
*
* RETURN VALUES:
* int8_t = 0 = command successfully processed
*
* SPECIAL CONSIDERATIONS:
* None.
*/
int8_t Cmd_show(int8_t argc, char * argv[])
{
if (argc > CMDLINE_MAX_ARGS)
{
return CMDLINE_TOO_MANY_ARGS;
}
else
{
Serial.println(F("Command Line: "));
for (uint8_t i = 0; i < argc; ++i)
{
// show the parameter
switch (i)
{
case CMD:
Serial.print(F(" Cmd: "));
break;
case ARG1:
Serial.print(F(" Arg1: "));
break;
case ARG2:
Serial.print(F(" Arg2: "));
break;
case ARG3:
Serial.print(F(" Arg3: "));
break;
case ARG4:
Serial.print(F(" Arg4: "));
break;
case ARG5:
Serial.print(F(" Arg5: "));
break;
case ARG6:
Serial.print(F(" Arg6: "));
break;
case ARG7:
Serial.print(F(" Arg7: "));
break;
case ARG8:
Serial.print(F(" Arg8: "));
break;
}
Serial.println(argv[i]);
}
}
// Return success.
return 0;
}
/*
* NAME:
* int8_t Cmd_input(int8_t argc, char * argv[])
*
* PARAMETERS:
* int8_t argc = number of command line arguments for the command
* char * argv[] = pointer to array of parameters associated with the command
*
* WHAT:
* Implements the "input" command to show/set the example numeric input values.
*
* RETURN VALUES:
* int8_t = 0 = command successfully processed
*
* SPECIAL CONSIDERATIONS:
* None.
*/
int8_t Cmd_input(int8_t argc, char * argv[])
{
int32_t val;
int8_t paramtype;
if (argc > CMDLINE_MAX_ARGS)
{
return CMDLINE_TOO_MANY_ARGS;
}
else if (argc > 1)
{
// get the input value
paramtype = CmdLine.ParseParam(argv[ARG1], &val);
if ((paramtype == BADPARAM) || (paramtype == STRVAL) ||
(val < -1000000) || (val > 1000000))
{
return CMDLINE_INVALID_ARG;
}
if (paramtype == DECVAL)
{
Serial.print(F("Dec:"));
}
else if (paramtype == HEXVAL)
{
Serial.print(F("Hex:"));
}
Input_Value = val;
}
Serial.print(F("Input Value: "));
Serial.println(Input_Value);
// Return success.
return 0;
}
/*
* NAME:
* int8_t Cmd_errs(int8_t argc, char * argv[])
*
* PARAMETERS:
* int8_t argc = number of command line arguments for the command
* char * argv[] = pointer to array of parameters associated with the command
*
* WHAT:
* Implements the "errs" command to check custom error responses.
*
* RETURN VALUES:
* int8_t = 0 = command successfully processed
*
* SPECIAL CONSIDERATIONS:
* None.
*/
int8_t Cmd_errs(int8_t argc, char * argv[])
{
int32_t val;
int8_t paramtype;
if (argc > CMDLINE_MAX_ARGS)
{
return CMDLINE_TOO_MANY_ARGS;
}
else if (argc == 2)
{
// get the input value
paramtype = CmdLine.ParseParam(argv[ARG1], &val);
if ((paramtype == BADPARAM) || (paramtype == STRVAL) ||
(val < -127) || (val > 127))
{
return CMDLINE_INVALID_ARG;
}
switch (val)
{
case CMDLINE_BAD_CMD: // -1
case CMDLINE_TOO_MANY_ARGS: // -2
case CMDLINE_TOO_FEW_ARGS: // -3
case CMDLINE_INVALID_ARG: // -4
default: // all else
return (int8_t)val;
break;
}
}
else
{
return CMDLINE_TOO_FEW_ARGS;
}
// Return success.
return 0;
}
/*
* NAME:
* int8_t Cmd_verb(int8_t argc, char * argv[])
*
* PARAMETERS:
* int8_t argc = number of command line arguments for the command
* char * argv[] = pointer to array of parameters associated with the command
*
* WHAT:
* Implements the "verb" command to query status of and set verbose error responses enable.
*
* RETURN VALUES:
* int8_t = 0 = command successfully processed
*
* SPECIAL CONSIDERATIONS:
* None.
*/
int8_t Cmd_verb(int8_t argc, char * argv[])
{
if (argc > 2)
{
return CMDLINE_TOO_MANY_ARGS;
}
else if (argc > 1) // has a command argument
{
if (strcmp_P(argv[ARG1], PSTR("on")) == 0)
{
VerboseErrsEnabled = true;
}
else if (strcmp_P(argv[ARG1], PSTR("off")) == 0)
{
VerboseErrsEnabled = false;
}
else // unknown/invalid argument
{
return CMDLINE_BAD_CMD;
}
}
Serial.print(F("Verbose Error Responses: "));
if (VerboseErrsEnabled)
{
Serial.println(F("on"));
}
else
{
Serial.println(F("off"));
}
// Return success.
return 0;
}
/*
* NAME:
* int8_t Cmd_help(int8_t argc, char * argv[])
*
* PARAMETERS:
* int8_t argc = number of command line arguments for the command
* char * argv[] = pointer to array of parameters associated with the command
*
* WHAT:
* Implements the "help" command to display a simple list of the available
* commands with a brief description of each command.
*
* One optional parameter supported.
* <cls> = clears the output screen using ANSI escape sequence
*
* RETURN VALUES:
* int8_t = 0 = command successfully processed
*
* SPECIAL CONSIDERATIONS:
* None.
*/
int8_t Cmd_help(int8_t argc, char * argv[])
{
if (argc > 1)
{
if (strcmp_P(argv[ARG1], PSTR("cls")) == 0)
{
Serial.println(F(CLS_HOME));
return 0;
}
}
// Print some header text.
Serial.println("");
Serial.println(TITLE_MSG);
Serial.println(F("Available commands"));
Serial.println(F("------------------"));
CmdLine.ShowCommands(); // show commands menu with help information
// Return success.
return 0;
}