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

Committer:
wvd_vegt
Date:
Tue Sep 20 18:14:33 2011 +0000
Revision:
18:7a4dc478d022
Parent:
17:7e6a723d65de
1) Made lots of stuff static (including the cmd struct). This is a breaking change (easy to fix though).

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 18:7a4dc478d022 117 const char *cmdstr;
wvd_vegt 3:abbf43fab7d5 118 int subs;
wvd_vegt 7:269c2445b8f5 119 int cid;
wvd_vegt 18:7a4dc478d022 120 const char *parms;
wvd_vegt 18:7a4dc478d022 121 const char *cmddescr;
wvd_vegt 18:7a4dc478d022 122 const char *parmdescr;
wvd_vegt 3:abbf43fab7d5 123 };
wvd_vegt 3:abbf43fab7d5 124
wvd_vegt 7:269c2445b8f5 125 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 126
wvd_vegt 7:269c2445b8f5 127 /** Cr.
wvd_vegt 7:269c2445b8f5 128 */
wvd_vegt 18:7a4dc478d022 129 static const char cr = '\r';
wvd_vegt 5:68d3a351c3ea 130
wvd_vegt 7:269c2445b8f5 131 /** Lf.
wvd_vegt 7:269c2445b8f5 132 */
wvd_vegt 18:7a4dc478d022 133 static const char lf = '\n';
wvd_vegt 7:269c2445b8f5 134
wvd_vegt 7:269c2445b8f5 135 /** Bell.
wvd_vegt 7:269c2445b8f5 136 */
wvd_vegt 18:7a4dc478d022 137 static const char bell = '\7';
wvd_vegt 7:269c2445b8f5 138
wvd_vegt 7:269c2445b8f5 139 /** Escape.
wvd_vegt 7:269c2445b8f5 140 */
wvd_vegt 18:7a4dc478d022 141 static const char esc = '\033';
wvd_vegt 7:269c2445b8f5 142
wvd_vegt 7:269c2445b8f5 143 /** Space.
wvd_vegt 7:269c2445b8f5 144 */
wvd_vegt 18:7a4dc478d022 145 static const char sp = ' ';
wvd_vegt 5:68d3a351c3ea 146
wvd_vegt 7:269c2445b8f5 147 /** CrLf.
wvd_vegt 7:269c2445b8f5 148 */
wvd_vegt 18:7a4dc478d022 149 static const char crlf[] = "\r\n";
wvd_vegt 7:269c2445b8f5 150
wvd_vegt 7:269c2445b8f5 151 /** Backspace that 'tries' to wipe the last character.
wvd_vegt 7:269c2445b8f5 152 */
wvd_vegt 18:7a4dc478d022 153 static const char bs[] = "\b \b";
wvd_vegt 5:68d3a351c3ea 154
wvd_vegt 7:269c2445b8f5 155 /** VT100 Bold Command.
wvd_vegt 7:269c2445b8f5 156 */
wvd_vegt 18:7a4dc478d022 157 static const char boldon[] = "\033[1m";
wvd_vegt 7:269c2445b8f5 158
wvd_vegt 7:269c2445b8f5 159 /** VT100 Normal Command.
wvd_vegt 7:269c2445b8f5 160 */
wvd_vegt 18:7a4dc478d022 161 static const char boldoff[] = "\033[0m";
wvd_vegt 5:68d3a351c3ea 162
wvd_vegt 7:269c2445b8f5 163 /** VT100 Cls Command.
wvd_vegt 7:269c2445b8f5 164 */
wvd_vegt 18:7a4dc478d022 165 static const char cls[] = "\033[2J";
wvd_vegt 7:269c2445b8f5 166
wvd_vegt 7:269c2445b8f5 167 /** VT100 Home Command.
wvd_vegt 7:269c2445b8f5 168 */
wvd_vegt 18:7a4dc478d022 169 static const char home[] = "\033[H";
wvd_vegt 5:68d3a351c3ea 170
wvd_vegt 7:269c2445b8f5 171 /** The default command prompt.
wvd_vegt 7:269c2445b8f5 172 */
wvd_vegt 18:7a4dc478d022 173 static const char PROMPT[] = "CMD>";
wvd_vegt 7:269c2445b8f5 174
wvd_vegt 7:269c2445b8f5 175 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 176
wvd_vegt 7:269c2445b8f5 177 /** Subsystem Id for a Subsystem.
wvd_vegt 7:269c2445b8f5 178 */
wvd_vegt 7:269c2445b8f5 179 #define SUBSYSTEM -1
wvd_vegt 3:abbf43fab7d5 180
wvd_vegt 7:269c2445b8f5 181 /** Subsystem Id for a Global Command (always available).
wvd_vegt 7:269c2445b8f5 182 */
wvd_vegt 3:abbf43fab7d5 183 #define GLOBALCMD -2
wvd_vegt 7:269c2445b8f5 184
wvd_vegt 7:269c2445b8f5 185 /** Subsystem Id for a Hidden Subsystem (ommitted from help).
wvd_vegt 7:269c2445b8f5 186 */
wvd_vegt 3:abbf43fab7d5 187 #define HIDDENSUB -3
wvd_vegt 3:abbf43fab7d5 188
wvd_vegt 9:dc9faec79298 189 /** Predefined Dump Command.
wvd_vegt 9:dc9faec79298 190 */
wvd_vegt 9:dc9faec79298 191 #define CID_COMMANDS 9989
wvd_vegt 9:dc9faec79298 192
wvd_vegt 7:269c2445b8f5 193 /** Predefined Boot Command.
wvd_vegt 7:269c2445b8f5 194 */
wvd_vegt 6:76b033971c3c 195 #define CID_BOOT 9990
wvd_vegt 7:269c2445b8f5 196
wvd_vegt 7:269c2445b8f5 197 /** Predefined Macro Command.
wvd_vegt 7:269c2445b8f5 198 *
wvd_vegt 7:269c2445b8f5 199 * This command will take a string with spaces replace by _ and cr replace by | for later replay with run.
wvd_vegt 7:269c2445b8f5 200 */
wvd_vegt 6:76b033971c3c 201 #define CID_MACRO 9991
wvd_vegt 7:269c2445b8f5 202
wvd_vegt 7:269c2445b8f5 203 /** Predefined Macro Command.
wvd_vegt 7:269c2445b8f5 204 *
wvd_vegt 7:269c2445b8f5 205 * This command replay a macro.
wvd_vegt 7:269c2445b8f5 206 */
wvd_vegt 6:76b033971c3c 207 #define CID_RUN 9992
wvd_vegt 7:269c2445b8f5 208
wvd_vegt 7:269c2445b8f5 209 /** Predefined Macro Command.
wvd_vegt 7:269c2445b8f5 210 *
wvd_vegt 7:269c2445b8f5 211 * This command print the current macro.
wvd_vegt 7:269c2445b8f5 212 */
wvd_vegt 6:76b033971c3c 213 #define CID_MACROS 9993
wvd_vegt 3:abbf43fab7d5 214
wvd_vegt 7:269c2445b8f5 215 /** Predefined Echo Command.
wvd_vegt 7:269c2445b8f5 216 *
wvd_vegt 7:269c2445b8f5 217 * This command turn echo on or off.
wvd_vegt 7:269c2445b8f5 218 */
wvd_vegt 6:76b033971c3c 219 #define CID_ECHO 9994
wvd_vegt 7:269c2445b8f5 220
wvd_vegt 7:269c2445b8f5 221 /** Predefined VT100 Bold Command.
wvd_vegt 7:269c2445b8f5 222 *
wvd_vegt 7:269c2445b8f5 223 * This command turn VT100 bold usage on or off.
wvd_vegt 7:269c2445b8f5 224 */
wvd_vegt 6:76b033971c3c 225 #define CID_BOLD 9995
wvd_vegt 7:269c2445b8f5 226
wvd_vegt 7:269c2445b8f5 227 /** Predefined VT100 Cls Command.
wvd_vegt 7:269c2445b8f5 228 *
wvd_vegt 7:269c2445b8f5 229 * This command will clear the screen.
wvd_vegt 7:269c2445b8f5 230 */
wvd_vegt 6:76b033971c3c 231 #define CID_CLS 9996
wvd_vegt 7:269c2445b8f5 232
wvd_vegt 7:269c2445b8f5 233 /** Predefined Idle Command.
wvd_vegt 7:269c2445b8f5 234 *
wvd_vegt 7:269c2445b8f5 235 * This command will return to the global command level, leaving the active subsystem.
wvd_vegt 7:269c2445b8f5 236 */
wvd_vegt 6:76b033971c3c 237 #define CID_IDLE 9997
wvd_vegt 7:269c2445b8f5 238
wvd_vegt 7:269c2445b8f5 239 /** Predefined Help Command.
wvd_vegt 7:269c2445b8f5 240 *
wvd_vegt 7:269c2445b8f5 241 * This command will either print all active command (without parameters) or a more detailed
wvd_vegt 7:269c2445b8f5 242 * help for a command passed as parameter.
wvd_vegt 7:269c2445b8f5 243 */
wvd_vegt 6:76b033971c3c 244 #define CID_HELP 9998
wvd_vegt 7:269c2445b8f5 245
wvd_vegt 7:269c2445b8f5 246 /** Predefided Semi Command.
wvd_vegt 7:269c2445b8f5 247 *
wvd_vegt 7:269c2445b8f5 248 * CID_LAST only functions as a special Commend Id to signal unknown commands.
wvd_vegt 7:269c2445b8f5 249 */
wvd_vegt 6:76b033971c3c 250 #define CID_LAST 9999
wvd_vegt 3:abbf43fab7d5 251
wvd_vegt 7:269c2445b8f5 252 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 253
wvd_vegt 7:269c2445b8f5 254 /** The Boot Command.
wvd_vegt 7:269c2445b8f5 255 *
wvd_vegt 9:dc9faec79298 256 * @note: this command can be used to list all commands in Windows ini file format for host processing.
wvd_vegt 9:dc9faec79298 257 *
wvd_vegt 7:269c2445b8f5 258 * Optional.
wvd_vegt 7:269c2445b8f5 259 */
wvd_vegt 18:7a4dc478d022 260 static const cmd COMMANDS = {"Commands",GLOBALCMD,CID_COMMANDS,"","Dump Commands"};
wvd_vegt 9:dc9faec79298 261
wvd_vegt 9:dc9faec79298 262 /** The Boot Command.
wvd_vegt 9:dc9faec79298 263 *
wvd_vegt 9:dc9faec79298 264 * Optional.
wvd_vegt 9:dc9faec79298 265 */
wvd_vegt 18:7a4dc478d022 266 static const cmd BOOT = {"Boot",GLOBALCMD,CID_BOOT,"","Boot mBed"};
wvd_vegt 3:abbf43fab7d5 267
wvd_vegt 7:269c2445b8f5 268 /** The Macro Command.
wvd_vegt 7:269c2445b8f5 269 *
wvd_vegt 7:269c2445b8f5 270 * Optional.
wvd_vegt 7:269c2445b8f5 271 */
wvd_vegt 18:7a4dc478d022 272 static const cmd MACRO = {"Macro",GLOBALCMD,CID_MACRO,"%s","Define macro (sp->_, cr->|)","command(s)"};
wvd_vegt 7:269c2445b8f5 273
wvd_vegt 7:269c2445b8f5 274 /** The Run Command.
wvd_vegt 7:269c2445b8f5 275 *
wvd_vegt 7:269c2445b8f5 276 * Optional.
wvd_vegt 7:269c2445b8f5 277 */
wvd_vegt 18:7a4dc478d022 278 static const cmd RUN = {"Run",GLOBALCMD,CID_RUN,"","Run a macro"};
wvd_vegt 7:269c2445b8f5 279
wvd_vegt 7:269c2445b8f5 280 /** The Macros Command.
wvd_vegt 7:269c2445b8f5 281 *
wvd_vegt 7:269c2445b8f5 282 * Optional.
wvd_vegt 7:269c2445b8f5 283 */
wvd_vegt 18:7a4dc478d022 284 static const cmd MACROS = {"Macros",GLOBALCMD,CID_MACROS,"","List macro(s)"};
wvd_vegt 3:abbf43fab7d5 285
wvd_vegt 7:269c2445b8f5 286 /** The Echo Command.
wvd_vegt 7:269c2445b8f5 287 *
wvd_vegt 7:269c2445b8f5 288 * Optional.
wvd_vegt 7:269c2445b8f5 289 */
wvd_vegt 18:7a4dc478d022 290 static const cmd ECHO = {"Echo",GLOBALCMD,CID_ECHO,"%bu","Echo On|Off (1|0)","state"};
wvd_vegt 7:269c2445b8f5 291
wvd_vegt 7:269c2445b8f5 292 /** The Bold Command.
wvd_vegt 7:269c2445b8f5 293 *
wvd_vegt 7:269c2445b8f5 294 * Optional.
wvd_vegt 7:269c2445b8f5 295 */
wvd_vegt 18:7a4dc478d022 296 static const cmd BOLD = {"Bold",GLOBALCMD,CID_BOLD,"%bu","Bold On|Off (1|0)","state"};
wvd_vegt 3:abbf43fab7d5 297
wvd_vegt 7:269c2445b8f5 298 /** The Cls Command.
wvd_vegt 7:269c2445b8f5 299 *
wvd_vegt 7:269c2445b8f5 300 * Optional.
wvd_vegt 7:269c2445b8f5 301 */
wvd_vegt 18:7a4dc478d022 302 static const cmd CLS = {"Cls",GLOBALCMD,CID_CLS,"","Clears the terminal screen"};
wvd_vegt 3:abbf43fab7d5 303
wvd_vegt 7:269c2445b8f5 304 /** The Idle Command.
wvd_vegt 7:269c2445b8f5 305 *
wvd_vegt 7:269c2445b8f5 306 * Mandatory if you use subsystems.
wvd_vegt 7:269c2445b8f5 307 */
wvd_vegt 18:7a4dc478d022 308 static const cmd IDLE = {"Idle",GLOBALCMD,CID_IDLE,"","Deselect Subsystems"};
wvd_vegt 3:abbf43fab7d5 309
wvd_vegt 7:269c2445b8f5 310 /** The Help Command.
wvd_vegt 7:269c2445b8f5 311 *
wvd_vegt 7:269c2445b8f5 312 * Mandatory.
wvd_vegt 7:269c2445b8f5 313 */
wvd_vegt 18:7a4dc478d022 314 static const cmd HELP = {"Help",GLOBALCMD,CID_HELP,"%s","Help"};
wvd_vegt 7:269c2445b8f5 315
wvd_vegt 7:269c2445b8f5 316 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 317
wvd_vegt 7:269c2445b8f5 318 /** We'll only define the 4 cursor codes at the moment.
wvd_vegt 7:269c2445b8f5 319 */
wvd_vegt 3:abbf43fab7d5 320 #define ESC_TBL_LEN 4
wvd_vegt 3:abbf43fab7d5 321
wvd_vegt 7:269c2445b8f5 322 /** Escape code definition struct.
wvd_vegt 7:269c2445b8f5 323 */
wvd_vegt 8:83edd6addbd5 324 struct esc {
wvd_vegt 3:abbf43fab7d5 325 char *escstr;
wvd_vegt 3:abbf43fab7d5 326 int id;
wvd_vegt 3:abbf43fab7d5 327 };
wvd_vegt 3:abbf43fab7d5 328
wvd_vegt 7:269c2445b8f5 329 /** The Escape Code Id's.
wvd_vegt 7:269c2445b8f5 330 */
wvd_vegt 5:68d3a351c3ea 331 enum {
wvd_vegt 3:abbf43fab7d5 332 EID_CURSOR_UP,
wvd_vegt 3:abbf43fab7d5 333 EID_CURSOR_DOWN,
wvd_vegt 3:abbf43fab7d5 334 EID_CURSOR_RIGHT,
wvd_vegt 3:abbf43fab7d5 335 EID_CURSOR_LEFT,
wvd_vegt 3:abbf43fab7d5 336 EID_LAST
wvd_vegt 3:abbf43fab7d5 337 };
wvd_vegt 3:abbf43fab7d5 338
wvd_vegt 7:269c2445b8f5 339 /** The Escape Codes Table.
wvd_vegt 7:269c2445b8f5 340 */
wvd_vegt 18:7a4dc478d022 341 static const struct esc esc_tbl [ESC_TBL_LEN] = {
wvd_vegt 3:abbf43fab7d5 342 { "\033[A", EID_CURSOR_UP },
wvd_vegt 3:abbf43fab7d5 343 { "\033[B", EID_CURSOR_DOWN },
wvd_vegt 3:abbf43fab7d5 344 { "\033[C", EID_CURSOR_RIGHT },
wvd_vegt 3:abbf43fab7d5 345 { "\033[D", EID_CURSOR_LEFT },
wvd_vegt 3:abbf43fab7d5 346 };
wvd_vegt 3:abbf43fab7d5 347
wvd_vegt 7:269c2445b8f5 348 //------------------------------------------------------------------------------
wvd_vegt 6:76b033971c3c 349
wvd_vegt 7:269c2445b8f5 350 /** The Command Interpreter Version.
wvd_vegt 7:269c2445b8f5 351 */
wvd_vegt 18:7a4dc478d022 352 #define CMDB_VERSION 0.80
wvd_vegt 6:76b033971c3c 353
wvd_vegt 7:269c2445b8f5 354 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 355
wvd_vegt 15:d9680ef7b3f8 356 /** Command Interpreter class.
wvd_vegt 17:7e6a723d65de 357 *
wvd_vegt 15:d9680ef7b3f8 358 * Steps to take:
wvd_vegt 17:7e6a723d65de 359 *
wvd_vegt 15:d9680ef7b3f8 360 * 1) Create a std::vector<cmd> and fill it with at least
wvd_vegt 15:d9680ef7b3f8 361 * the mandatory commands IDLE and HELP.
wvd_vegt 17:7e6a723d65de 362 *
wvd_vegt 15:d9680ef7b3f8 363 * 2) Create an Cmdb class instance and pass it the vector,
wvd_vegt 17:7e6a723d65de 364 * a Serial port object like Serial serial(USBTX, USBRX);
wvd_vegt 15:d9680ef7b3f8 365 * and finally a command dispatcher function.
wvd_vegt 17:7e6a723d65de 366 *
wvd_vegt 15:d9680ef7b3f8 367 * 3) Feed the interpreter with characters received from your serial port.
wvd_vegt 15:d9680ef7b3f8 368 * Note: Cmdb self does not retrieve input it must be handed to it.
wvd_vegt 15:d9680ef7b3f8 369 * It implements basic members for checking/reading the serial port.
wvd_vegt 17:7e6a723d65de 370 *
wvd_vegt 15:d9680ef7b3f8 371 * 4) Handle commands added by the application by the Cid and parameters passed.
wvd_vegt 17:7e6a723d65de 372 *
wvd_vegt 15:d9680ef7b3f8 373 * Note: Predefined commands and all subsystems transitions are handled by the internal dispatcher.
wvd_vegt 15:d9680ef7b3f8 374 * So the passed dispatcher only has to handle user/application defined commands'.
wvd_vegt 17:7e6a723d65de 375 *
wvd_vegt 8:83edd6addbd5 376 * @see main.cpp for a demo.
wvd_vegt 3:abbf43fab7d5 377 */
wvd_vegt 3:abbf43fab7d5 378 class Cmdb {
wvd_vegt 3:abbf43fab7d5 379 public:
wvd_vegt 3:abbf43fab7d5 380 /** Create a Command Interpreter.
wvd_vegt 3:abbf43fab7d5 381 *
wvd_vegt 7:269c2445b8f5 382 * @see http://www.newty.de/fpt/fpt.html#chapter2 for function pointers.
wvd_vegt 7:269c2445b8f5 383 * @see http://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c
wvd_vegt 7:269c2445b8f5 384 * @see http://www.daniweb.com/forums/thread293338.html
wvd_vegt 6:76b033971c3c 385 *
wvd_vegt 3:abbf43fab7d5 386 * @param serial a Serial port used for communication.
wvd_vegt 3:abbf43fab7d5 387 * @param cmds a vector with the command table.
wvd_vegt 3:abbf43fab7d5 388 */
wvd_vegt 17:7e6a723d65de 389 Cmdb(const Serial& _serial, std::vector<cmd>& _cmds, void (*_callback)(Cmdb&,int) );
wvd_vegt 7:269c2445b8f5 390
wvd_vegt 7:269c2445b8f5 391 /** The version of the Command Interpreter.
wvd_vegt 7:269c2445b8f5 392 *
wvd_vegt 7:269c2445b8f5 393 * returns the version.
wvd_vegt 7:269c2445b8f5 394 */
wvd_vegt 7:269c2445b8f5 395 static float version() {
wvd_vegt 7:269c2445b8f5 396 return CMDB_VERSION;
wvd_vegt 7:269c2445b8f5 397 }
wvd_vegt 3:abbf43fab7d5 398
wvd_vegt 18:7a4dc478d022 399 /** NULL is used as No Comment Value.
wvd_vegt 18:7a4dc478d022 400 */
wvd_vegt 18:7a4dc478d022 401 static const char* NoComment;
wvd_vegt 18:7a4dc478d022 402
wvd_vegt 18:7a4dc478d022 403 /** Column 72 is used as Default Comment Starting Position.
wvd_vegt 18:7a4dc478d022 404 */
wvd_vegt 18:7a4dc478d022 405 static int DefComPos;
wvd_vegt 18:7a4dc478d022 406
wvd_vegt 3:abbf43fab7d5 407 /** Checks if the macro buffer has any characters left.
wvd_vegt 3:abbf43fab7d5 408 *
wvd_vegt 3:abbf43fab7d5 409 * @returns true if any characters left.
wvd_vegt 3:abbf43fab7d5 410 */
wvd_vegt 7:269c2445b8f5 411 bool macro_hasnext();
wvd_vegt 3:abbf43fab7d5 412
wvd_vegt 4:e7673688a9c8 413 /** Gets the next character from the macro buffer and
wvd_vegt 3:abbf43fab7d5 414 * advances the macro buffer pointer.
wvd_vegt 3:abbf43fab7d5 415 *
wvd_vegt 7:269c2445b8f5 416 * @note Do not call if no more characters are left!
wvd_vegt 3:abbf43fab7d5 417 *
wvd_vegt 3:abbf43fab7d5 418 * @returns the next character.
wvd_vegt 3:abbf43fab7d5 419 */
wvd_vegt 7:269c2445b8f5 420 char macro_next();
wvd_vegt 3:abbf43fab7d5 421
wvd_vegt 3:abbf43fab7d5 422 /** Gets the next character from the macro buffer
wvd_vegt 3:abbf43fab7d5 423 * but does not advance the macro buffer pointer.
wvd_vegt 3:abbf43fab7d5 424 *
wvd_vegt 7:269c2445b8f5 425 * @note Do not call if no more characters are left!
wvd_vegt 3:abbf43fab7d5 426 *
wvd_vegt 3:abbf43fab7d5 427 * @returns the next character.
wvd_vegt 3:abbf43fab7d5 428 */
wvd_vegt 7:269c2445b8f5 429 char macro_peek();
wvd_vegt 4:e7673688a9c8 430
wvd_vegt 3:abbf43fab7d5 431 /** Resets the macro buffer and macro buffer pointer.
wvd_vegt 4:e7673688a9c8 432 *
wvd_vegt 3:abbf43fab7d5 433 */
wvd_vegt 7:269c2445b8f5 434 void macro_reset();
wvd_vegt 3:abbf43fab7d5 435
wvd_vegt 4:e7673688a9c8 436 /** Checks if the serial port has any characters
wvd_vegt 3:abbf43fab7d5 437 * left to read by calling serial.readable().
wvd_vegt 3:abbf43fab7d5 438 *
wvd_vegt 3:abbf43fab7d5 439 * @returns true if any characters available.
wvd_vegt 3:abbf43fab7d5 440 */
wvd_vegt 7:269c2445b8f5 441 bool hasnext();
wvd_vegt 3:abbf43fab7d5 442
wvd_vegt 4:e7673688a9c8 443 /** Gets the next character from the serial port by
wvd_vegt 3:abbf43fab7d5 444 * calling serial.getc().
wvd_vegt 3:abbf43fab7d5 445 *
wvd_vegt 3:abbf43fab7d5 446 * Do not call if no characters are left!
wvd_vegt 3:abbf43fab7d5 447 *
wvd_vegt 3:abbf43fab7d5 448 * @returns the next character.
wvd_vegt 3:abbf43fab7d5 449 */
wvd_vegt 7:269c2445b8f5 450 char next();
wvd_vegt 3:abbf43fab7d5 451
wvd_vegt 4:e7673688a9c8 452 /** Add a character to the command being processed.
wvd_vegt 3:abbf43fab7d5 453 * If a cr is added, the command is parsed and executed if possible
wvd_vegt 3:abbf43fab7d5 454 * If supported special keys are encountered (like backspace, delete and cursor up) they are processed.
wvd_vegt 3:abbf43fab7d5 455 *
wvd_vegt 7:269c2445b8f5 456 * @param c the character to add.
wvd_vegt 3:abbf43fab7d5 457 *
wvd_vegt 3:abbf43fab7d5 458 * @returns true if a command was recognized and executed.
wvd_vegt 3:abbf43fab7d5 459 */
wvd_vegt 7:269c2445b8f5 460 bool scan(const char c);
wvd_vegt 7:269c2445b8f5 461
wvd_vegt 7:269c2445b8f5 462 /** printf substitute using the serial parameter passed to the constructor.
wvd_vegt 7:269c2445b8f5 463 *
wvd_vegt 7:269c2445b8f5 464 * @see http://www.cplusplus.com/reference/clibrary/cstdio/printf/
wvd_vegt 7:269c2445b8f5 465 *
wvd_vegt 7:269c2445b8f5 466 * @parm format the printf format string.
wvd_vegt 7:269c2445b8f5 467 * @parm ... optional paramaters to be merged into the format string.
wvd_vegt 7:269c2445b8f5 468 *
wvd_vegt 7:269c2445b8f5 469 * @returns the printf return value.
wvd_vegt 7:269c2445b8f5 470 */
wvd_vegt 7:269c2445b8f5 471 int printf(const char *format, ...);
wvd_vegt 3:abbf43fab7d5 472
wvd_vegt 7:269c2445b8f5 473 /** print is simply printf without parameters using the serial parameter passed to the constructor.
wvd_vegt 7:269c2445b8f5 474 *
wvd_vegt 7:269c2445b8f5 475 * @parm msg the string to print.
wvd_vegt 7:269c2445b8f5 476 *
wvd_vegt 7:269c2445b8f5 477 * @returns the printf return value.
wvd_vegt 7:269c2445b8f5 478 */
wvd_vegt 7:269c2445b8f5 479 int print(const char *msg);
wvd_vegt 7:269c2445b8f5 480
wvd_vegt 7:269c2445b8f5 481 /** printch is simply putc subsitute using the serial parameter passed to the constructor.
wvd_vegt 7:269c2445b8f5 482 *
wvd_vegt 7:269c2445b8f5 483 * @parm msg the string to print.
wvd_vegt 7:269c2445b8f5 484 *
wvd_vegt 7:269c2445b8f5 485 * @returns the printf return value.
wvd_vegt 7:269c2445b8f5 486 */
wvd_vegt 7:269c2445b8f5 487 char printch(const char ch);
wvd_vegt 7:269c2445b8f5 488
wvd_vegt 18:7a4dc478d022 489 /** printsection prints an inifile Section Header
wvd_vegt 18:7a4dc478d022 490 * like:
wvd_vegt 18:7a4dc478d022 491 *
wvd_vegt 18:7a4dc478d022 492 * [Section]\r\n
wvd_vegt 18:7a4dc478d022 493 *
wvd_vegt 18:7a4dc478d022 494 * Usage: cmdb.printsection("GP");
wvd_vegt 18:7a4dc478d022 495 *
wvd_vegt 18:7a4dc478d022 496 * @parm section the section to print.
wvd_vegt 18:7a4dc478d022 497 *
wvd_vegt 18:7a4dc478d022 498 * @returns the printf return value.
wvd_vegt 18:7a4dc478d022 499 */
wvd_vegt 18:7a4dc478d022 500 int printsection(const char *section);
wvd_vegt 18:7a4dc478d022 501
wvd_vegt 18:7a4dc478d022 502 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 18:7a4dc478d022 503 * like:
wvd_vegt 18:7a4dc478d022 504 *
wvd_vegt 18:7a4dc478d022 505 * Key=Value ;comment\r\n
wvd_vegt 18:7a4dc478d022 506 *
wvd_vegt 18:7a4dc478d022 507 * Note: the Comment is (if present) located at position 72.
wvd_vegt 18:7a4dc478d022 508 *
wvd_vegt 18:7a4dc478d022 509 * Usage: cmdb.printvaluef("Value", Cmdb::DefComPos, "Hex", "0x%8.8X", LPC_RTC->GPREG0);
wvd_vegt 18:7a4dc478d022 510 *
wvd_vegt 18:7a4dc478d022 511 * @parm key the key to print.
wvd_vegt 18:7a4dc478d022 512 * @parm comment the comment to print.
wvd_vegt 18:7a4dc478d022 513 * @parm width the location of the comment to print.
wvd_vegt 18:7a4dc478d022 514 * @parm format the value to print.
wvd_vegt 18:7a4dc478d022 515 * @parm parameter to print.
wvd_vegt 18:7a4dc478d022 516 *
wvd_vegt 18:7a4dc478d022 517 * @returns the printf return value.
wvd_vegt 18:7a4dc478d022 518 */
wvd_vegt 18:7a4dc478d022 519 int printvaluef(const char *key, const int width, const char *comment, const char *format, ...);
wvd_vegt 18:7a4dc478d022 520
wvd_vegt 18:7a4dc478d022 521 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 18:7a4dc478d022 522 * like:
wvd_vegt 18:7a4dc478d022 523 *
wvd_vegt 18:7a4dc478d022 524 * Key=Value\r\n
wvd_vegt 18:7a4dc478d022 525 *
wvd_vegt 18:7a4dc478d022 526 * Usage: cmdb.printvaluef("Value", "0x%8.8X", LPC_RTC->GPREG0);
wvd_vegt 18:7a4dc478d022 527 *
wvd_vegt 18:7a4dc478d022 528 * @parm key the key to print.
wvd_vegt 18:7a4dc478d022 529 * @parm format the value to print.
wvd_vegt 18:7a4dc478d022 530 * @parm parameter to print.
wvd_vegt 18:7a4dc478d022 531 *
wvd_vegt 18:7a4dc478d022 532 * @returns the printf return value.
wvd_vegt 18:7a4dc478d022 533 */
wvd_vegt 18:7a4dc478d022 534 int printvaluef(const char *key, const char *format, ...);
wvd_vegt 18:7a4dc478d022 535
wvd_vegt 18:7a4dc478d022 536 /** printvalue prints an inifile Key/Value Pair
wvd_vegt 18:7a4dc478d022 537 * like:
wvd_vegt 18:7a4dc478d022 538 *
wvd_vegt 18:7a4dc478d022 539 * Key=Value ;comment\r\n
wvd_vegt 18:7a4dc478d022 540 *
wvd_vegt 18:7a4dc478d022 541 * Note the Comment is (if present) located at position 72.
wvd_vegt 18:7a4dc478d022 542 *
wvd_vegt 18:7a4dc478d022 543 * @parm key the key to print.
wvd_vegt 18:7a4dc478d022 544 * @parm value the value to print.
wvd_vegt 18:7a4dc478d022 545 * @parm comment the comment to print.
wvd_vegt 18:7a4dc478d022 546 * @parm width the location of the comment to print.
wvd_vegt 18:7a4dc478d022 547 *
wvd_vegt 18:7a4dc478d022 548 * @returns the printf return value.
wvd_vegt 18:7a4dc478d022 549 */
wvd_vegt 18:7a4dc478d022 550 int printvalue(const char *key, const char *value, const char *comment = NoComment, const int width = DefComPos);
wvd_vegt 18:7a4dc478d022 551
wvd_vegt 7:269c2445b8f5 552 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 553
wvd_vegt 7:269c2445b8f5 554 /** Initializes the parser (called by the constructor).
wvd_vegt 7:269c2445b8f5 555 *
wvd_vegt 7:269c2445b8f5 556 * @parm full if true the macro buffer is also cleared else only the command interpreter is reset.
wvd_vegt 7:269c2445b8f5 557 */
wvd_vegt 7:269c2445b8f5 558 void init(const char full);
wvd_vegt 3:abbf43fab7d5 559
wvd_vegt 3:abbf43fab7d5 560 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 561 //----These helper functions retieve parameters in the correct format.
wvd_vegt 3:abbf43fab7d5 562 //------------------------------------------------------------------------------
wvd_vegt 3:abbf43fab7d5 563
wvd_vegt 7:269c2445b8f5 564 /** Typecasts parameter ndx to a bool.
wvd_vegt 7:269c2445b8f5 565 *
wvd_vegt 7:269c2445b8f5 566 * mask: %bu
wvd_vegt 7:269c2445b8f5 567 *
wvd_vegt 7:269c2445b8f5 568 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 569 *
wvd_vegt 7:269c2445b8f5 570 * @return a bool
wvd_vegt 7:269c2445b8f5 571 */
wvd_vegt 3:abbf43fab7d5 572 bool BOOLPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 573 return parms[ndx].val.uc!=0;
wvd_vegt 3:abbf43fab7d5 574 }
wvd_vegt 3:abbf43fab7d5 575
wvd_vegt 7:269c2445b8f5 576 /** Typecasts parameter ndx to a byte/unsigned char.
wvd_vegt 7:269c2445b8f5 577 *
wvd_vegt 7:269c2445b8f5 578 * mask: %bu
wvd_vegt 7:269c2445b8f5 579 *
wvd_vegt 7:269c2445b8f5 580 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 581 *
wvd_vegt 7:269c2445b8f5 582 * @return a byte/unsigned char
wvd_vegt 7:269c2445b8f5 583 */
wvd_vegt 3:abbf43fab7d5 584 unsigned char BYTEPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 585 return parms[ndx].val.uc;
wvd_vegt 3:abbf43fab7d5 586 }
wvd_vegt 3:abbf43fab7d5 587
wvd_vegt 7:269c2445b8f5 588 /** Typecasts parameter ndx to a char.
wvd_vegt 7:269c2445b8f5 589 *
wvd_vegt 7:269c2445b8f5 590 * mask: %c
wvd_vegt 7:269c2445b8f5 591 *
wvd_vegt 7:269c2445b8f5 592 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 593 *
wvd_vegt 7:269c2445b8f5 594 * @return a char
wvd_vegt 7:269c2445b8f5 595 */
wvd_vegt 3:abbf43fab7d5 596 char CHARPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 597 return parms[ndx].val.c;
wvd_vegt 3:abbf43fab7d5 598 }
wvd_vegt 3:abbf43fab7d5 599
wvd_vegt 7:269c2445b8f5 600 /** Typecasts parameter ndx to word/unsigned int.
wvd_vegt 7:269c2445b8f5 601 *
wvd_vegt 7:269c2445b8f5 602 * mask: %hu
wvd_vegt 7:269c2445b8f5 603 *
wvd_vegt 7:269c2445b8f5 604 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 605 *
wvd_vegt 7:269c2445b8f5 606 * @return a word/unsigned int
wvd_vegt 7:269c2445b8f5 607 */
wvd_vegt 3:abbf43fab7d5 608 unsigned int WORDPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 609 return parms[ndx].val.ui;
wvd_vegt 3:abbf43fab7d5 610 }
wvd_vegt 3:abbf43fab7d5 611
wvd_vegt 7:269c2445b8f5 612 /** Typecasts parameter ndx to a unsigned int.
wvd_vegt 7:269c2445b8f5 613 *
wvd_vegt 7:269c2445b8f5 614 * mask: %u
wvd_vegt 7:269c2445b8f5 615 *
wvd_vegt 7:269c2445b8f5 616 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 617 *
wvd_vegt 7:269c2445b8f5 618 * @return a unsigned int
wvd_vegt 7:269c2445b8f5 619 */
wvd_vegt 3:abbf43fab7d5 620 unsigned int UINTPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 621 return parms[ndx].val.ui;
wvd_vegt 3:abbf43fab7d5 622 }
wvd_vegt 3:abbf43fab7d5 623
wvd_vegt 7:269c2445b8f5 624 /** Typecasts parameter ndx to a int.
wvd_vegt 7:269c2445b8f5 625 *
wvd_vegt 7:269c2445b8f5 626 * mask: %i
wvd_vegt 7:269c2445b8f5 627 *
wvd_vegt 7:269c2445b8f5 628 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 629 *
wvd_vegt 7:269c2445b8f5 630 * @return a int
wvd_vegt 7:269c2445b8f5 631 */
wvd_vegt 3:abbf43fab7d5 632 int INTPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 633 return parms[ndx].val.i;
wvd_vegt 3:abbf43fab7d5 634 }
wvd_vegt 3:abbf43fab7d5 635
wvd_vegt 7:269c2445b8f5 636 /** Typecasts parameter ndx to a bool.
wvd_vegt 7:269c2445b8f5 637 *
wvd_vegt 7:269c2445b8f5 638 * mask: %lu
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 bool
wvd_vegt 7:269c2445b8f5 643 */
wvd_vegt 3:abbf43fab7d5 644 unsigned long DWORDPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 645 return parms[ndx].val.ul;
wvd_vegt 3:abbf43fab7d5 646 }
wvd_vegt 3:abbf43fab7d5 647
wvd_vegt 7:269c2445b8f5 648 /** Typecasts parameter ndx to a long.
wvd_vegt 7:269c2445b8f5 649 *
wvd_vegt 7:269c2445b8f5 650 * mask: %li
wvd_vegt 7:269c2445b8f5 651 *
wvd_vegt 7:269c2445b8f5 652 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 653 *
wvd_vegt 7:269c2445b8f5 654 * @return a long
wvd_vegt 7:269c2445b8f5 655 */
wvd_vegt 3:abbf43fab7d5 656 long LONGPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 657 return parms[ndx].val.l;
wvd_vegt 3:abbf43fab7d5 658 }
wvd_vegt 3:abbf43fab7d5 659
wvd_vegt 7:269c2445b8f5 660 /** Typecasts parameter ndx to a float.
wvd_vegt 7:269c2445b8f5 661 *
wvd_vegt 7:269c2445b8f5 662 * mask: %f
wvd_vegt 7:269c2445b8f5 663 *
wvd_vegt 7:269c2445b8f5 664 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 665 *
wvd_vegt 7:269c2445b8f5 666 * @return a float
wvd_vegt 7:269c2445b8f5 667 */
wvd_vegt 3:abbf43fab7d5 668 float FLOATPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 669 return parms[ndx].val.f;
wvd_vegt 3:abbf43fab7d5 670 }
wvd_vegt 3:abbf43fab7d5 671
wvd_vegt 7:269c2445b8f5 672 /** Typecasts parameter ndx to a string.
wvd_vegt 7:269c2445b8f5 673 *
wvd_vegt 7:269c2445b8f5 674 * @note spaces are not allowed as it makes parsing so much harder.
wvd_vegt 7:269c2445b8f5 675 *
wvd_vegt 7:269c2445b8f5 676 * mask: %s
wvd_vegt 7:269c2445b8f5 677 *
wvd_vegt 7:269c2445b8f5 678 * @parm the parameter index
wvd_vegt 7:269c2445b8f5 679 *
wvd_vegt 7:269c2445b8f5 680 * @return a string
wvd_vegt 7:269c2445b8f5 681 */
wvd_vegt 3:abbf43fab7d5 682 char* STRINGPARM(int ndx) {
wvd_vegt 3:abbf43fab7d5 683 return parms[ndx].val.s;
wvd_vegt 3:abbf43fab7d5 684 }
wvd_vegt 3:abbf43fab7d5 685
wvd_vegt 17:7e6a723d65de 686 bool present(char *cmdstr) {
wvd_vegt 17:7e6a723d65de 687 return cmdid_search(cmdstr)!=CID_LAST;
wvd_vegt 17:7e6a723d65de 688 }
wvd_vegt 17:7e6a723d65de 689
wvd_vegt 17:7e6a723d65de 690 void replace(std::vector<cmd>& newcmds) {
wvd_vegt 17:7e6a723d65de 691 cmds.assign(newcmds.begin(), newcmds.end());
wvd_vegt 17:7e6a723d65de 692 }
wvd_vegt 17:7e6a723d65de 693
wvd_vegt 17:7e6a723d65de 694
wvd_vegt 17:7e6a723d65de 695 int indexof(int cid) {
wvd_vegt 17:7e6a723d65de 696 return cmdid_index(cid);
wvd_vegt 17:7e6a723d65de 697 }
wvd_vegt 17:7e6a723d65de 698
wvd_vegt 17:7e6a723d65de 699 //FAILS...
wvd_vegt 17:7e6a723d65de 700 /*
wvd_vegt 17:7e6a723d65de 701 void insert(int cid, cmd newcmd) {
wvd_vegt 17:7e6a723d65de 702 //Add Command (update our original and then assign/replace cmdb's copy)...
wvd_vegt 17:7e6a723d65de 703 vector<cmd>::iterator iter;
wvd_vegt 17:7e6a723d65de 704
wvd_vegt 17:7e6a723d65de 705 std::vector<cmd> newcmds = std::vector<cmd>(cmds);
wvd_vegt 17:7e6a723d65de 706
wvd_vegt 17:7e6a723d65de 707 iter = newcmds.begin();
wvd_vegt 17:7e6a723d65de 708
wvd_vegt 17:7e6a723d65de 709 newcmds.insert(iter+indexof(cid)+1,newcmd);
wvd_vegt 17:7e6a723d65de 710
wvd_vegt 17:7e6a723d65de 711 replace(newcmds);
wvd_vegt 17:7e6a723d65de 712
wvd_vegt 17:7e6a723d65de 713 printf("Index: %d\r\n", ndx);
wvd_vegt 17:7e6a723d65de 714
wvd_vegt 17:7e6a723d65de 715 print("check #1\r\n");
wvd_vegt 17:7e6a723d65de 716 print("check #2\r\n");
wvd_vegt 17:7e6a723d65de 717
wvd_vegt 17:7e6a723d65de 718 vector<cmd>::iterator it;
wvd_vegt 17:7e6a723d65de 719 it=newcmds.begin();
wvd_vegt 17:7e6a723d65de 720
wvd_vegt 17:7e6a723d65de 721 print("check #3\r\n");
wvd_vegt 17:7e6a723d65de 722 ndx++;
wvd_vegt 17:7e6a723d65de 723 newcmds.insert(it, newcmd);
wvd_vegt 17:7e6a723d65de 724 print("check #4\r\n");
wvd_vegt 17:7e6a723d65de 725
wvd_vegt 17:7e6a723d65de 726 //cmds.push_back(c1);
wvd_vegt 17:7e6a723d65de 727 cmds.assign(newcmds.begin(), newcmds.end());
wvd_vegt 17:7e6a723d65de 728 }
wvd_vegt 17:7e6a723d65de 729 */
wvd_vegt 17:7e6a723d65de 730
wvd_vegt 6:76b033971c3c 731 private:
wvd_vegt 6:76b033971c3c 732
wvd_vegt 17:7e6a723d65de 733 /** Internal Serial Port Storage.
wvd_vegt 17:7e6a723d65de 734 */
wvd_vegt 17:7e6a723d65de 735 Serial serial;
wvd_vegt 17:7e6a723d65de 736
wvd_vegt 17:7e6a723d65de 737 /** Internal Command Table Vector Storage.
wvd_vegt 17:7e6a723d65de 738 *
wvd_vegt 17:7e6a723d65de 739 * @see http://www.cplusplus.com/reference/stl/vector/
wvd_vegt 17:7e6a723d65de 740 */
wvd_vegt 17:7e6a723d65de 741 std::vector<cmd> cmds;
wvd_vegt 17:7e6a723d65de 742
wvd_vegt 7:269c2445b8f5 743 /** C callback function
wvd_vegt 7:269c2445b8f5 744 *
wvd_vegt 7:269c2445b8f5 745 * @see See http://www.newty.de/fpt/fpt.html#chapter2 for function pointers.
wvd_vegt 7:269c2445b8f5 746 *
wvd_vegt 7:269c2445b8f5 747 * C++ member equivalent would be void (Cmdb::*callback)(Cmdb&,int);
wvd_vegt 7:269c2445b8f5 748 */
wvd_vegt 13:c18957fb06e4 749 void (*user_callback)(Cmdb&,int);
wvd_vegt 6:76b033971c3c 750
wvd_vegt 6:76b033971c3c 751 /** Searches the escape code list for a match.
wvd_vegt 6:76b033971c3c 752 *
wvd_vegt 6:76b033971c3c 753 * @param char* escstr the escape code to lookup.
wvd_vegt 6:76b033971c3c 754 *
wvd_vegt 6:76b033971c3c 755 * @returns the index of the escape code or -1.
wvd_vegt 6:76b033971c3c 756 */
wvd_vegt 7:269c2445b8f5 757 int escid_search(char *escstr);
wvd_vegt 6:76b033971c3c 758
wvd_vegt 6:76b033971c3c 759 /** Checks if the command table for a match.
wvd_vegt 6:76b033971c3c 760 *
wvd_vegt 6:76b033971c3c 761 * @param char* cmdstr the command to lookup.
wvd_vegt 6:76b033971c3c 762 *
wvd_vegt 6:76b033971c3c 763 * @returns the id of the command or -1.
wvd_vegt 6:76b033971c3c 764 */
wvd_vegt 7:269c2445b8f5 765 int cmdid_search(char *cmdstr);
wvd_vegt 6:76b033971c3c 766
wvd_vegt 6:76b033971c3c 767 /** Converts an command id to an index of the command table.
wvd_vegt 6:76b033971c3c 768 *
wvd_vegt 6:76b033971c3c 769 * @param cmdid the command id to lookup.
wvd_vegt 6:76b033971c3c 770 *
wvd_vegt 6:76b033971c3c 771 * @returns the index of the command or -1.
wvd_vegt 6:76b033971c3c 772 */
wvd_vegt 7:269c2445b8f5 773 int cmdid_index(int cmdid);
wvd_vegt 6:76b033971c3c 774
wvd_vegt 6:76b033971c3c 775 /** Writes a prompt to the serial port.
wvd_vegt 6:76b033971c3c 776 *
wvd_vegt 6:76b033971c3c 777 */
wvd_vegt 7:269c2445b8f5 778 void prompt(void);
wvd_vegt 6:76b033971c3c 779
wvd_vegt 7:269c2445b8f5 780 /** Called by cmd_dispatch it parses the command against the command table.
wvd_vegt 6:76b033971c3c 781 *
wvd_vegt 6:76b033971c3c 782 * @param cmd the command and paramaters to parse.
wvd_vegt 6:76b033971c3c 783 *
wvd_vegt 6:76b033971c3c 784 * @returns the id of the parsed command.
wvd_vegt 6:76b033971c3c 785 */
wvd_vegt 7:269c2445b8f5 786 int parse(char *cmd);
wvd_vegt 6:76b033971c3c 787
wvd_vegt 7:269c2445b8f5 788 /** Called by scan it processes the arguments and dispatches the command.
wvd_vegt 6:76b033971c3c 789 *
wvd_vegt 7:269c2445b8f5 790 * Note: This member calls the callback callback function.
wvd_vegt 6:76b033971c3c 791 *
wvd_vegt 6:76b033971c3c 792 * @param cmd the command to dispatch.
wvd_vegt 6:76b033971c3c 793 */
wvd_vegt 7:269c2445b8f5 794 void cmd_dispatcher(char *cmd);
wvd_vegt 6:76b033971c3c 795
wvd_vegt 6:76b033971c3c 796 /** Generates Help from the command table and prints it.
wvd_vegt 6:76b033971c3c 797 *
wvd_vegt 6:76b033971c3c 798 * @param pre leading text
wvd_vegt 6:76b033971c3c 799 * @param ndx the index of the command in the command table.
wvd_vegt 6:76b033971c3c 800 * @param post trailing text.
wvd_vegt 6:76b033971c3c 801 */
wvd_vegt 9:dc9faec79298 802 void cmd_help(char *pre, int ndx, char *post);
wvd_vegt 9:dc9faec79298 803
wvd_vegt 9:dc9faec79298 804 /** Dumps all commands in ini file format.
wvd_vegt 9:dc9faec79298 805 */
wvd_vegt 9:dc9faec79298 806 void cmd_dump();
wvd_vegt 7:269c2445b8f5 807
wvd_vegt 7:269c2445b8f5 808 /** memset wrapper.
wvd_vegt 7:269c2445b8f5 809 *
wvd_vegt 7:269c2445b8f5 810 * @param p The string to be cleared.
wvd_vegt 7:269c2445b8f5 811 * @param siz The string size.
wvd_vegt 7:269c2445b8f5 812 */
wvd_vegt 7:269c2445b8f5 813 void zeromemory(char *p,unsigned int siz);
wvd_vegt 6:76b033971c3c 814
wvd_vegt 7:269c2445b8f5 815 /** Case insensitive compare.
wvd_vegt 7:269c2445b8f5 816 *
wvd_vegt 7:269c2445b8f5 817 * @see strcmp.
wvd_vegt 7:269c2445b8f5 818 *
wvd_vegt 7:269c2445b8f5 819 * @param s1
wvd_vegt 7:269c2445b8f5 820 * @param s2 the second string to compare.
wvd_vegt 7:269c2445b8f5 821 *
wvd_vegt 7:269c2445b8f5 822 * @returns 0 if s1=s2, -1 if s1<s2 or +1 if s1>s2.
wvd_vegt 7:269c2445b8f5 823 */
wvd_vegt 6:76b033971c3c 824 int stricmp (char *s1, char *s2);
wvd_vegt 6:76b033971c3c 825
wvd_vegt 7:269c2445b8f5 826 /** Internal Echo Flag Storage.
wvd_vegt 7:269c2445b8f5 827 */
wvd_vegt 6:76b033971c3c 828 bool echo;
wvd_vegt 7:269c2445b8f5 829
wvd_vegt 7:269c2445b8f5 830 /** Internal VT100 Bold Flag Storage.
wvd_vegt 7:269c2445b8f5 831 */
wvd_vegt 6:76b033971c3c 832 bool bold;
wvd_vegt 6:76b033971c3c 833
wvd_vegt 7:269c2445b8f5 834 /** Internal Command Table Length Storage.
wvd_vegt 7:269c2445b8f5 835 */
wvd_vegt 7:269c2445b8f5 836 //int CMD_TBL_LEN;
wvd_vegt 6:76b033971c3c 837
wvd_vegt 6:76b033971c3c 838 //Macro's.
wvd_vegt 7:269c2445b8f5 839 /** Internal Macro Pointer.
wvd_vegt 7:269c2445b8f5 840 */
wvd_vegt 6:76b033971c3c 841 int macro_ptr;
wvd_vegt 7:269c2445b8f5 842
wvd_vegt 7:269c2445b8f5 843 /** Internal Macro Buffer.
wvd_vegt 7:269c2445b8f5 844 */
wvd_vegt 6:76b033971c3c 845 char macro_buf[1 + MAX_CMD_LEN];
wvd_vegt 6:76b033971c3c 846
wvd_vegt 7:269c2445b8f5 847 /** Used for parsing parameters.
wvd_vegt 7:269c2445b8f5 848 */
wvd_vegt 6:76b033971c3c 849 enum parmtype {
wvd_vegt 6:76b033971c3c 850 PARM_UNUSED, //0
wvd_vegt 6:76b033971c3c 851
wvd_vegt 6:76b033971c3c 852 PARM_FLOAT, //1 (f)
wvd_vegt 6:76b033971c3c 853
wvd_vegt 6:76b033971c3c 854 PARM_LONG, //2 (l/ul)
wvd_vegt 6:76b033971c3c 855 PARM_INT, //3 (i/ui)
wvd_vegt 6:76b033971c3c 856 PARM_SHORT, //4 (w/uw)
wvd_vegt 6:76b033971c3c 857
wvd_vegt 6:76b033971c3c 858 PARM_CHAR, //5 (c/uc)
wvd_vegt 6:76b033971c3c 859 PARM_STRING //6 (s)
wvd_vegt 6:76b033971c3c 860 };
wvd_vegt 6:76b033971c3c 861
wvd_vegt 7:269c2445b8f5 862 /** Used for parsing parameters.
wvd_vegt 7:269c2445b8f5 863 */
wvd_vegt 6:76b033971c3c 864 union value {
wvd_vegt 6:76b033971c3c 865 float f;
wvd_vegt 6:76b033971c3c 866
wvd_vegt 6:76b033971c3c 867 unsigned long ul;
wvd_vegt 6:76b033971c3c 868 long l;
wvd_vegt 6:76b033971c3c 869
wvd_vegt 6:76b033971c3c 870 int i;
wvd_vegt 6:76b033971c3c 871 unsigned int ui;
wvd_vegt 6:76b033971c3c 872
wvd_vegt 6:76b033971c3c 873 short w;
wvd_vegt 6:76b033971c3c 874 unsigned short uw;
wvd_vegt 6:76b033971c3c 875
wvd_vegt 6:76b033971c3c 876 char c;
wvd_vegt 6:76b033971c3c 877 unsigned char uc;
wvd_vegt 6:76b033971c3c 878
wvd_vegt 6:76b033971c3c 879 char s[MAX_PARM_LEN];
wvd_vegt 6:76b033971c3c 880 };
wvd_vegt 6:76b033971c3c 881
wvd_vegt 7:269c2445b8f5 882 /** Used for parsing parameters.
wvd_vegt 7:269c2445b8f5 883 */
wvd_vegt 6:76b033971c3c 884 struct parm {
wvd_vegt 6:76b033971c3c 885 enum parmtype type;
wvd_vegt 6:76b033971c3c 886 union value val;
wvd_vegt 6:76b033971c3c 887 };
wvd_vegt 6:76b033971c3c 888
wvd_vegt 7:269c2445b8f5 889 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 890 //----Buffers & Storage.
wvd_vegt 7:269c2445b8f5 891 //------------------------------------------------------------------------------
wvd_vegt 7:269c2445b8f5 892
wvd_vegt 7:269c2445b8f5 893 /** Command Buffer.
wvd_vegt 7:269c2445b8f5 894 */
wvd_vegt 7:269c2445b8f5 895 char cmdbuf [1 + MAX_CMD_LEN]; // command buffer
wvd_vegt 7:269c2445b8f5 896
wvd_vegt 7:269c2445b8f5 897 /** Command Buffer Pointer.
wvd_vegt 7:269c2445b8f5 898 */
wvd_vegt 7:269c2445b8f5 899 char cmdndx; // command index
wvd_vegt 3:abbf43fab7d5 900
wvd_vegt 7:269c2445b8f5 901 /** Last Command Buffer (Used when pressing Cursor Up).
wvd_vegt 7:269c2445b8f5 902 */
wvd_vegt 7:269c2445b8f5 903 char lstbuf [1 + MAX_CMD_LEN]; // last command buffer
wvd_vegt 3:abbf43fab7d5 904
wvd_vegt 7:269c2445b8f5 905 /** Escape Code Buffer.
wvd_vegt 7:269c2445b8f5 906 */
wvd_vegt 7:269c2445b8f5 907 char escbuf [1 + MAX_ESC_LEN];
wvd_vegt 7:269c2445b8f5 908
wvd_vegt 7:269c2445b8f5 909 /** Escape Code Buffer Pointer.
wvd_vegt 7:269c2445b8f5 910 */
wvd_vegt 7:269c2445b8f5 911 unsigned char escndx;
wvd_vegt 3:abbf43fab7d5 912
wvd_vegt 7:269c2445b8f5 913 /** Storage for Parsed Parameters
wvd_vegt 7:269c2445b8f5 914 */
wvd_vegt 7:269c2445b8f5 915 struct parm parms[MAX_ARGS];
wvd_vegt 3:abbf43fab7d5 916
wvd_vegt 7:269c2445b8f5 917 /** Parsed Parameters Pointer.
wvd_vegt 7:269c2445b8f5 918 */
wvd_vegt 7:269c2445b8f5 919 int noparms;
wvd_vegt 7:269c2445b8f5 920
wvd_vegt 7:269c2445b8f5 921 /** Current Selected Subsystem (-1 for Global).
wvd_vegt 7:269c2445b8f5 922 */
wvd_vegt 7:269c2445b8f5 923 int subsystem;
wvd_vegt 3:abbf43fab7d5 924
wvd_vegt 7:269c2445b8f5 925 /** No of arguments found in command.
wvd_vegt 7:269c2445b8f5 926 */
wvd_vegt 7:269c2445b8f5 927 int argcnt;
wvd_vegt 3:abbf43fab7d5 928
wvd_vegt 7:269c2445b8f5 929 /** No of arguments to find in parameter definition (Command Table).
wvd_vegt 7:269c2445b8f5 930 */
wvd_vegt 7:269c2445b8f5 931 int argfnd;
wvd_vegt 3:abbf43fab7d5 932
wvd_vegt 7:269c2445b8f5 933 /** strtoXX() Error detection.
wvd_vegt 7:269c2445b8f5 934 */
wvd_vegt 7:269c2445b8f5 935 int error;
wvd_vegt 3:abbf43fab7d5 936 };
wvd_vegt 3:abbf43fab7d5 937
wvd_vegt 7:269c2445b8f5 938 extern "C" void mbed_reset();
wvd_vegt 7:269c2445b8f5 939
wvd_vegt 0:4d95ee0b4c37 940 #endif