-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ino
More file actions
306 lines (285 loc) · 8.23 KB
/
commands.ino
File metadata and controls
306 lines (285 loc) · 8.23 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
/*
* 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>
uint32_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[]);
//*****************************************************************************
//
// 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";
// 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";
//*****************************************************************************
//
// 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 },
{ 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;
case ARG9:
Serial.print(F(" Arg9: "));
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[])
{
uint32_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 < 1) || (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_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;
}