A Command Interpreter with support for used defined commands, subsystems, macros, help and parameter parsing.

Committer:
wvd_vegt
Date:
Thu Mar 24 21:40:58 2011 +0000
Revision:
16:ec8147828286
Parent:
15:d9680ef7b3f8
Child:
17:7e6a723d65de
Fixed some index/id mix-ups.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wvd_vegt 3:abbf43fab7d5 1 /* mbed Command Interpreter Library
wvd_vegt 3:abbf43fab7d5 2 * Copyright (c) 2011 wvd_vegt
wvd_vegt 3:abbf43fab7d5 3 *
wvd_vegt 3:abbf43fab7d5 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wvd_vegt 3:abbf43fab7d5 5 * of this software and associated documentation files (the "Software"), to deal
wvd_vegt 3:abbf43fab7d5 6 * in the Software without restriction, including without limitation the rights
wvd_vegt 3:abbf43fab7d5 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wvd_vegt 3:abbf43fab7d5 8 * copies of the Software, and to permit persons to whom the Software is
wvd_vegt 3:abbf43fab7d5 9 * furnished to do so, subject to the following conditions:
wvd_vegt 3:abbf43fab7d5 10 *
wvd_vegt 3:abbf43fab7d5 11 * The above copyright notice and this permission notice shall be included in
wvd_vegt 3:abbf43fab7d5 12 * all copies or substantial portions of the Software.
wvd_vegt 3:abbf43fab7d5 13 *
wvd_vegt 3:abbf43fab7d5 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wvd_vegt 3:abbf43fab7d5 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wvd_vegt 3:abbf43fab7d5 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wvd_vegt 3:abbf43fab7d5 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wvd_vegt 3:abbf43fab7d5 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wvd_vegt 3:abbf43fab7d5 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wvd_vegt 3:abbf43fab7d5 20 * THE SOFTWARE.
wvd_vegt 3:abbf43fab7d5 21 */
wvd_vegt 3:abbf43fab7d5 22
wvd_vegt 3:abbf43fab7d5 23 #ifndef MBED_CMDB_H
wvd_vegt 3:abbf43fab7d5 24 #define MBED_CMDB_H
wvd_vegt 3:abbf43fab7d5 25
wvd_vegt 3:abbf43fab7d5 26 #include "mbed.h"
wvd_vegt 3:abbf43fab7d5 27
wvd_vegt 3:abbf43fab7d5 28 #include <vector>
wvd_vegt 7:269c2445b8f5 29 #include <limits>
wvd_vegt 3:abbf43fab7d5 30
wvd_vegt 7:269c2445b8f5 31 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 32
wvd_vegt 7:269c2445b8f5 33 /** Max size of an Ansi escape code.
wvd_vegt 7:269c2445b8f5 34 */
wvd_vegt 3:abbf43fab7d5 35 #define MAX_ESC_LEN 5
wvd_vegt 3:abbf43fab7d5 36
wvd_vegt 7:269c2445b8f5 37 /** Max (strlen) of a Param.
wvd_vegt 7:269c2445b8f5 38 */
wvd_vegt 3:abbf43fab7d5 39 #define MAX_PARM_LEN 32
wvd_vegt 3:abbf43fab7d5 40
wvd_vegt 7:269c2445b8f5 41 /** Max eight parms.
wvd_vegt 7:269c2445b8f5 42 */
wvd_vegt 7:269c2445b8f5 43 #define MAX_ARGS 8
wvd_vegt 3:abbf43fab7d5 44
wvd_vegt 7:269c2445b8f5 45 /** Max 132 characters commandline.
wvd_vegt 7:269c2445b8f5 46 */
wvd_vegt 3:abbf43fab7d5 47 #define MAX_CMD_LEN 132
wvd_vegt 3:abbf43fab7d5 48
wvd_vegt 7:269c2445b8f5 49 /** 'Show' hidden subsystems and commands.
wvd_vegt 7:269c2445b8f5 50 */
wvd_vegt 3:abbf43fab7d5 51 #define SHOWHIDDEN
wvd_vegt 3:abbf43fab7d5 52
wvd_vegt 7:269c2445b8f5 53 /** Enable macro commands.
wvd_vegt 7:269c2445b8f5 54 */
wvd_vegt 7:269c2445b8f5 55 #define ENABLEMACROS
wvd_vegt 7:269c2445b8f5 56
wvd_vegt 7:269c2445b8f5 57 /** Enable statemachine.
wvd_vegt 7:269c2445b8f5 58 *
wvd_vegt 7:269c2445b8f5 59 * Used to implement a series of commands running at power-up.
wvd_vegt 7:269c2445b8f5 60 *
wvd_vegt 7:269c2445b8f5 61 * @note Not Implemented!
wvd_vegt 7:269c2445b8f5 62 */
wvd_vegt 7:269c2445b8f5 63 #undef STATEMACHINE
wvd_vegt 3:abbf43fab7d5 64
wvd_vegt 7:269c2445b8f5 65 /** Enable subsystem prompts.
wvd_vegt 7:269c2445b8f5 66 *
wvd_vegt 7:269c2445b8f5 67 * When defined, prompts will reflect the SubSystem.
wvd_vegt 7:269c2445b8f5 68 */
wvd_vegt 8:83edd6addbd5 69 #define SUBSYSTEMPROMPTS
wvd_vegt 7:269c2445b8f5 70
wvd_vegt 7:269c2445b8f5 71 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 72
wvd_vegt 7:269c2445b8f5 73 /** 8 bit limits.
wvd_vegt 7:269c2445b8f5 74 *
wvd_vegt 7:269c2445b8f5 75 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 7:269c2445b8f5 76 */
wvd_vegt 7:269c2445b8f5 77 #define MIN_BYTE std::numeric_limits<signed char>::min()
wvd_vegt 7:269c2445b8f5 78
wvd_vegt 7:269c2445b8f5 79 /** 8 bit limits.
wvd_vegt 7:269c2445b8f5 80 *
wvd_vegt 7:269c2445b8f5 81 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 7:269c2445b8f5 82 */
wvd_vegt 7:269c2445b8f5 83 #define MAX_BYTE std::numeric_limits<signed char>::max()
wvd_vegt 3:abbf43fab7d5 84
wvd_vegt 7:269c2445b8f5 85 /** 16 bit limits.
wvd_vegt 7:269c2445b8f5 86 *
wvd_vegt 7:269c2445b8f5 87 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 7:269c2445b8f5 88 */
wvd_vegt 7:269c2445b8f5 89 #define MIN_SHORT std::numeric_limits<short int>::min()
wvd_vegt 7:269c2445b8f5 90
wvd_vegt 7:269c2445b8f5 91 /** 16 bit limits.
wvd_vegt 7:269c2445b8f5 92 *
wvd_vegt 7:269c2445b8f5 93 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 7:269c2445b8f5 94 */
wvd_vegt 7:269c2445b8f5 95 #define MAX_SHORT std::numeric_limits<short int>::max()
wvd_vegt 3:abbf43fab7d5 96
wvd_vegt 7:269c2445b8f5 97 /** 32 bit limits.
wvd_vegt 7:269c2445b8f5 98 *
wvd_vegt 7:269c2445b8f5 99 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 7:269c2445b8f5 100 */
wvd_vegt 7:269c2445b8f5 101 #define MIN_INT std::numeric_limits<int>::min()
wvd_vegt 7:269c2445b8f5 102 #define MAX_INT std::numeric_limits<int>::max()
wvd_vegt 3:abbf43fab7d5 103
wvd_vegt 7:269c2445b8f5 104 /** 32 bit limits.
wvd_vegt 7:269c2445b8f5 105 *
wvd_vegt 7:269c2445b8f5 106 * @see http://www.daniweb.com/forums/thread18963.html
wvd_vegt 7:269c2445b8f5 107 */
wvd_vegt 7:269c2445b8f5 108 #define MIN_LONG std::numeric_limits<long>::min()
wvd_vegt 7:269c2445b8f5 109 #define MAX_LONG std::numeric_limits<long>::max()
wvd_vegt 7:269c2445b8f5 110
wvd_vegt 7:269c2445b8f5 111 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 112
wvd_vegt 7:269c2445b8f5 113 /** Description of a command.
wvd_vegt 7:269c2445b8f5 114 */
wvd_vegt 7:269c2445b8f5 115 struct cmd {
wvd_vegt 6:76b033971c3c 116 public:
wvd_vegt 3:abbf43fab7d5 117 char *cmdstr;
wvd_vegt 3:abbf43fab7d5 118 int subs;
wvd_vegt 7:269c2445b8f5 119 int cid;
wvd_vegt 3:abbf43fab7d5 120 char *parms;
wvd_vegt 3:abbf43fab7d5 121 char *cmddescr;
wvd_vegt 3:abbf43fab7d5 122 char *parmdescr;
wvd_vegt 5:68d3a351c3ea 123
wvd_vegt 10:05fea1780005 124 /** Default Constructor.
wvd_vegt 10:05fea1780005 125 */
wvd_vegt 10:05fea1780005 126 cmd() {
wvd_vegt 10:05fea1780005 127 }
wvd_vegt 10:05fea1780005 128
wvd_vegt 7:269c2445b8f5 129 /** Command Constructor.
wvd_vegt 7:269c2445b8f5 130 *
wvd_vegt 7:269c2445b8f5 131 * @parm _cmdstr the command, not case sensitive.
wvd_vegt 7:269c2445b8f5 132 * @parm _subs subsystem id
wvd_vegt 7:269c2445b8f5 133 * @parm _cid the command id that will be passed to the dispatcher.
wvd_vegt 7:269c2445b8f5 134 * @parm _parms a scanf alike pattern.
wvd_vegt 7:269c2445b8f5 135 * @parm _cmddescr command description.
wvd_vegt 7:269c2445b8f5 136 * @parm _parmdescr parameter description.
wvd_vegt 7:269c2445b8f5 137 */
wvd_vegt 7:269c2445b8f5 138 cmd(char *_cmdstr, int _subs, int _cid, char *_parms, char *_cmddescr, char *_parmdescr = "") {
wvd_vegt 6:76b033971c3c 139 cmdstr = (char*)malloc(strlen(_cmdstr)+1);
wvd_vegt 6:76b033971c3c 140 strcpy(cmdstr,_cmdstr);
wvd_vegt 6:76b033971c3c 141
wvd_vegt 6:76b033971c3c 142 subs = _subs;
wvd_vegt 7:269c2445b8f5 143 cid = _cid;
wvd_vegt 5:68d3a351c3ea 144
wvd_vegt 6:76b033971c3c 145 parms = (char*)malloc(strlen(_parms)+1);
wvd_vegt 5:68d3a351c3ea 146 strcpy(parms,_parms);
wvd_vegt 6:76b033971c3c 147
wvd_vegt 6:76b033971c3c 148 cmddescr = (char*)malloc(strlen(_cmddescr)+1);
wvd_vegt 5:68d3a351c3ea 149 strcpy(cmddescr,_cmddescr);
wvd_vegt 6:76b033971c3c 150
wvd_vegt 6:76b033971c3c 151 parmdescr = (char*)malloc(strlen(_parmdescr)+1);
wvd_vegt 5:68d3a351c3ea 152 strcpy(parmdescr,_parmdescr);
wvd_vegt 16:ec8147828286 153
wvd_vegt 16:ec8147828286 154 //printf("%d:%d\r\n", subs, cid);
wvd_vegt 5:68d3a351c3ea 155 }
wvd_vegt 3:abbf43fab7d5 156 };
wvd_vegt 3:abbf43fab7d5 157
wvd_vegt 7:269c2445b8f5 158 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 159
wvd_vegt 7:269c2445b8f5 160 /** Cr.
wvd_vegt 7:269c2445b8f5 161 */
wvd_vegt 7:269c2445b8f5 162 const char cr = '\r';
wvd_vegt 5:68d3a351c3ea 163
wvd_vegt 7:269c2445b8f5 164 /** Lf.
wvd_vegt 7:269c2445b8f5 165 */
wvd_vegt 5:68d3a351c3ea 166 const char lf = '\n';
wvd_vegt 7:269c2445b8f5 167
wvd_vegt 7:269c2445b8f5 168 /** Bell.
wvd_vegt 7:269c2445b8f5 169 */
wvd_vegt 5:68d3a351c3ea 170 const char bell = '\7';
wvd_vegt 7:269c2445b8f5 171
wvd_vegt 7:269c2445b8f5 172 /** Escape.
wvd_vegt 7:269c2445b8f5 173 */
wvd_vegt 5:68d3a351c3ea 174 const char esc = '\033';
wvd_vegt 7:269c2445b8f5 175
wvd_vegt 7:269c2445b8f5 176 /** Space.
wvd_vegt 7:269c2445b8f5 177 */
wvd_vegt 5:68d3a351c3ea 178 const char sp = ' ';
wvd_vegt 5:68d3a351c3ea 179
wvd_vegt 7:269c2445b8f5 180 /** CrLf.
wvd_vegt 7:269c2445b8f5 181 */
wvd_vegt 7:269c2445b8f5 182 const char crlf[] = "\r\n";
wvd_vegt 7:269c2445b8f5 183
wvd_vegt 7:269c2445b8f5 184 /** Backspace that 'tries' to wipe the last character.
wvd_vegt 7:269c2445b8f5 185 */
wvd_vegt 7:269c2445b8f5 186 const char bs[] = "\b \b";
wvd_vegt 5:68d3a351c3ea 187
wvd_vegt 7:269c2445b8f5 188 /** VT100 Bold Command.
wvd_vegt 7:269c2445b8f5 189 */
wvd_vegt 7:269c2445b8f5 190 const char boldon[] = "\033[1m";
wvd_vegt 7:269c2445b8f5 191
wvd_vegt 7:269c2445b8f5 192 /** VT100 Normal Command.
wvd_vegt 7:269c2445b8f5 193 */
wvd_vegt 7:269c2445b8f5 194 const char boldoff[] = "\033[0m";
wvd_vegt 5:68d3a351c3ea 195
wvd_vegt 7:269c2445b8f5 196 /** VT100 Cls Command.
wvd_vegt 7:269c2445b8f5 197 */
wvd_vegt 7:269c2445b8f5 198 const char cls[] = "\033[2J";
wvd_vegt 7:269c2445b8f5 199
wvd_vegt 7:269c2445b8f5 200 /** VT100 Home Command.
wvd_vegt 7:269c2445b8f5 201 */
wvd_vegt 7:269c2445b8f5 202 const char home[] = "\033[H";
wvd_vegt 5:68d3a351c3ea 203
wvd_vegt 7:269c2445b8f5 204 /** The default command prompt.
wvd_vegt 7:269c2445b8f5 205 */
wvd_vegt 7:269c2445b8f5 206 const char PROMPT[] = "CMD>";
wvd_vegt 7:269c2445b8f5 207
wvd_vegt 7:269c2445b8f5 208 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 209
wvd_vegt 7:269c2445b8f5 210 /** Subsystem Id for a Subsystem.
wvd_vegt 7:269c2445b8f5 211 */
wvd_vegt 7:269c2445b8f5 212 #define SUBSYSTEM -1
wvd_vegt 3:abbf43fab7d5 213
wvd_vegt 7:269c2445b8f5 214 /** Subsystem Id for a Global Command (always available).
wvd_vegt 7:269c2445b8f5 215 */
wvd_vegt 3:abbf43fab7d5 216 #define GLOBALCMD -2
wvd_vegt 7:269c2445b8f5 217
wvd_vegt 7:269c2445b8f5 218 /** Subsystem Id for a Hidden Subsystem (ommitted from help).
wvd_vegt 7:269c2445b8f5 219 */
wvd_vegt 3:abbf43fab7d5 220 #define HIDDENSUB -3
wvd_vegt 3:abbf43fab7d5 221
wvd_vegt 9:dc9faec79298 222 /** Predefined Dump Command.
wvd_vegt 9:dc9faec79298 223 */
wvd_vegt 9:dc9faec79298 224 #define CID_COMMANDS 9989
wvd_vegt 9:dc9faec79298 225
wvd_vegt 7:269c2445b8f5 226 /** Predefined Boot Command.
wvd_vegt 7:269c2445b8f5 227 */
wvd_vegt 6:76b033971c3c 228 #define CID_BOOT 9990
wvd_vegt 7:269c2445b8f5 229
wvd_vegt 7:269c2445b8f5 230 /** Predefined Macro Command.
wvd_vegt 7:269c2445b8f5 231 *
wvd_vegt 7:269c2445b8f5 232 * This command will take a string with spaces replace by _ and cr replace by | for later replay with run.
wvd_vegt 7:269c2445b8f5 233 */
wvd_vegt 6:76b033971c3c 234 #define CID_MACRO 9991
wvd_vegt 7:269c2445b8f5 235
wvd_vegt 7:269c2445b8f5 236 /** Predefined Macro Command.
wvd_vegt 7:269c2445b8f5 237 *
wvd_vegt 7:269c2445b8f5 238 * This command replay a macro.
wvd_vegt 7:269c2445b8f5 239 */
wvd_vegt 6:76b033971c3c 240 #define CID_RUN 9992
wvd_vegt 7:269c2445b8f5 241
wvd_vegt 7:269c2445b8f5 242 /** Predefined Macro Command.
wvd_vegt 7:269c2445b8f5 243 *
wvd_vegt 7:269c2445b8f5 244 * This command print the current macro.
wvd_vegt 7:269c2445b8f5 245 */
wvd_vegt 6:76b033971c3c 246 #define CID_MACROS 9993
wvd_vegt 3:abbf43fab7d5 247
wvd_vegt 7:269c2445b8f5 248 /** Predefined Echo Command.
wvd_vegt 7:269c2445b8f5 249 *
wvd_vegt 7:269c2445b8f5 250 * This command turn echo on or off.
wvd_vegt 7:269c2445b8f5 251 */
wvd_vegt 6:76b033971c3c 252 #define CID_ECHO 9994
wvd_vegt 7:269c2445b8f5 253
wvd_vegt 7:269c2445b8f5 254 /** Predefined VT100 Bold Command.
wvd_vegt 7:269c2445b8f5 255 *
wvd_vegt 7:269c2445b8f5 256 * This command turn VT100 bold usage on or off.
wvd_vegt 7:269c2445b8f5 257 */
wvd_vegt 6:76b033971c3c 258 #define CID_BOLD 9995
wvd_vegt 7:269c2445b8f5 259
wvd_vegt 7:269c2445b8f5 260 /** Predefined VT100 Cls Command.
wvd_vegt 7:269c2445b8f5 261 *
wvd_vegt 7:269c2445b8f5 262 * This command will clear the screen.
wvd_vegt 7:269c2445b8f5 263 */
wvd_vegt 6:76b033971c3c 264 #define CID_CLS 9996
wvd_vegt 7:269c2445b8f5 265
wvd_vegt 7:269c2445b8f5 266 /** Predefined Idle Command.
wvd_vegt 7:269c2445b8f5 267 *
wvd_vegt 7:269c2445b8f5 268 * This command will return to the global command level, leaving the active subsystem.
wvd_vegt 7:269c2445b8f5 269 */
wvd_vegt 6:76b033971c3c 270 #define CID_IDLE 9997
wvd_vegt 7:269c2445b8f5 271
wvd_vegt 7:269c2445b8f5 272 /** Predefined Help Command.
wvd_vegt 7:269c2445b8f5 273 *
wvd_vegt 7:269c2445b8f5 274 * This command will either print all active command (without parameters) or a more detailed
wvd_vegt 7:269c2445b8f5 275 * help for a command passed as parameter.
wvd_vegt 7:269c2445b8f5 276 */
wvd_vegt 6:76b033971c3c 277 #define CID_HELP 9998
wvd_vegt 7:269c2445b8f5 278
wvd_vegt 7:269c2445b8f5 279 /** Predefided Semi Command.
wvd_vegt 7:269c2445b8f5 280 *
wvd_vegt 7:269c2445b8f5 281 * CID_LAST only functions as a special Commend Id to signal unknown commands.
wvd_vegt 7:269c2445b8f5 282 */
wvd_vegt 6:76b033971c3c 283 #define CID_LAST 9999
wvd_vegt 3:abbf43fab7d5 284
wvd_vegt 7:269c2445b8f5 285 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 286
wvd_vegt 7:269c2445b8f5 287 /** The Boot Command.
wvd_vegt 7:269c2445b8f5 288 *
wvd_vegt 9:dc9faec79298 289 * @note: this command can be used to list all commands in Windows ini file format for host processing.
wvd_vegt 9:dc9faec79298 290 *
wvd_vegt 7:269c2445b8f5 291 * Optional.
wvd_vegt 7:269c2445b8f5 292 */
wvd_vegt 9:dc9faec79298 293 const cmd COMMANDS("Commands",GLOBALCMD,CID_COMMANDS,"","Dump Commands");
wvd_vegt 9:dc9faec79298 294
wvd_vegt 9:dc9faec79298 295 /** The Boot Command.
wvd_vegt 9:dc9faec79298 296 *
wvd_vegt 9:dc9faec79298 297 * Optional.
wvd_vegt 9:dc9faec79298 298 */
wvd_vegt 9:dc9faec79298 299 const cmd BOOT("Boot",GLOBALCMD,CID_BOOT,"","Boot mBed");
wvd_vegt 3:abbf43fab7d5 300
wvd_vegt 7:269c2445b8f5 301 /** The Macro Command.
wvd_vegt 7:269c2445b8f5 302 *
wvd_vegt 7:269c2445b8f5 303 * Optional.
wvd_vegt 7:269c2445b8f5 304 */
wvd_vegt 7:269c2445b8f5 305 const cmd MACRO("Macro",GLOBALCMD,CID_MACRO,"%s","Define macro (sp->_, cr->|)","command(s)");
wvd_vegt 7:269c2445b8f5 306
wvd_vegt 7:269c2445b8f5 307 /** The Run Command.
wvd_vegt 7:269c2445b8f5 308 *
wvd_vegt 7:269c2445b8f5 309 * Optional.
wvd_vegt 7:269c2445b8f5 310 */
wvd_vegt 7:269c2445b8f5 311 const cmd RUN("Run",GLOBALCMD,CID_RUN,"","Run a macro");
wvd_vegt 7:269c2445b8f5 312
wvd_vegt 7:269c2445b8f5 313 /** The Macros Command.
wvd_vegt 7:269c2445b8f5 314 *
wvd_vegt 7:269c2445b8f5 315 * Optional.
wvd_vegt 7:269c2445b8f5 316 */
wvd_vegt 7:269c2445b8f5 317 const cmd MACROS("Macros",GLOBALCMD,CID_MACROS,"","List macro(s)");
wvd_vegt 3:abbf43fab7d5 318
wvd_vegt 7:269c2445b8f5 319 /** The Echo Command.
wvd_vegt 7:269c2445b8f5 320 *
wvd_vegt 7:269c2445b8f5 321 * Optional.
wvd_vegt 7:269c2445b8f5 322 */
wvd_vegt 7:269c2445b8f5 323 const cmd ECHO("Echo",GLOBALCMD,CID_ECHO,"%bu","Echo On|Off (1|0)","state");
wvd_vegt 7:269c2445b8f5 324
wvd_vegt 7:269c2445b8f5 325 /** The Bold Command.
wvd_vegt 7:269c2445b8f5 326 *
wvd_vegt 7:269c2445b8f5 327 * Optional.
wvd_vegt 7:269c2445b8f5 328 */
wvd_vegt 7:269c2445b8f5 329 const cmd BOLD("Bold",GLOBALCMD,CID_BOLD,"%bu","Bold On|Off (1|0)","state");
wvd_vegt 3:abbf43fab7d5 330
wvd_vegt 7:269c2445b8f5 331 /** The Cls Command.
wvd_vegt 7:269c2445b8f5 332 *
wvd_vegt 7:269c2445b8f5 333 * Optional.
wvd_vegt 7:269c2445b8f5 334 */
wvd_vegt 7:269c2445b8f5 335 const cmd CLS("Cls",GLOBALCMD,CID_CLS,"","Clears the terminal screen");
wvd_vegt 3:abbf43fab7d5 336
wvd_vegt 7:269c2445b8f5 337 /** The Idle Command.
wvd_vegt 7:269c2445b8f5 338 *
wvd_vegt 7:269c2445b8f5 339 * Mandatory if you use subsystems.
wvd_vegt 7:269c2445b8f5 340 */
wvd_vegt 7:269c2445b8f5 341 const cmd IDLE("Idle",GLOBALCMD,CID_IDLE,"","Deselect Subsystems");
wvd_vegt 3:abbf43fab7d5 342
wvd_vegt 7:269c2445b8f5 343 /** The Help Command.
wvd_vegt 7:269c2445b8f5 344 *
wvd_vegt 7:269c2445b8f5 345 * Mandatory.
wvd_vegt 7:269c2445b8f5 346 */
wvd_vegt 7:269c2445b8f5 347 const cmd HELP("Help",GLOBALCMD,CID_HELP,"%s","Help");
wvd_vegt 7:269c2445b8f5 348
wvd_vegt 7:269c2445b8f5 349 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 350
wvd_vegt 7:269c2445b8f5 351 /** We'll only define the 4 cursor codes at the moment.
wvd_vegt 7:269c2445b8f5 352 */
wvd_vegt 3:abbf43fab7d5 353 #define ESC_TBL_LEN 4
wvd_vegt 3:abbf43fab7d5 354
wvd_vegt 7:269c2445b8f5 355 /** Escape code definition struct.
wvd_vegt 7:269c2445b8f5 356 */
wvd_vegt 8:83edd6addbd5 357 struct esc {
wvd_vegt 3:abbf43fab7d5 358 char *escstr;
wvd_vegt 3:abbf43fab7d5 359 int id;
wvd_vegt 3:abbf43fab7d5 360 };
wvd_vegt 3:abbf43fab7d5 361
wvd_vegt 7:269c2445b8f5 362 /** The Escape Code Id's.
wvd_vegt 7:269c2445b8f5 363 */
wvd_vegt 5:68d3a351c3ea 364 enum {
wvd_vegt 3:abbf43fab7d5 365 EID_CURSOR_UP,
wvd_vegt 3:abbf43fab7d5 366 EID_CURSOR_DOWN,
wvd_vegt 3:abbf43fab7d5 367 EID_CURSOR_RIGHT,
wvd_vegt 3:abbf43fab7d5 368 EID_CURSOR_LEFT,
wvd_vegt 3:abbf43fab7d5 369 EID_LAST
wvd_vegt 3:abbf43fab7d5 370 };
wvd_vegt 3:abbf43fab7d5 371
wvd_vegt 7:269c2445b8f5 372 /** The Escape Codes Table.
wvd_vegt 7:269c2445b8f5 373 */
wvd_vegt 8:83edd6addbd5 374 const struct esc esc_tbl [ESC_TBL_LEN] = {
wvd_vegt 3:abbf43fab7d5 375 { "\033[A", EID_CURSOR_UP },
wvd_vegt 3:abbf43fab7d5 376 { "\033[B", EID_CURSOR_DOWN },
wvd_vegt 3:abbf43fab7d5 377 { "\033[C", EID_CURSOR_RIGHT },
wvd_vegt 3:abbf43fab7d5 378 { "\033[D", EID_CURSOR_LEFT },
wvd_vegt 3:abbf43fab7d5 379 };
wvd_vegt 3:abbf43fab7d5 380
wvd_vegt 7:269c2445b8f5 381 //------------------------------------------------------------------------------
wvd_vegt 6:76b033971c3c 382
wvd_vegt 7:269c2445b8f5 383 /** The Command Interpreter Version.
wvd_vegt 7:269c2445b8f5 384 */
wvd_vegt 16:ec8147828286 385 #define CMDB_VERSION 0.77
wvd_vegt 6:76b033971c3c 386
wvd_vegt 7:269c2445b8f5 387 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 388
wvd_vegt 15:d9680ef7b3f8 389 /** Command Interpreter class.
wvd_vegt 15:d9680ef7b3f8 390 *
wvd_vegt 15:d9680ef7b3f8 391 * Steps to take:
wvd_vegt 15:d9680ef7b3f8 392 *
wvd_vegt 15:d9680ef7b3f8 393 * 1) Create a std::vector<cmd> and fill it with at least
wvd_vegt 15:d9680ef7b3f8 394 * the mandatory commands IDLE and HELP.
wvd_vegt 15:d9680ef7b3f8 395 *
wvd_vegt 15:d9680ef7b3f8 396 * 2) Create an Cmdb class instance and pass it the vector,
wvd_vegt 15:d9680ef7b3f8 397 * a Serial port object like Serial serial(USBTX, USBRX);
wvd_vegt 15:d9680ef7b3f8 398 * and finally a command dispatcher function.
wvd_vegt 15:d9680ef7b3f8 399 *
wvd_vegt 15:d9680ef7b3f8 400 * 3) Feed the interpreter with characters received from your serial port.
wvd_vegt 15:d9680ef7b3f8 401 * Note: Cmdb self does not retrieve input it must be handed to it.
wvd_vegt 15:d9680ef7b3f8 402 * It implements basic members for checking/reading the serial port.
wvd_vegt 15:d9680ef7b3f8 403 *
wvd_vegt 15:d9680ef7b3f8 404 * 4) Handle commands added by the application by the Cid and parameters passed.
wvd_vegt 15:d9680ef7b3f8 405 *
wvd_vegt 15:d9680ef7b3f8 406 * Note: Predefined commands and all subsystems transitions are handled by the internal dispatcher.
wvd_vegt 15:d9680ef7b3f8 407 * So the passed dispatcher only has to handle user/application defined commands'.
wvd_vegt 15:d9680ef7b3f8 408 *
wvd_vegt 8:83edd6addbd5 409 * @see main.cpp for a demo.
wvd_vegt 3:abbf43fab7d5 410 */
wvd_vegt 3:abbf43fab7d5 411 class Cmdb {
wvd_vegt 3:abbf43fab7d5 412 public:
wvd_vegt 3:abbf43fab7d5 413 /** Create a Command Interpreter.
wvd_vegt 3:abbf43fab7d5 414 *
wvd_vegt 7:269c2445b8f5 415 * @see http://www.newty.de/fpt/fpt.html#chapter2 for function pointers.
wvd_vegt 7:269c2445b8f5 416 * @see http://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c
wvd_vegt 7:269c2445b8f5 417 * @see http://www.daniweb.com/forums/thread293338.html
wvd_vegt 6:76b033971c3c 418 *
wvd_vegt 3:abbf43fab7d5 419 * @param serial a Serial port used for communication.
wvd_vegt 3:abbf43fab7d5 420 * @param cmds a vector with the command table.
wvd_vegt 3:abbf43fab7d5 421 */
wvd_vegt 7:269c2445b8f5 422 Cmdb(const Serial& serial, const std::vector<cmd>& cmds, void (*callback)(Cmdb&,int) );
wvd_vegt 7:269c2445b8f5 423
wvd_vegt 7:269c2445b8f5 424 /** The version of the Command Interpreter.
wvd_vegt 7:269c2445b8f5 425 *
wvd_vegt 7:269c2445b8f5 426 * returns the version.
wvd_vegt 7:269c2445b8f5 427 */
wvd_vegt 7:269c2445b8f5 428 static float version() {
wvd_vegt 7:269c2445b8f5 429 return CMDB_VERSION;
wvd_vegt 7:269c2445b8f5 430 }
wvd_vegt 3:abbf43fab7d5 431
wvd_vegt 3:abbf43fab7d5 432 /** Checks if the macro buffer has any characters left.
wvd_vegt 3:abbf43fab7d5 433 *
wvd_vegt 3:abbf43fab7d5 434 * @returns true if any characters left.
wvd_vegt 3:abbf43fab7d5 435 */
wvd_vegt 7:269c2445b8f5 436 bool macro_hasnext();
wvd_vegt 3:abbf43fab7d5 437
wvd_vegt 4:e7673688a9c8 438 /** Gets the next character from the macro buffer and
wvd_vegt 3:abbf43fab7d5 439 * advances the macro buffer pointer.
wvd_vegt 3:abbf43fab7d5 440 *
wvd_vegt 7:269c2445b8f5 441 * @note Do not call if no more characters are left!
wvd_vegt 3:abbf43fab7d5 442 *
wvd_vegt 3:abbf43fab7d5 443 * @returns the next character.
wvd_vegt 3:abbf43fab7d5 444 */
wvd_vegt 7:269c2445b8f5 445 char macro_next();
wvd_vegt 3:abbf43fab7d5 446
wvd_vegt 3:abbf43fab7d5 447 /** Gets the next character from the macro buffer
wvd_vegt 3:abbf43fab7d5 448 * but does not advance the macro buffer pointer.
wvd_vegt 3:abbf43fab7d5 449 *
wvd_vegt 7:269c2445b8f5 450 * @note Do not call if no more characters are left!
wvd_vegt 3:abbf43fab7d5 451 *
wvd_vegt 3:abbf43fab7d5 452 * @returns the next character.
wvd_vegt 3:abbf43fab7d5 453 */
wvd_vegt 7:269c2445b8f5 454 char macro_peek();
wvd_vegt 4:e7673688a9c8 455
wvd_vegt 3:abbf43fab7d5 456 /** Resets the macro buffer and macro buffer pointer.
wvd_vegt 4:e7673688a9c8 457 *
wvd_vegt 3:abbf43fab7d5 458 */
wvd_vegt 7:269c2445b8f5 459 void macro_reset();
wvd_vegt 3:abbf43fab7d5 460
wvd_vegt 4:e7673688a9c8 461 /** Checks if the serial port has any characters
wvd_vegt 3:abbf43fab7d5 462 * left to read by calling serial.readable().
wvd_vegt 3:abbf43fab7d5 463 *
wvd_vegt 3:abbf43fab7d5 464 * @returns true if any characters available.
wvd_vegt 3:abbf43fab7d5 465 */
wvd_vegt 7:269c2445b8f5 466 bool hasnext();
wvd_vegt 3:abbf43fab7d5 467
wvd_vegt 4:e7673688a9c8 468 /** Gets the next character from the serial port by
wvd_vegt 3:abbf43fab7d5 469 * calling serial.getc().
wvd_vegt 3:abbf43fab7d5 470 *
wvd_vegt 3:abbf43fab7d5 471 * Do not call if no characters are left!
wvd_vegt 3:abbf43fab7d5 472 *
wvd_vegt 3:abbf43fab7d5 473 * @returns the next character.
wvd_vegt 3:abbf43fab7d5 474 */
wvd_vegt 7:269c2445b8f5 475 char next();
wvd_vegt 3:abbf43fab7d5 476
wvd_vegt 4:e7673688a9c8 477 /** Add a character to the command being processed.
wvd_vegt 3:abbf43fab7d5 478 * If a cr is added, the command is parsed and executed if possible
wvd_vegt 3:abbf43fab7d5 479 * If supported special keys are encountered (like backspace, delete and cursor up) they are processed.
wvd_vegt 3:abbf43fab7d5 480 *
wvd_vegt 7:269c2445b8f5 481 * @param c the character to add.
wvd_vegt 3:abbf43fab7d5 482 *
wvd_vegt 3:abbf43fab7d5 483 * @returns true if a command was recognized and executed.
wvd_vegt 3:abbf43fab7d5 484 */
wvd_vegt 7:269c2445b8f5 485 bool scan(const char c);
wvd_vegt 7:269c2445b8f5 486
wvd_vegt 7:269c2445b8f5 487 /** printf substitute using the serial parameter passed to the constructor.
wvd_vegt 7:269c2445b8f5 488 *
wvd_vegt 7:269c2445b8f5 489 * @see http://www.cplusplus.com/reference/clibrary/cstdio/printf/
wvd_vegt 7:269c2445b8f5 490 *
wvd_vegt 7:269c2445b8f5 491 * @parm format the printf format string.
wvd_vegt 7:269c2445b8f5 492 * @parm ... optional paramaters to be merged into the format string.
wvd_vegt 7:269c2445b8f5 493 *
wvd_vegt 7:269c2445b8f5 494 * @returns the printf return value.
wvd_vegt 7:269c2445b8f5 495 */
wvd_vegt 7:269c2445b8f5 496 int printf(const char *format, ...);
wvd_vegt 3:abbf43fab7d5 497
wvd_vegt 7:269c2445b8f5 498 /** print is simply printf without parameters using the serial parameter passed to the constructor.
wvd_vegt 7:269c2445b8f5 499 *
wvd_vegt 7:269c2445b8f5 500 * @parm msg the string to print.
wvd_vegt 7:269c2445b8f5 501 *
wvd_vegt 7:269c2445b8f5 502 * @returns the printf return value.
wvd_vegt 7:269c2445b8f5 503 */
wvd_vegt 7:269c2445b8f5 504 int print(const char *msg);
wvd_vegt 7:269c2445b8f5 505
wvd_vegt 7:269c2445b8f5 506 /** printch is simply putc subsitute using the serial parameter passed to the constructor.
wvd_vegt 7:269c2445b8f5 507 *
wvd_vegt 7:269c2445b8f5 508 * @parm msg the string to print.
wvd_vegt 7:269c2445b8f5 509 *
wvd_vegt 7:269c2445b8f5 510 * @returns the printf return value.
wvd_vegt 7:269c2445b8f5 511 */
wvd_vegt 7:269c2445b8f5 512 char printch(const char ch);
wvd_vegt 7:269c2445b8f5 513
wvd_vegt 7:269c2445b8f5 514 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 515
wvd_vegt 7:269c2445b8f5 516 /** Initializes the parser (called by the constructor).
wvd_vegt 7:269c2445b8f5 517 *
wvd_vegt 7:269c2445b8f5 518 * @parm full if true the macro buffer is also cleared else only the command interpreter is reset.
wvd_vegt 7:269c2445b8f5 519 */
wvd_vegt 7:269c2445b8f5 520 void init(const char full);
wvd_vegt 3:abbf43fab7d5 521
wvd_vegt 3:abbf43fab7d5 522 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 523 //----These helper functions retieve parameters in the correct format.
wvd_vegt 3:abbf43fab7d5 524 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 525
wvd_vegt 7:269c2445b8f5 526 /** Typecasts parameter ndx to a bool.
wvd_vegt 7:269c2445b8f5 527 *
wvd_vegt 7:269c2445b8f5 528 * mask: %bu
wvd_vegt 7:269c2445b8f5 529 *
wvd_vegt 7:269c2445b8f5 530 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 531 *
wvd_vegt 7:269c2445b8f5 532 * @return a bool
wvd_vegt 7:269c2445b8f5 533 */
wvd_vegt 3:abbf43fab7d5 534 bool BOOLPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 535 return parms[ndx].val.uc!=0;
wvd_vegt 3:abbf43fab7d5 536 }
wvd_vegt 3:abbf43fab7d5 537
wvd_vegt 7:269c2445b8f5 538 /** Typecasts parameter ndx to a byte/unsigned char.
wvd_vegt 7:269c2445b8f5 539 *
wvd_vegt 7:269c2445b8f5 540 * mask: %bu
wvd_vegt 7:269c2445b8f5 541 *
wvd_vegt 7:269c2445b8f5 542 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 543 *
wvd_vegt 7:269c2445b8f5 544 * @return a byte/unsigned char
wvd_vegt 7:269c2445b8f5 545 */
wvd_vegt 3:abbf43fab7d5 546 unsigned char BYTEPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 547 return parms[ndx].val.uc;
wvd_vegt 3:abbf43fab7d5 548 }
wvd_vegt 3:abbf43fab7d5 549
wvd_vegt 7:269c2445b8f5 550 /** Typecasts parameter ndx to a char.
wvd_vegt 7:269c2445b8f5 551 *
wvd_vegt 7:269c2445b8f5 552 * mask: %c
wvd_vegt 7:269c2445b8f5 553 *
wvd_vegt 7:269c2445b8f5 554 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 555 *
wvd_vegt 7:269c2445b8f5 556 * @return a char
wvd_vegt 7:269c2445b8f5 557 */
wvd_vegt 3:abbf43fab7d5 558 char CHARPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 559 return parms[ndx].val.c;
wvd_vegt 3:abbf43fab7d5 560 }
wvd_vegt 3:abbf43fab7d5 561
wvd_vegt 7:269c2445b8f5 562 /** Typecasts parameter ndx to word/unsigned int.
wvd_vegt 7:269c2445b8f5 563 *
wvd_vegt 7:269c2445b8f5 564 * mask: %hu
wvd_vegt 7:269c2445b8f5 565 *
wvd_vegt 7:269c2445b8f5 566 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 567 *
wvd_vegt 7:269c2445b8f5 568 * @return a word/unsigned int
wvd_vegt 7:269c2445b8f5 569 */
wvd_vegt 3:abbf43fab7d5 570 unsigned int WORDPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 571 return parms[ndx].val.ui;
wvd_vegt 3:abbf43fab7d5 572 }
wvd_vegt 3:abbf43fab7d5 573
wvd_vegt 7:269c2445b8f5 574 /** Typecasts parameter ndx to a unsigned int.
wvd_vegt 7:269c2445b8f5 575 *
wvd_vegt 7:269c2445b8f5 576 * mask: %u
wvd_vegt 7:269c2445b8f5 577 *
wvd_vegt 7:269c2445b8f5 578 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 579 *
wvd_vegt 7:269c2445b8f5 580 * @return a unsigned int
wvd_vegt 7:269c2445b8f5 581 */
wvd_vegt 3:abbf43fab7d5 582 unsigned int UINTPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 583 return parms[ndx].val.ui;
wvd_vegt 3:abbf43fab7d5 584 }
wvd_vegt 3:abbf43fab7d5 585
wvd_vegt 7:269c2445b8f5 586 /** Typecasts parameter ndx to a int.
wvd_vegt 7:269c2445b8f5 587 *
wvd_vegt 7:269c2445b8f5 588 * mask: %i
wvd_vegt 7:269c2445b8f5 589 *
wvd_vegt 7:269c2445b8f5 590 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 591 *
wvd_vegt 7:269c2445b8f5 592 * @return a int
wvd_vegt 7:269c2445b8f5 593 */
wvd_vegt 3:abbf43fab7d5 594 int INTPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 595 return parms[ndx].val.i;
wvd_vegt 3:abbf43fab7d5 596 }
wvd_vegt 3:abbf43fab7d5 597
wvd_vegt 7:269c2445b8f5 598 /** Typecasts parameter ndx to a bool.
wvd_vegt 7:269c2445b8f5 599 *
wvd_vegt 7:269c2445b8f5 600 * mask: %lu
wvd_vegt 7:269c2445b8f5 601 *
wvd_vegt 7:269c2445b8f5 602 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 603 *
wvd_vegt 7:269c2445b8f5 604 * @return a bool
wvd_vegt 7:269c2445b8f5 605 */
wvd_vegt 3:abbf43fab7d5 606 unsigned long DWORDPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 607 return parms[ndx].val.ul;
wvd_vegt 3:abbf43fab7d5 608 }
wvd_vegt 3:abbf43fab7d5 609
wvd_vegt 7:269c2445b8f5 610 /** Typecasts parameter ndx to a long.
wvd_vegt 7:269c2445b8f5 611 *
wvd_vegt 7:269c2445b8f5 612 * mask: %li
wvd_vegt 7:269c2445b8f5 613 *
wvd_vegt 7:269c2445b8f5 614 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 615 *
wvd_vegt 7:269c2445b8f5 616 * @return a long
wvd_vegt 7:269c2445b8f5 617 */
wvd_vegt 3:abbf43fab7d5 618 long LONGPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 619 return parms[ndx].val.l;
wvd_vegt 3:abbf43fab7d5 620 }
wvd_vegt 3:abbf43fab7d5 621
wvd_vegt 7:269c2445b8f5 622 /** Typecasts parameter ndx to a float.
wvd_vegt 7:269c2445b8f5 623 *
wvd_vegt 7:269c2445b8f5 624 * mask: %f
wvd_vegt 7:269c2445b8f5 625 *
wvd_vegt 7:269c2445b8f5 626 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 627 *
wvd_vegt 7:269c2445b8f5 628 * @return a float
wvd_vegt 7:269c2445b8f5 629 */
wvd_vegt 3:abbf43fab7d5 630 float FLOATPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 631 return parms[ndx].val.f;
wvd_vegt 3:abbf43fab7d5 632 }
wvd_vegt 3:abbf43fab7d5 633
wvd_vegt 7:269c2445b8f5 634 /** Typecasts parameter ndx to a string.
wvd_vegt 7:269c2445b8f5 635 *
wvd_vegt 7:269c2445b8f5 636 * @note spaces are not allowed as it makes parsing so much harder.
wvd_vegt 7:269c2445b8f5 637 *
wvd_vegt 7:269c2445b8f5 638 * mask: %s
wvd_vegt 7:269c2445b8f5 639 *
wvd_vegt 7:269c2445b8f5 640 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 641 *
wvd_vegt 7:269c2445b8f5 642 * @return a string
wvd_vegt 7:269c2445b8f5 643 */
wvd_vegt 3:abbf43fab7d5 644 char* STRINGPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 645 return parms[ndx].val.s;
wvd_vegt 3:abbf43fab7d5 646 }
wvd_vegt 3:abbf43fab7d5 647
wvd_vegt 6:76b033971c3c 648 private:
wvd_vegt 6:76b033971c3c 649
wvd_vegt 7:269c2445b8f5 650 /** C callback function
wvd_vegt 7:269c2445b8f5 651 *
wvd_vegt 7:269c2445b8f5 652 * @see See http://www.newty.de/fpt/fpt.html#chapter2 for function pointers.
wvd_vegt 7:269c2445b8f5 653 *
wvd_vegt 7:269c2445b8f5 654 * C++ member equivalent would be void (Cmdb::*callback)(Cmdb&,int);
wvd_vegt 7:269c2445b8f5 655 */
wvd_vegt 13:c18957fb06e4 656 void (*user_callback)(Cmdb&,int);
wvd_vegt 6:76b033971c3c 657
wvd_vegt 6:76b033971c3c 658 /** Searches the escape code list for a match.
wvd_vegt 6:76b033971c3c 659 *
wvd_vegt 6:76b033971c3c 660 * @param char* escstr the escape code to lookup.
wvd_vegt 6:76b033971c3c 661 *
wvd_vegt 6:76b033971c3c 662 * @returns the index of the escape code or -1.
wvd_vegt 6:76b033971c3c 663 */
wvd_vegt 7:269c2445b8f5 664 int escid_search(char *escstr);
wvd_vegt 6:76b033971c3c 665
wvd_vegt 6:76b033971c3c 666 /** Checks if the command table for a match.
wvd_vegt 6:76b033971c3c 667 *
wvd_vegt 6:76b033971c3c 668 * @param char* cmdstr the command to lookup.
wvd_vegt 6:76b033971c3c 669 *
wvd_vegt 6:76b033971c3c 670 * @returns the id of the command or -1.
wvd_vegt 6:76b033971c3c 671 */
wvd_vegt 7:269c2445b8f5 672 int cmdid_search(char *cmdstr);
wvd_vegt 6:76b033971c3c 673
wvd_vegt 6:76b033971c3c 674 /** Converts an command id to an index of the command table.
wvd_vegt 6:76b033971c3c 675 *
wvd_vegt 6:76b033971c3c 676 * @param cmdid the command id to lookup.
wvd_vegt 6:76b033971c3c 677 *
wvd_vegt 6:76b033971c3c 678 * @returns the index of the command or -1.
wvd_vegt 6:76b033971c3c 679 */
wvd_vegt 7:269c2445b8f5 680 int cmdid_index(int cmdid);
wvd_vegt 6:76b033971c3c 681
wvd_vegt 6:76b033971c3c 682 /** Writes a prompt to the serial port.
wvd_vegt 6:76b033971c3c 683 *
wvd_vegt 6:76b033971c3c 684 */
wvd_vegt 7:269c2445b8f5 685 void prompt(void);
wvd_vegt 6:76b033971c3c 686
wvd_vegt 7:269c2445b8f5 687 /** Called by cmd_dispatch it parses the command against the command table.
wvd_vegt 6:76b033971c3c 688 *
wvd_vegt 6:76b033971c3c 689 * @param cmd the command and paramaters to parse.
wvd_vegt 6:76b033971c3c 690 *
wvd_vegt 6:76b033971c3c 691 * @returns the id of the parsed command.
wvd_vegt 6:76b033971c3c 692 */
wvd_vegt 7:269c2445b8f5 693 int parse(char *cmd);
wvd_vegt 6:76b033971c3c 694
wvd_vegt 7:269c2445b8f5 695 /** Called by scan it processes the arguments and dispatches the command.
wvd_vegt 6:76b033971c3c 696 *
wvd_vegt 7:269c2445b8f5 697 * Note: This member calls the callback callback function.
wvd_vegt 6:76b033971c3c 698 *
wvd_vegt 6:76b033971c3c 699 * @param cmd the command to dispatch.
wvd_vegt 6:76b033971c3c 700 */
wvd_vegt 7:269c2445b8f5 701 void cmd_dispatcher(char *cmd);
wvd_vegt 6:76b033971c3c 702
wvd_vegt 6:76b033971c3c 703 /** Generates Help from the command table and prints it.
wvd_vegt 6:76b033971c3c 704 *
wvd_vegt 6:76b033971c3c 705 * @param pre leading text
wvd_vegt 6:76b033971c3c 706 * @param ndx the index of the command in the command table.
wvd_vegt 6:76b033971c3c 707 * @param post trailing text.
wvd_vegt 6:76b033971c3c 708 */
wvd_vegt 9:dc9faec79298 709 void cmd_help(char *pre, int ndx, char *post);
wvd_vegt 9:dc9faec79298 710
wvd_vegt 9:dc9faec79298 711 /** Dumps all commands in ini file format.
wvd_vegt 9:dc9faec79298 712 */
wvd_vegt 9:dc9faec79298 713 void cmd_dump();
wvd_vegt 7:269c2445b8f5 714
wvd_vegt 7:269c2445b8f5 715 /** memset wrapper.
wvd_vegt 7:269c2445b8f5 716 *
wvd_vegt 7:269c2445b8f5 717 * @param p The string to be cleared.
wvd_vegt 7:269c2445b8f5 718 * @param siz The string size.
wvd_vegt 7:269c2445b8f5 719 */
wvd_vegt 7:269c2445b8f5 720 void zeromemory(char *p,unsigned int siz);
wvd_vegt 6:76b033971c3c 721
wvd_vegt 7:269c2445b8f5 722 /** Case insensitive compare.
wvd_vegt 7:269c2445b8f5 723 *
wvd_vegt 7:269c2445b8f5 724 * @see strcmp.
wvd_vegt 7:269c2445b8f5 725 *
wvd_vegt 7:269c2445b8f5 726 * @param s1
wvd_vegt 7:269c2445b8f5 727 * @param s2 the second string to compare.
wvd_vegt 7:269c2445b8f5 728 *
wvd_vegt 7:269c2445b8f5 729 * @returns 0 if s1=s2, -1 if s1<s2 or +1 if s1>s2.
wvd_vegt 7:269c2445b8f5 730 */
wvd_vegt 6:76b033971c3c 731 int stricmp (char *s1, char *s2);
wvd_vegt 6:76b033971c3c 732
wvd_vegt 7:269c2445b8f5 733 /** Internal Serial Port Storage.
wvd_vegt 7:269c2445b8f5 734 */
wvd_vegt 7:269c2445b8f5 735 Serial serial;
wvd_vegt 7:269c2445b8f5 736
wvd_vegt 7:269c2445b8f5 737 /** Internal Command Table Vector Storage.
wvd_vegt 7:269c2445b8f5 738 *
wvd_vegt 7:269c2445b8f5 739 * @see http://www.cplusplus.com/reference/stl/vector/
wvd_vegt 7:269c2445b8f5 740 */
wvd_vegt 7:269c2445b8f5 741 std::vector<cmd> cmds;
wvd_vegt 7:269c2445b8f5 742
wvd_vegt 7:269c2445b8f5 743 /** Internal Echo Flag Storage.
wvd_vegt 7:269c2445b8f5 744 */
wvd_vegt 6:76b033971c3c 745 bool echo;
wvd_vegt 7:269c2445b8f5 746
wvd_vegt 7:269c2445b8f5 747 /** Internal VT100 Bold Flag Storage.
wvd_vegt 7:269c2445b8f5 748 */
wvd_vegt 6:76b033971c3c 749 bool bold;
wvd_vegt 6:76b033971c3c 750
wvd_vegt 7:269c2445b8f5 751 /** Internal Command Table Length Storage.
wvd_vegt 7:269c2445b8f5 752 */
wvd_vegt 7:269c2445b8f5 753 //int CMD_TBL_LEN;
wvd_vegt 6:76b033971c3c 754
wvd_vegt 6:76b033971c3c 755 //Macro's.
wvd_vegt 7:269c2445b8f5 756 /** Internal Macro Pointer.
wvd_vegt 7:269c2445b8f5 757 */
wvd_vegt 6:76b033971c3c 758 int macro_ptr;
wvd_vegt 7:269c2445b8f5 759
wvd_vegt 7:269c2445b8f5 760 /** Internal Macro Buffer.
wvd_vegt 7:269c2445b8f5 761 */
wvd_vegt 6:76b033971c3c 762 char macro_buf[1 + MAX_CMD_LEN];
wvd_vegt 6:76b033971c3c 763
wvd_vegt 7:269c2445b8f5 764 /** Used for parsing parameters.
wvd_vegt 7:269c2445b8f5 765 */
wvd_vegt 6:76b033971c3c 766 enum parmtype {
wvd_vegt 6:76b033971c3c 767 PARM_UNUSED, //0
wvd_vegt 6:76b033971c3c 768
wvd_vegt 6:76b033971c3c 769 PARM_FLOAT, //1 (f)
wvd_vegt 6:76b033971c3c 770
wvd_vegt 6:76b033971c3c 771 PARM_LONG, //2 (l/ul)
wvd_vegt 6:76b033971c3c 772 PARM_INT, //3 (i/ui)
wvd_vegt 6:76b033971c3c 773 PARM_SHORT, //4 (w/uw)
wvd_vegt 6:76b033971c3c 774
wvd_vegt 6:76b033971c3c 775 PARM_CHAR, //5 (c/uc)
wvd_vegt 6:76b033971c3c 776 PARM_STRING //6 (s)
wvd_vegt 6:76b033971c3c 777 };
wvd_vegt 6:76b033971c3c 778
wvd_vegt 7:269c2445b8f5 779 /** Used for parsing parameters.
wvd_vegt 7:269c2445b8f5 780 */
wvd_vegt 6:76b033971c3c 781 union value {
wvd_vegt 6:76b033971c3c 782 float f;
wvd_vegt 6:76b033971c3c 783
wvd_vegt 6:76b033971c3c 784 unsigned long ul;
wvd_vegt 6:76b033971c3c 785 long l;
wvd_vegt 6:76b033971c3c 786
wvd_vegt 6:76b033971c3c 787 int i;
wvd_vegt 6:76b033971c3c 788 unsigned int ui;
wvd_vegt 6:76b033971c3c 789
wvd_vegt 6:76b033971c3c 790 short w;
wvd_vegt 6:76b033971c3c 791 unsigned short uw;
wvd_vegt 6:76b033971c3c 792
wvd_vegt 6:76b033971c3c 793 char c;
wvd_vegt 6:76b033971c3c 794 unsigned char uc;
wvd_vegt 6:76b033971c3c 795
wvd_vegt 6:76b033971c3c 796 char s[MAX_PARM_LEN];
wvd_vegt 6:76b033971c3c 797 };
wvd_vegt 6:76b033971c3c 798
wvd_vegt 7:269c2445b8f5 799 /** Used for parsing parameters.
wvd_vegt 7:269c2445b8f5 800 */
wvd_vegt 6:76b033971c3c 801 struct parm {
wvd_vegt 6:76b033971c3c 802 enum parmtype type;
wvd_vegt 6:76b033971c3c 803 union value val;
wvd_vegt 6:76b033971c3c 804 };
wvd_vegt 6:76b033971c3c 805
wvd_vegt 7:269c2445b8f5 806 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 807 //----Buffers & Storage.
wvd_vegt 7:269c2445b8f5 808 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 809
wvd_vegt 7:269c2445b8f5 810 /** Command Buffer.
wvd_vegt 7:269c2445b8f5 811 */
wvd_vegt 7:269c2445b8f5 812 char cmdbuf [1 + MAX_CMD_LEN]; // command buffer
wvd_vegt 7:269c2445b8f5 813
wvd_vegt 7:269c2445b8f5 814 /** Command Buffer Pointer.
wvd_vegt 7:269c2445b8f5 815 */
wvd_vegt 7:269c2445b8f5 816 char cmdndx; // command index
wvd_vegt 3:abbf43fab7d5 817
wvd_vegt 7:269c2445b8f5 818 /** Last Command Buffer (Used when pressing Cursor Up).
wvd_vegt 7:269c2445b8f5 819 */
wvd_vegt 7:269c2445b8f5 820 char lstbuf [1 + MAX_CMD_LEN]; // last command buffer
wvd_vegt 3:abbf43fab7d5 821
wvd_vegt 7:269c2445b8f5 822 /** Escape Code Buffer.
wvd_vegt 7:269c2445b8f5 823 */
wvd_vegt 7:269c2445b8f5 824 char escbuf [1 + MAX_ESC_LEN];
wvd_vegt 7:269c2445b8f5 825
wvd_vegt 7:269c2445b8f5 826 /** Escape Code Buffer Pointer.
wvd_vegt 7:269c2445b8f5 827 */
wvd_vegt 7:269c2445b8f5 828 unsigned char escndx;
wvd_vegt 3:abbf43fab7d5 829
wvd_vegt 7:269c2445b8f5 830 /** Storage for Parsed Parameters
wvd_vegt 7:269c2445b8f5 831 */
wvd_vegt 7:269c2445b8f5 832 struct parm parms[MAX_ARGS];
wvd_vegt 3:abbf43fab7d5 833
wvd_vegt 7:269c2445b8f5 834 /** Parsed Parameters Pointer.
wvd_vegt 7:269c2445b8f5 835 */
wvd_vegt 7:269c2445b8f5 836 int noparms;
wvd_vegt 7:269c2445b8f5 837
wvd_vegt 7:269c2445b8f5 838 /** Current Selected Subsystem (-1 for Global).
wvd_vegt 7:269c2445b8f5 839 */
wvd_vegt 7:269c2445b8f5 840 int subsystem;
wvd_vegt 3:abbf43fab7d5 841
wvd_vegt 7:269c2445b8f5 842 /** No of arguments found in command.
wvd_vegt 7:269c2445b8f5 843 */
wvd_vegt 7:269c2445b8f5 844 int argcnt;
wvd_vegt 3:abbf43fab7d5 845
wvd_vegt 7:269c2445b8f5 846 /** No of arguments to find in parameter definition (Command Table).
wvd_vegt 7:269c2445b8f5 847 */
wvd_vegt 7:269c2445b8f5 848 int argfnd;
wvd_vegt 3:abbf43fab7d5 849
wvd_vegt 7:269c2445b8f5 850 /** strtoXX() Error detection.
wvd_vegt 7:269c2445b8f5 851 */
wvd_vegt 7:269c2445b8f5 852 int error;
wvd_vegt 3:abbf43fab7d5 853 };
wvd_vegt 3:abbf43fab7d5 854
wvd_vegt 7:269c2445b8f5 855 extern "C" void mbed_reset();
wvd_vegt 7:269c2445b8f5 856
wvd_vegt 0:4d95ee0b4c37 857 #endif