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