-
Notifications
You must be signed in to change notification settings - Fork 49
/
Cmd.cpp
executable file
·233 lines (199 loc) · 7.23 KB
/
Cmd.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
/*******************************************************************
Copyright (C) 2009 FreakLabs
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Originally written by Christopher Wang aka Akiba.
Please post support questions to the FreakLabs forum.
*******************************************************************/
/*!
\file Cmd.c
This implements a simple command line interface for the Arduino so that
its possible to execute individual functions within the sketch.
*/
/**************************************************************************/
#include <avr/pgmspace.h>
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "HardwareSerial.h"
#include "Cmd.h"
// command line message buffer and pointer
static uint8_t msg[MAX_MSG_SIZE];
static uint8_t *msg_ptr;
// linked list for command table
static cmd_t *cmd_tbl_list, *cmd_tbl;
// text strings for command prompt (stored in flash)
const char cmd_banner[] PROGMEM = "*************** CMD *******************";
const char cmd_prompt[] PROGMEM = "CMD >> ";
const char cmd_unrecog[] PROGMEM = "CMD: Command not recognized.";
/**************************************************************************/
/*!
Generate the main command prompt
*/
/**************************************************************************/
void cmd_display()
{
char buf[50];
Serial.println();
strcpy_P(buf, cmd_banner);
Serial.println(buf);
strcpy_P(buf, cmd_prompt);
Serial.print(buf);
}
/**************************************************************************/
/*!
Parse the command line. This function tokenizes the command input, then
searches for the command table entry associated with the commmand. Once found,
it will jump to the corresponding function.
*/
/**************************************************************************/
void cmd_parse(char *cmd)
{
uint8_t argc, i = 0;
char *argv[30];
char buf[50];
cmd_t *cmd_entry;
fflush(stdout);
// parse the command line statement and break it up into space-delimited
// strings. the array of strings will be saved in the argv array.
argv[i] = strtok(cmd, " ");
do
{
argv[++i] = strtok(NULL, " ");
} while ((i < 30) && (argv[i] != NULL));
// save off the number of arguments for the particular command.
argc = i;
// parse the command table for valid command. used argv[0] which is the
// actual command name typed in at the prompt
for (cmd_entry = cmd_tbl; cmd_entry != NULL; cmd_entry = cmd_entry->next)
{
if (!strcmp(argv[0], cmd_entry->cmd))
{
cmd_entry->func(argc, argv);
cmd_display();
return;
}
}
// command not recognized. print message and re-generate prompt.
strcpy_P(buf, cmd_unrecog);
Serial.println(buf);
cmd_display();
}
/**************************************************************************/
/*!
This function processes the individual characters typed into the command
prompt. It saves them off into the message buffer unless its a "backspace"
or "enter" key.
*/
/**************************************************************************/
void cmd_handler()
{
char c = Serial.read();
switch (c)
{
case '\r':
// terminate the msg and reset the msg ptr. then send
// it to the handler for processing.
*msg_ptr = '\0';
Serial.print("\r\n");
cmd_parse((char *)msg);
msg_ptr = msg;
break;
case '\b':
// backspace
Serial.print(c);
if (msg_ptr > msg)
{
msg_ptr--;
}
break;
default:
// normal character entered. add it to the buffer
Serial.print(c);
*msg_ptr++ = c;
break;
}
}
/**************************************************************************/
/*!
This function should be set inside the main loop. It needs to be called
constantly to check if there is any available input at the command prompt.
*/
/**************************************************************************/
void cmdPoll()
{
while (Serial.available())
{
cmd_handler();
}
}
/**************************************************************************/
/*!
Initialize the command line interface. This sets the terminal speed and
and initializes things.
*/
/**************************************************************************/
void cmdInit(uint32_t speed)
{
// init the msg ptr
msg_ptr = msg;
// init the command table
cmd_tbl_list = NULL;
// set the serial speed
Serial.begin(speed);
}
/**************************************************************************/
/*!
Add a command to the command table. The commands should be added in
at the setup() portion of the sketch.
*/
/**************************************************************************/
void cmdAdd(char *name, void (*func)(int argc, char **argv))
{
// alloc memory for command struct
cmd_tbl = (cmd_t *)malloc(sizeof(cmd_t));
// alloc memory for command name
char *cmd_name = (char *)malloc(strlen(name)+1);
// copy command name
strcpy(cmd_name, name);
// terminate the command name
cmd_name[strlen(name)] = '\0';
// fill out structure
cmd_tbl->cmd = cmd_name;
cmd_tbl->func = func;
cmd_tbl->next = cmd_tbl_list;
cmd_tbl_list = cmd_tbl;
}
/**************************************************************************/
/*!
Convert a string to a number. The base must be specified, ie: "32" is a
different value in base 10 (decimal) and base 16 (hexadecimal).
*/
/**************************************************************************/
uint32_t cmdStr2Num(char *str, uint8_t base)
{
return strtol(str, NULL, base);
}