editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Committer:
whismanoid
Date:
Thu Jun 06 01:50:32 2019 +0000
Revision:
2:0f702da53f2a
Parent:
1:5b33e7447601
Child:
3:cf3de26aa444
add parse_uint16_dec parse_int16_dec functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
whismanoid 0:4d7de8b5c800 1 // /*******************************************************************************
whismanoid 0:4d7de8b5c800 2 // * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
whismanoid 0:4d7de8b5c800 3 // *
whismanoid 0:4d7de8b5c800 4 // * Permission is hereby granted, free of charge, to any person obtaining a
whismanoid 0:4d7de8b5c800 5 // * copy of this software and associated documentation files (the "Software"),
whismanoid 0:4d7de8b5c800 6 // * to deal in the Software without restriction, including without limitation
whismanoid 0:4d7de8b5c800 7 // * the rights to use, copy, modify, merge, publish, distribute, sublicense,
whismanoid 0:4d7de8b5c800 8 // * and/or sell copies of the Software, and to permit persons to whom the
whismanoid 0:4d7de8b5c800 9 // * Software is furnished to do so, subject to the following conditions:
whismanoid 0:4d7de8b5c800 10 // *
whismanoid 0:4d7de8b5c800 11 // * The above copyright notice and this permission notice shall be included
whismanoid 0:4d7de8b5c800 12 // * in all copies or substantial portions of the Software.
whismanoid 0:4d7de8b5c800 13 // *
whismanoid 0:4d7de8b5c800 14 // * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
whismanoid 0:4d7de8b5c800 15 // * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
whismanoid 0:4d7de8b5c800 16 // * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
whismanoid 0:4d7de8b5c800 17 // * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
whismanoid 0:4d7de8b5c800 18 // * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
whismanoid 0:4d7de8b5c800 19 // * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
whismanoid 0:4d7de8b5c800 20 // * OTHER DEALINGS IN THE SOFTWARE.
whismanoid 0:4d7de8b5c800 21 // *
whismanoid 0:4d7de8b5c800 22 // * Except as contained in this notice, the name of Maxim Integrated
whismanoid 0:4d7de8b5c800 23 // * Products, Inc. shall not be used except as stated in the Maxim Integrated
whismanoid 0:4d7de8b5c800 24 // * Products, Inc. Branding Policy.
whismanoid 0:4d7de8b5c800 25 // *
whismanoid 0:4d7de8b5c800 26 // * The mere transfer of this software does not imply any licenses
whismanoid 0:4d7de8b5c800 27 // * of trade secrets, proprietary technology, copyrights, patents,
whismanoid 0:4d7de8b5c800 28 // * trademarks, maskwork rights, or any other form of intellectual
whismanoid 0:4d7de8b5c800 29 // * property whatsoever. Maxim Integrated Products, Inc. retains all
whismanoid 0:4d7de8b5c800 30 // * ownership rights.
whismanoid 0:4d7de8b5c800 31 // *******************************************************************************
whismanoid 0:4d7de8b5c800 32 // */
whismanoid 0:4d7de8b5c800 33 // *********************************************************************
whismanoid 0:4d7de8b5c800 34 // @file CmdLine.cpp
whismanoid 0:4d7de8b5c800 35 // *********************************************************************
whismanoid 0:4d7de8b5c800 36
whismanoid 0:4d7de8b5c800 37 #include "CmdLine.h"
whismanoid 0:4d7de8b5c800 38
whismanoid 0:4d7de8b5c800 39 CmdLine::CmdLine(Stream& AssociatedSerialPort, const char *Name)
whismanoid 0:4d7de8b5c800 40 : associatedSerialPort(AssociatedSerialPort)
whismanoid 0:4d7de8b5c800 41 , name(Name)
whismanoid 0:4d7de8b5c800 42 , onEOLcommandParser()
whismanoid 0:4d7de8b5c800 43 {
whismanoid 0:4d7de8b5c800 44 indexOfNextEmptyCell = 0;
whismanoid 0:4d7de8b5c800 45 memset(buf, 0, COMMAND_BUFFER_LENGTH);
whismanoid 0:4d7de8b5c800 46 }
whismanoid 0:4d7de8b5c800 47 /** CmdLine::clear empties the command-line buffer */
whismanoid 0:4d7de8b5c800 48 void CmdLine::clear(void)
whismanoid 0:4d7de8b5c800 49 {
whismanoid 0:4d7de8b5c800 50 indexOfNextEmptyCell = 0;
whismanoid 0:4d7de8b5c800 51 memset(buf, 0, COMMAND_BUFFER_LENGTH);
whismanoid 0:4d7de8b5c800 52 }
whismanoid 0:4d7de8b5c800 53 //void CmdLine::idleAppendIfReadable()
whismanoid 0:4d7de8b5c800 54 //{
whismanoid 0:4d7de8b5c800 55 // // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 56 // // Polymorphism fail: associatedSerialPort.readable()
whismanoid 0:4d7de8b5c800 57 // if (associatedSerialPort.readable()) {
whismanoid 0:4d7de8b5c800 58 // append(associatedSerialPort.getc());
whismanoid 0:4d7de8b5c800 59 // //
whismanoid 0:4d7de8b5c800 60 // // TODO1: set EOL timeout, so that we don't get lingering buffer cruft
whismanoid 0:4d7de8b5c800 61 // //
whismanoid 0:4d7de8b5c800 62 // }
whismanoid 0:4d7de8b5c800 63 //}
whismanoid 0:4d7de8b5c800 64 /** CmdLine::append handles an input character by appending the buffer,
whismanoid 0:4d7de8b5c800 65 * or handling an immediate function like backspace/delete
whismanoid 0:4d7de8b5c800 66 * or other custom immediate motor control functions.
whismanoid 0:4d7de8b5c800 67 */
whismanoid 0:4d7de8b5c800 68 void CmdLine::append(char ch)
whismanoid 0:4d7de8b5c800 69 {
whismanoid 0:4d7de8b5c800 70 void diagnostic_led_EOF();
whismanoid 0:4d7de8b5c800 71 void main_menu_status(CmdLine & cmdLine);
whismanoid 0:4d7de8b5c800 72
whismanoid 0:4d7de8b5c800 73 // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 74 switch (ch)
whismanoid 0:4d7de8b5c800 75 {
whismanoid 0:4d7de8b5c800 76 case '\b': // Unicode (U+0008) BS BACKSPACE as destructive backspace
whismanoid 0:4d7de8b5c800 77 case '\x7f': // Unicode (U+007F) DEL DELETE as destructive backspace
whismanoid 0:4d7de8b5c800 78 if (indexOfNextEmptyCell > 0)
whismanoid 0:4d7de8b5c800 79 {
whismanoid 0:4d7de8b5c800 80 buf[--indexOfNextEmptyCell] = '\0'; // pre-decrement index, overwrite with null
whismanoid 0:4d7de8b5c800 81 associatedSerialPort.printf("\b \b"); // tty: backspace, overwrite with space, backspace
whismanoid 0:4d7de8b5c800 82 }
whismanoid 0:4d7de8b5c800 83 break;
whismanoid 0:4d7de8b5c800 84 case '\r': // Unicode (U+000D) CR CARRIAGE RETURN(CR) as EOL end of line
whismanoid 0:4d7de8b5c800 85 case '\n': // Unicode (U+000A) LF LINE FEED(LF) as EOL end of line
whismanoid 0:4d7de8b5c800 86 //associatedSerialPort.printf("%c", ch); // echo line end
whismanoid 0:4d7de8b5c800 87 associatedSerialPort.printf("\r\n"); // echo line end
whismanoid 0:4d7de8b5c800 88 //~ associatedSerialPort.printf("\r\n~%s~\r\n", buf); // DIAGNOSTIC: print line buffer
whismanoid 0:4d7de8b5c800 89 // parse and handle the command by invoking onEOLcommandParser callback
whismanoid 0:4d7de8b5c800 90 if (onEOLcommandParser) {
whismanoid 0:4d7de8b5c800 91 onEOLcommandParser(*this);
whismanoid 0:4d7de8b5c800 92 }
whismanoid 0:4d7de8b5c800 93 clear();
whismanoid 0:4d7de8b5c800 94 break;
whismanoid 0:4d7de8b5c800 95 #define ECHO_EOF_IMMEDIATELY 1
whismanoid 0:4d7de8b5c800 96 #if ECHO_EOF_IMMEDIATELY
whismanoid 0:4d7de8b5c800 97 case '\x04': // Unicode (U+0004) EOT END OF TRANSMISSION = CTRL+D as EOF end of file
whismanoid 0:4d7de8b5c800 98 diagnostic_led_EOF();
whismanoid 0:4d7de8b5c800 99 //~ main_menu_status(*this);
whismanoid 0:4d7de8b5c800 100 associatedSerialPort.printf("** U+0004 EOT = EOF **"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 101 diagnostic_led_EOF();
whismanoid 0:4d7de8b5c800 102 associatedSerialPort.printf("\r\n\x04\r\n"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 103 //~ associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 104 //~ associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 105 //~ associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 106 clear(); // EOF discard any pending commands, to avoid surprise
whismanoid 0:4d7de8b5c800 107 break;
whismanoid 0:4d7de8b5c800 108 case '\x1a': // Unicode (U+001A) SUB SUBSTITUTE = CTRL+Z as EOF end of file
whismanoid 0:4d7de8b5c800 109 diagnostic_led_EOF();
whismanoid 0:4d7de8b5c800 110 //~ main_menu_status(*this);
whismanoid 0:4d7de8b5c800 111 associatedSerialPort.printf("** U+001A SUB = EOF **"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 112 diagnostic_led_EOF();
whismanoid 0:4d7de8b5c800 113 associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 114 associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 115 associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 116 associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 117 clear(); // EOF discard any pending commands, to avoid surprise
whismanoid 0:4d7de8b5c800 118 break;
whismanoid 0:4d7de8b5c800 119 #endif
whismanoid 0:4d7de8b5c800 120 //
whismanoid 0:4d7de8b5c800 121 // Support commands that get handled immediately w/o waiting for EOL
whismanoid 0:4d7de8b5c800 122 // Avoid using characters that may appear in other commands,
whismanoid 0:4d7de8b5c800 123 // such as 0-9 A-Z a-z and some punctuation %*+-./=
whismanoid 0:4d7de8b5c800 124 // so these 25 characters are available: !"#$&'(),:;<>?@[\]^_`{|}~
whismanoid 0:4d7de8b5c800 125 //case '!': // immediate command !) example
whismanoid 0:4d7de8b5c800 126 // do_some_immediate_action();
whismanoid 0:4d7de8b5c800 127 // clear();
whismanoid 0:4d7de8b5c800 128 // break;
whismanoid 0:4d7de8b5c800 129 //case ' ':
whismanoid 0:4d7de8b5c800 130 // on_immediate_0x20(); // Unicode (U+0020) SPACE
whismanoid 0:4d7de8b5c800 131 // break;
whismanoid 0:4d7de8b5c800 132 case '!':
whismanoid 1:5b33e7447601 133 //~ on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 1:5b33e7447601 134 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 135 if (on_immediate_0x21) {
whismanoid 1:5b33e7447601 136 on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 1:5b33e7447601 137 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 138 }
whismanoid 0:4d7de8b5c800 139 case '{':
whismanoid 1:5b33e7447601 140 //~ on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 1:5b33e7447601 141 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 142 if (on_immediate_0x7b) {
whismanoid 1:5b33e7447601 143 on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 1:5b33e7447601 144 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 145 }
whismanoid 0:4d7de8b5c800 146 case '}':
whismanoid 1:5b33e7447601 147 //~ on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 1:5b33e7447601 148 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 149 if (on_immediate_0x7d) {
whismanoid 1:5b33e7447601 150 on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 1:5b33e7447601 151 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 152 }
whismanoid 0:4d7de8b5c800 153 //
whismanoid 0:4d7de8b5c800 154 // default:
whismanoid 0:4d7de8b5c800 155 case '"':
whismanoid 1:5b33e7447601 156 if (on_immediate_0x22) {
whismanoid 1:5b33e7447601 157 on_immediate_0x22(); // Unicode (U+0022) " QUOTATION MARK
whismanoid 1:5b33e7447601 158 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 159 }
whismanoid 0:4d7de8b5c800 160 case '#':
whismanoid 1:5b33e7447601 161 if (on_immediate_0x23) {
whismanoid 1:5b33e7447601 162 on_immediate_0x23(); // Unicode (U+0023) # NUMBER SIGN = pound sign, hash, crosshatch
whismanoid 1:5b33e7447601 163 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 164 }
whismanoid 0:4d7de8b5c800 165 case '$':
whismanoid 1:5b33e7447601 166 if (on_immediate_0x24) {
whismanoid 1:5b33e7447601 167 on_immediate_0x24(); // Unicode (U+0024) $ DOLLAR SIGN
whismanoid 1:5b33e7447601 168 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 169 }
whismanoid 0:4d7de8b5c800 170 //~ on_immediate_0x24(); // Unicode (U+0024) $ DOLLAR SIGN
whismanoid 0:4d7de8b5c800 171 //~ break; // handled as immediate command, do not append to buffer
whismanoid 0:4d7de8b5c800 172 //case '%':
whismanoid 0:4d7de8b5c800 173 // on_immediate_0x25(); // Unicode (U+0025) % PERCENT SIGN
whismanoid 0:4d7de8b5c800 174 // break;
whismanoid 0:4d7de8b5c800 175 case '&':
whismanoid 1:5b33e7447601 176 if (on_immediate_0x26) {
whismanoid 1:5b33e7447601 177 on_immediate_0x26(); // Unicode (U+0026) & AMPERSAND
whismanoid 1:5b33e7447601 178 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 179 }
whismanoid 0:4d7de8b5c800 180 case '\'':
whismanoid 1:5b33e7447601 181 if (on_immediate_0x27) {
whismanoid 1:5b33e7447601 182 on_immediate_0x27(); // Unicode (U+0027) ' APOSTROPHE
whismanoid 1:5b33e7447601 183 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 184 }
whismanoid 0:4d7de8b5c800 185 case '(':
whismanoid 1:5b33e7447601 186 if (on_immediate_0x28) {
whismanoid 1:5b33e7447601 187 on_immediate_0x28(); // Unicode (U+0028) ( LEFT PARENTHESIS
whismanoid 1:5b33e7447601 188 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 189 }
whismanoid 0:4d7de8b5c800 190 case ')':
whismanoid 1:5b33e7447601 191 if (on_immediate_0x29) {
whismanoid 1:5b33e7447601 192 on_immediate_0x29(); // Unicode (U+0029) ) RIGHT PARENTHESIS
whismanoid 1:5b33e7447601 193 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 194 }
whismanoid 0:4d7de8b5c800 195 //case '*':
whismanoid 0:4d7de8b5c800 196 // on_immediate_0x2a(); // Unicode (U+002A) * ASTERISK
whismanoid 0:4d7de8b5c800 197 // break;
whismanoid 0:4d7de8b5c800 198 //case '+':
whismanoid 0:4d7de8b5c800 199 // on_immediate_0x2b(); // Unicode (U+002B) + PLUS SIGN
whismanoid 0:4d7de8b5c800 200 // break;
whismanoid 0:4d7de8b5c800 201 case ',':
whismanoid 1:5b33e7447601 202 if (on_immediate_0x2c) {
whismanoid 1:5b33e7447601 203 on_immediate_0x2c(); // Unicode (U+002C) , COMMA
whismanoid 1:5b33e7447601 204 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 205 }
whismanoid 0:4d7de8b5c800 206 //case '-':
whismanoid 0:4d7de8b5c800 207 // on_immediate_0x2d(); // Unicode (U+002D) - HYPHEN-MINUS
whismanoid 0:4d7de8b5c800 208 // break;
whismanoid 0:4d7de8b5c800 209 //case '.':
whismanoid 0:4d7de8b5c800 210 // on_immediate_0x2e(); // Unicode (U+002E) . FULL STOP
whismanoid 0:4d7de8b5c800 211 // break;
whismanoid 0:4d7de8b5c800 212 //case '/':
whismanoid 0:4d7de8b5c800 213 // on_immediate_0x2f(); // Unicode (U+002F) / SOLIDUS =SLASH
whismanoid 0:4d7de8b5c800 214 // break;
whismanoid 0:4d7de8b5c800 215 case ':':
whismanoid 1:5b33e7447601 216 if (on_immediate_0x3a) {
whismanoid 1:5b33e7447601 217 on_immediate_0x3a(); // Unicode (U+003A) : COLON
whismanoid 1:5b33e7447601 218 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 219 }
whismanoid 0:4d7de8b5c800 220 case ';':
whismanoid 1:5b33e7447601 221 if (on_immediate_0x3b) {
whismanoid 1:5b33e7447601 222 on_immediate_0x3b(); // Unicode (U+003B) ; SEMICOLON
whismanoid 1:5b33e7447601 223 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 224 }
whismanoid 0:4d7de8b5c800 225 case '<':
whismanoid 1:5b33e7447601 226 if (on_immediate_0x3c) {
whismanoid 1:5b33e7447601 227 on_immediate_0x3c(); // Unicode (U+003C) < LESS-THAN SIGN
whismanoid 1:5b33e7447601 228 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 229 }
whismanoid 0:4d7de8b5c800 230 //case '=':
whismanoid 0:4d7de8b5c800 231 // on_immediate_0x3d(); // Unicode (U+003D) = EQUALS SIGN
whismanoid 0:4d7de8b5c800 232 // break;
whismanoid 0:4d7de8b5c800 233 case '>':
whismanoid 1:5b33e7447601 234 if (on_immediate_0x3e) {
whismanoid 1:5b33e7447601 235 on_immediate_0x3e(); // Unicode (U+003E) > GREATER-THAN SIGN
whismanoid 1:5b33e7447601 236 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 237 }
whismanoid 0:4d7de8b5c800 238 case '?':
whismanoid 1:5b33e7447601 239 if (on_immediate_0x3f) {
whismanoid 1:5b33e7447601 240 on_immediate_0x3f(); // Unicode (U+003F) ? QUESTION MARK
whismanoid 1:5b33e7447601 241 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 242 }
whismanoid 0:4d7de8b5c800 243 case '@':
whismanoid 1:5b33e7447601 244 if (on_immediate_0x40) {
whismanoid 1:5b33e7447601 245 on_immediate_0x40(); // Unicode (U+0040) @ COMMERCIAL AT = at sign
whismanoid 1:5b33e7447601 246 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 247 }
whismanoid 0:4d7de8b5c800 248 case '[':
whismanoid 1:5b33e7447601 249 if (on_immediate_0x5b) {
whismanoid 1:5b33e7447601 250 on_immediate_0x5b(); // Unicode (U+005B) [ LEFT SQUARE BRACKET
whismanoid 1:5b33e7447601 251 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 252 }
whismanoid 0:4d7de8b5c800 253 case '\\':
whismanoid 1:5b33e7447601 254 if (on_immediate_0x5c) {
whismanoid 1:5b33e7447601 255 on_immediate_0x5c(); // Unicode (U+005C) \ REVERSE SOLIDUS
whismanoid 1:5b33e7447601 256 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 257 }
whismanoid 0:4d7de8b5c800 258 case ']':
whismanoid 1:5b33e7447601 259 if (on_immediate_0x5d) {
whismanoid 1:5b33e7447601 260 on_immediate_0x5d(); // Unicode (U+005D) ] RIGHT SQUARE BRACKET
whismanoid 1:5b33e7447601 261 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 262 }
whismanoid 0:4d7de8b5c800 263 case '^':
whismanoid 1:5b33e7447601 264 if (on_immediate_0x5e) {
whismanoid 1:5b33e7447601 265 on_immediate_0x5e(); // Unicode (U+005E) ^ CIRCUMFLEX ACCENT
whismanoid 1:5b33e7447601 266 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 267 }
whismanoid 0:4d7de8b5c800 268 case '_':
whismanoid 1:5b33e7447601 269 if (on_immediate_0x5f) {
whismanoid 1:5b33e7447601 270 on_immediate_0x5f(); // Unicode (U+005F) _ LOW LINE =SPACING UNDERSCORE
whismanoid 1:5b33e7447601 271 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 272 }
whismanoid 0:4d7de8b5c800 273 case '`':
whismanoid 1:5b33e7447601 274 if (on_immediate_0x60) {
whismanoid 1:5b33e7447601 275 on_immediate_0x60(); // Unicode (U+0060) ` GRAVE ACCENT (also called backtick)
whismanoid 1:5b33e7447601 276 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 277 }
whismanoid 0:4d7de8b5c800 278 case '|':
whismanoid 1:5b33e7447601 279 if (on_immediate_0x7c) {
whismanoid 1:5b33e7447601 280 on_immediate_0x7c(); // Unicode (U+007C) | VERTICAL LINE
whismanoid 1:5b33e7447601 281 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 282 }
whismanoid 0:4d7de8b5c800 283 case '~':
whismanoid 1:5b33e7447601 284 if (on_immediate_0x7e) {
whismanoid 1:5b33e7447601 285 on_immediate_0x7e(); // Unicode (U+007E) ~ TILDE
whismanoid 1:5b33e7447601 286 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 287 }
whismanoid 0:4d7de8b5c800 288 //
whismanoid 0:4d7de8b5c800 289 default:
whismanoid 0:4d7de8b5c800 290 MBED_ASSERT(indexOfNextEmptyCell <= COMMAND_BUFFER_LENGTH - 2);
whismanoid 0:4d7de8b5c800 291 buf[indexOfNextEmptyCell++] = ch; // append character, post-increment index
whismanoid 0:4d7de8b5c800 292 buf[indexOfNextEmptyCell] = '\0'; // null-terminate the buffer
whismanoid 0:4d7de8b5c800 293 MBED_ASSERT(indexOfNextEmptyCell <= COMMAND_BUFFER_LENGTH - 1);
whismanoid 0:4d7de8b5c800 294 associatedSerialPort.printf("%c", ch); // echo
whismanoid 0:4d7de8b5c800 295 if (indexOfNextEmptyCell == COMMAND_BUFFER_LENGTH - 1)
whismanoid 0:4d7de8b5c800 296 {
whismanoid 0:4d7de8b5c800 297 // buffer is full, parse what we've got
whismanoid 0:4d7de8b5c800 298 if (onEOLcommandParser) {
whismanoid 0:4d7de8b5c800 299 onEOLcommandParser(*this);
whismanoid 0:4d7de8b5c800 300 }
whismanoid 0:4d7de8b5c800 301 clear();
whismanoid 0:4d7de8b5c800 302 }
whismanoid 0:4d7de8b5c800 303 break;
whismanoid 0:4d7de8b5c800 304 }
whismanoid 0:4d7de8b5c800 305 }
whismanoid 1:5b33e7447601 306
whismanoid 0:4d7de8b5c800 307 # if HAS_DAPLINK_SERIAL
whismanoid 0:4d7de8b5c800 308 CmdLine cmdLine_DAPLINKserial(DAPLINKserial, "DAPLINK");
whismanoid 0:4d7de8b5c800 309 # endif // HAS_DAPLINK_SERIAL
whismanoid 0:4d7de8b5c800 310 // TODO1: diagnostic: define HAS_MICROUSBSERIAL 0
whismanoid 0:4d7de8b5c800 311 //~ #define HAS_MICROUSBSERIAL 0
whismanoid 0:4d7de8b5c800 312 # if HAS_MICROUSBSERIAL
whismanoid 0:4d7de8b5c800 313 CmdLine cmdLine_microUSBserial(microUSBserial, "microUSB");
whismanoid 0:4d7de8b5c800 314 # endif // HAS_MICROUSBSERIAL
whismanoid 0:4d7de8b5c800 315
whismanoid 0:4d7de8b5c800 316 /** CmdLine::parse_and_remove_key matches "key"
whismanoid 0:4d7de8b5c800 317 *
whismanoid 0:4d7de8b5c800 318 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 319 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 320 * @param[out] valueBuf buffer is populated with the substring between key= and the first space delimiter (or end of string)
whismanoid 0:4d7de8b5c800 321 * @param[in] valueBufLen limits the size of valueBuf
whismanoid 0:4d7de8b5c800 322 *
whismanoid 0:4d7de8b5c800 323 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 324 *
whismanoid 0:4d7de8b5c800 325 */
whismanoid 0:4d7de8b5c800 326 bool CmdLine::parse_and_remove_key(const char *key, char *valueBuf, size_t valueBufLen)
whismanoid 0:4d7de8b5c800 327 {
whismanoid 0:4d7de8b5c800 328 // serial().printf("\r\n parse_and_remove_key(\"%s\")...", key);
whismanoid 0:4d7de8b5c800 329 // match key inside buf[]?
whismanoid 0:4d7de8b5c800 330 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 331 {
whismanoid 0:4d7de8b5c800 332 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 333 // serial().printf("\r\n parse_and_remove_key(\"%s\") no match", key);
whismanoid 0:4d7de8b5c800 334 return false; /* no match */
whismanoid 0:4d7de8b5c800 335 }
whismanoid 0:4d7de8b5c800 336 if (buf[idxSearch] != key[0]) { continue; }
whismanoid 0:4d7de8b5c800 337 // possible match; compare buf[idxSearch..] to key[0..]
whismanoid 0:4d7de8b5c800 338 unsigned int idxKey = idxSearch; // test whether buf[idxKey..] == key[0..]
whismanoid 0:4d7de8b5c800 339 unsigned int idxEqualSign = idxSearch; // test whether key=value pair
whismanoid 0:4d7de8b5c800 340 unsigned int idxSpace = idxSearch; // end of key=value word
whismanoid 0:4d7de8b5c800 341 for (unsigned int offset = 0; offset < strlen(key); offset++)
whismanoid 0:4d7de8b5c800 342 {
whismanoid 0:4d7de8b5c800 343 if (buf[idxKey + offset] != key[offset]) { idxKey = 0; break; }
whismanoid 0:4d7de8b5c800 344 idxSpace = idxKey + offset + 1;
whismanoid 0:4d7de8b5c800 345 idxEqualSign = idxKey + offset + 1;
whismanoid 0:4d7de8b5c800 346 if (buf[idxEqualSign] != '=') { idxEqualSign = 0; }
whismanoid 0:4d7de8b5c800 347 }
whismanoid 0:4d7de8b5c800 348 if (idxKey == 0) continue; // no match at idxSearch but keep searching
whismanoid 0:4d7de8b5c800 349 // ASSERT buf[idxKey..] == key[0..]
whismanoid 0:4d7de8b5c800 350 while ((buf[idxSpace] != ' ') && idxSpace < indexOfNextEmptyCell) { idxSpace++; }
whismanoid 0:4d7de8b5c800 351 // serial().printf("\r\n parse_and_remove_key(\"%s\") match at index %d length %d, '=' index %d, ' ' index %d",
whismanoid 0:4d7de8b5c800 352 // key, idxKey, strlen(key), idxEqualSign, idxSpace);
whismanoid 0:4d7de8b5c800 353 if (idxEqualSign != 0) {
whismanoid 0:4d7de8b5c800 354 // found key=value: copy buf[idxEqualSign+1..' '-1] into valueBuf[0..valueBufLen-1]
whismanoid 0:4d7de8b5c800 355 for (unsigned int offset = 0; offset < valueBufLen - 1; offset++)
whismanoid 0:4d7de8b5c800 356 {
whismanoid 0:4d7de8b5c800 357 if (buf[idxEqualSign + 1 + offset] == ' ') break;
whismanoid 0:4d7de8b5c800 358 valueBuf[offset] = buf[idxEqualSign + 1 + offset];
whismanoid 0:4d7de8b5c800 359 valueBuf[offset + 1] = '\0';
whismanoid 0:4d7de8b5c800 360 }
whismanoid 0:4d7de8b5c800 361 } else {
whismanoid 0:4d7de8b5c800 362 // found key but no =value: valueBuf[] = ""
whismanoid 0:4d7de8b5c800 363 valueBuf[0] = '\0';
whismanoid 0:4d7de8b5c800 364 }
whismanoid 0:4d7de8b5c800 365 // on successful match, the key=value should be deleted from cmdbuf
whismanoid 0:4d7de8b5c800 366 //serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" before deleting key",
whismanoid 0:4d7de8b5c800 367 // key, buf, valueBuf);
whismanoid 0:4d7de8b5c800 368 for (unsigned int offset = 0; offset < indexOfNextEmptyCell; offset++)
whismanoid 0:4d7de8b5c800 369 {
whismanoid 0:4d7de8b5c800 370 unsigned int idxCopyDst = idxKey + offset;
whismanoid 0:4d7de8b5c800 371 unsigned int idxCopySrc = idxSpace + 1 + offset;
whismanoid 0:4d7de8b5c800 372 if (idxCopyDst > indexOfNextEmptyCell) break;
whismanoid 0:4d7de8b5c800 373 if (idxCopySrc > indexOfNextEmptyCell) break;
whismanoid 0:4d7de8b5c800 374 buf[idxCopyDst] = buf[idxCopySrc];
whismanoid 0:4d7de8b5c800 375 }
whismanoid 0:4d7de8b5c800 376 // serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" after deleting key",
whismanoid 0:4d7de8b5c800 377 // key, buf, valueBuf);
whismanoid 0:4d7de8b5c800 378 // serial().printf("\r\n parse_and_remove_key(\"%s\") returning true: valueBuf=\"%s\"", key, valueBuf);
whismanoid 0:4d7de8b5c800 379 return true;
whismanoid 0:4d7de8b5c800 380 }
whismanoid 0:4d7de8b5c800 381 // serial().printf("\r\n parse_and_remove_key(\"%s\") no match", key);
whismanoid 0:4d7de8b5c800 382 return false; // no match
whismanoid 0:4d7de8b5c800 383 }
whismanoid 0:4d7de8b5c800 384
whismanoid 0:4d7de8b5c800 385 /** CmdLine::parse_frequency_Hz matches "key"=digits
whismanoid 0:4d7de8b5c800 386 *
whismanoid 0:4d7de8b5c800 387 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 388 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 389 * @param[out] frequency_Hz updated from value string if match "key"=value,
whismanoid 0:4d7de8b5c800 390 * optional suffix kHz or MHz scales the value by 1000 or 10000000
whismanoid 0:4d7de8b5c800 391 *
whismanoid 0:4d7de8b5c800 392 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 393 *
whismanoid 0:4d7de8b5c800 394 */
whismanoid 0:4d7de8b5c800 395 bool CmdLine::parse_frequency_Hz(const char *key, uint32_t& frequency_Hz)
whismanoid 0:4d7de8b5c800 396 {
whismanoid 0:4d7de8b5c800 397 char valueBuf[16];
whismanoid 0:4d7de8b5c800 398 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 399 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 400 {
whismanoid 0:4d7de8b5c800 401 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 402 // parse cmdLine arg (SCLK=\d+(kHZ|MHZ)?)? --> g_SPI_SCLK_Hz
whismanoid 0:4d7de8b5c800 403 frequency_Hz = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 404 if (strstr(valueBuf, "M")) {
whismanoid 0:4d7de8b5c800 405 frequency_Hz = frequency_Hz * 1000000;
whismanoid 0:4d7de8b5c800 406 }
whismanoid 0:4d7de8b5c800 407 if (strstr(valueBuf, "k")) {
whismanoid 0:4d7de8b5c800 408 frequency_Hz = frequency_Hz * 1000;
whismanoid 0:4d7de8b5c800 409 }
whismanoid 0:4d7de8b5c800 410 return true;
whismanoid 0:4d7de8b5c800 411 }
whismanoid 0:4d7de8b5c800 412 return false; // no match
whismanoid 0:4d7de8b5c800 413 }
whismanoid 0:4d7de8b5c800 414
whismanoid 0:4d7de8b5c800 415 /** CmdLine::parse_flag matches "key"=0 or 1
whismanoid 0:4d7de8b5c800 416 *
whismanoid 0:4d7de8b5c800 417 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 418 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 419 * @param[out] nFlagVar variable to be updated by setting or clearing bits specified by nFlagBitMask.
whismanoid 0:4d7de8b5c800 420 * If match "key"=0 then the nFlagBitMask bits in nFlagVar are cleared.
whismanoid 0:4d7de8b5c800 421 * If match "key"=1 then the nFlagBitMask bits in nFlagVar are set.
whismanoid 0:4d7de8b5c800 422 * @param[in] nFlagBitMask bit mask contains binary 1 in the bit to be controlled by the key=value setting
whismanoid 0:4d7de8b5c800 423 *
whismanoid 0:4d7de8b5c800 424 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 425 *
whismanoid 0:4d7de8b5c800 426 */
whismanoid 0:4d7de8b5c800 427 bool CmdLine::parse_flag(const char *key, uint8_t& nFlagVar, const uint8_t nFlagBitMask)
whismanoid 0:4d7de8b5c800 428 {
whismanoid 0:4d7de8b5c800 429 char valueBuf[16];
whismanoid 0:4d7de8b5c800 430 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 431 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 432 {
whismanoid 0:4d7de8b5c800 433 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 434 // parse cmdLine arg (CPHA=\d)? --> g_SPI_dataMode | SPI_MODE1
whismanoid 0:4d7de8b5c800 435 int x = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 436 if (x)
whismanoid 0:4d7de8b5c800 437 {
whismanoid 0:4d7de8b5c800 438 nFlagVar |= nFlagBitMask;
whismanoid 0:4d7de8b5c800 439 }
whismanoid 0:4d7de8b5c800 440 else
whismanoid 0:4d7de8b5c800 441 {
whismanoid 0:4d7de8b5c800 442 nFlagVar &= ~nFlagBitMask;
whismanoid 0:4d7de8b5c800 443 }
whismanoid 0:4d7de8b5c800 444 return true;
whismanoid 0:4d7de8b5c800 445 }
whismanoid 0:4d7de8b5c800 446 return false; // no match
whismanoid 0:4d7de8b5c800 447 }
whismanoid 0:4d7de8b5c800 448
whismanoid 0:4d7de8b5c800 449 /** CmdLine::parse_byte_hex matches "key"=value
whismanoid 0:4d7de8b5c800 450 *
whismanoid 0:4d7de8b5c800 451 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 452 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 453 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 454 *
whismanoid 0:4d7de8b5c800 455 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 456 *
whismanoid 2:0f702da53f2a 457 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 458 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 459 */
whismanoid 0:4d7de8b5c800 460 bool CmdLine::parse_byte_hex(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 461 {
whismanoid 0:4d7de8b5c800 462 char valueBuf[16];
whismanoid 0:4d7de8b5c800 463 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 464 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 465 {
whismanoid 0:4d7de8b5c800 466 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 467 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 468 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 469 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 470 {
whismanoid 0:4d7de8b5c800 471 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 472 return true;
whismanoid 0:4d7de8b5c800 473 }
whismanoid 0:4d7de8b5c800 474 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 475 {
whismanoid 0:4d7de8b5c800 476 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 477 return true;
whismanoid 0:4d7de8b5c800 478 }
whismanoid 2:0f702da53f2a 479 nByteVar = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 0:4d7de8b5c800 480 return true;
whismanoid 0:4d7de8b5c800 481 }
whismanoid 0:4d7de8b5c800 482 return false; // no match
whismanoid 0:4d7de8b5c800 483 }
whismanoid 0:4d7de8b5c800 484
whismanoid 0:4d7de8b5c800 485 /** CmdLine::parse_byte_dec matches "key"=value
whismanoid 0:4d7de8b5c800 486 *
whismanoid 0:4d7de8b5c800 487 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 488 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 489 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 490 *
whismanoid 0:4d7de8b5c800 491 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 492 *
whismanoid 2:0f702da53f2a 493 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 494 * Default number conversion radix is base-10 decimal.
whismanoid 0:4d7de8b5c800 495 */
whismanoid 0:4d7de8b5c800 496 bool CmdLine::parse_byte_dec(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 497 {
whismanoid 0:4d7de8b5c800 498 char valueBuf[16];
whismanoid 0:4d7de8b5c800 499 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 500 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 501 {
whismanoid 0:4d7de8b5c800 502 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 503 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 504 // TODO1: parse_byte_dec take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 505 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 506 {
whismanoid 0:4d7de8b5c800 507 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 508 return true;
whismanoid 0:4d7de8b5c800 509 }
whismanoid 0:4d7de8b5c800 510 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 511 {
whismanoid 0:4d7de8b5c800 512 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 513 return true;
whismanoid 0:4d7de8b5c800 514 }
whismanoid 2:0f702da53f2a 515 nByteVar = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 516 return true;
whismanoid 0:4d7de8b5c800 517 }
whismanoid 0:4d7de8b5c800 518 return false; // no match
whismanoid 0:4d7de8b5c800 519 }
whismanoid 0:4d7de8b5c800 520
whismanoid 0:4d7de8b5c800 521 /** CmdLine::parse_uint16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 522 *
whismanoid 0:4d7de8b5c800 523 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 524 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 525 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 526 *
whismanoid 0:4d7de8b5c800 527 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 528 *
whismanoid 2:0f702da53f2a 529 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 530 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 531 */
whismanoid 0:4d7de8b5c800 532 bool CmdLine::parse_uint16_hex(const char *key, uint16_t& uint16Var)
whismanoid 0:4d7de8b5c800 533 {
whismanoid 0:4d7de8b5c800 534 char valueBuf[16];
whismanoid 0:4d7de8b5c800 535 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 536 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 537 {
whismanoid 0:4d7de8b5c800 538 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 539 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 540 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 541 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 542 {
whismanoid 0:4d7de8b5c800 543 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 544 return true;
whismanoid 0:4d7de8b5c800 545 }
whismanoid 0:4d7de8b5c800 546 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 547 {
whismanoid 0:4d7de8b5c800 548 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 549 return true;
whismanoid 0:4d7de8b5c800 550 }
whismanoid 2:0f702da53f2a 551 uint16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 552 return true;
whismanoid 2:0f702da53f2a 553 }
whismanoid 2:0f702da53f2a 554 return false; // no match
whismanoid 2:0f702da53f2a 555 }
whismanoid 2:0f702da53f2a 556
whismanoid 2:0f702da53f2a 557 /** CmdLine::parse_uint16_dec matches "key"=value
whismanoid 2:0f702da53f2a 558 *
whismanoid 2:0f702da53f2a 559 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 560 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 561 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 562 *
whismanoid 2:0f702da53f2a 563 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 564 *
whismanoid 2:0f702da53f2a 565 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 566 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 567 */
whismanoid 2:0f702da53f2a 568 bool CmdLine::parse_uint16_dec(const char *key, uint16_t& uint16Var)
whismanoid 2:0f702da53f2a 569 {
whismanoid 2:0f702da53f2a 570 char valueBuf[16];
whismanoid 2:0f702da53f2a 571 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 572 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 573 {
whismanoid 2:0f702da53f2a 574 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 575 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 576 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 577 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 578 {
whismanoid 2:0f702da53f2a 579 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 580 return true;
whismanoid 2:0f702da53f2a 581 }
whismanoid 2:0f702da53f2a 582 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 583 {
whismanoid 2:0f702da53f2a 584 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 585 return true;
whismanoid 2:0f702da53f2a 586 }
whismanoid 2:0f702da53f2a 587 uint16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 588 return true;
whismanoid 0:4d7de8b5c800 589 }
whismanoid 0:4d7de8b5c800 590 return false; // no match
whismanoid 0:4d7de8b5c800 591 }
whismanoid 0:4d7de8b5c800 592
whismanoid 0:4d7de8b5c800 593 /** CmdLine::parse_int16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 594 *
whismanoid 0:4d7de8b5c800 595 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 596 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 597 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 598 *
whismanoid 0:4d7de8b5c800 599 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 600 *
whismanoid 2:0f702da53f2a 601 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 602 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 603 */
whismanoid 0:4d7de8b5c800 604 bool CmdLine::parse_int16_hex(const char *key, int16_t& int16Var)
whismanoid 0:4d7de8b5c800 605 {
whismanoid 0:4d7de8b5c800 606 char valueBuf[16];
whismanoid 0:4d7de8b5c800 607 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 608 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 609 {
whismanoid 0:4d7de8b5c800 610 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 611 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 612 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 613 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 614 {
whismanoid 0:4d7de8b5c800 615 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 616 return true;
whismanoid 0:4d7de8b5c800 617 }
whismanoid 0:4d7de8b5c800 618 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 619 {
whismanoid 0:4d7de8b5c800 620 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 621 return true;
whismanoid 0:4d7de8b5c800 622 }
whismanoid 2:0f702da53f2a 623 int16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 624 return true;
whismanoid 2:0f702da53f2a 625 }
whismanoid 2:0f702da53f2a 626 return false; // no match
whismanoid 2:0f702da53f2a 627 }
whismanoid 2:0f702da53f2a 628
whismanoid 2:0f702da53f2a 629 /** CmdLine::parse_int16_dec matches "key"=value
whismanoid 2:0f702da53f2a 630 *
whismanoid 2:0f702da53f2a 631 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 632 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 633 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 634 *
whismanoid 2:0f702da53f2a 635 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 636 *
whismanoid 2:0f702da53f2a 637 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 638 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 639 */
whismanoid 2:0f702da53f2a 640 bool CmdLine::parse_int16_dec(const char *key, int16_t& int16Var)
whismanoid 2:0f702da53f2a 641 {
whismanoid 2:0f702da53f2a 642 char valueBuf[16];
whismanoid 2:0f702da53f2a 643 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 644 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 645 {
whismanoid 2:0f702da53f2a 646 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 647 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 648 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 649 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 650 {
whismanoid 2:0f702da53f2a 651 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 652 return true;
whismanoid 2:0f702da53f2a 653 }
whismanoid 2:0f702da53f2a 654 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 655 {
whismanoid 2:0f702da53f2a 656 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 657 return true;
whismanoid 2:0f702da53f2a 658 }
whismanoid 2:0f702da53f2a 659 int16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 660 return true;
whismanoid 0:4d7de8b5c800 661 }
whismanoid 0:4d7de8b5c800 662 return false; // no match
whismanoid 0:4d7de8b5c800 663 }
whismanoid 0:4d7de8b5c800 664
whismanoid 0:4d7de8b5c800 665 /** CmdLine::parse_float matches "key"=value
whismanoid 0:4d7de8b5c800 666 *
whismanoid 0:4d7de8b5c800 667 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 668 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 669 * @param[out] floatVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 670 *
whismanoid 0:4d7de8b5c800 671 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 672 *
whismanoid 0:4d7de8b5c800 673 */
whismanoid 0:4d7de8b5c800 674 bool CmdLine::parse_float(const char *key, float& floatVar)
whismanoid 0:4d7de8b5c800 675 {
whismanoid 0:4d7de8b5c800 676 char valueBuf[16];
whismanoid 0:4d7de8b5c800 677 char *end;
whismanoid 0:4d7de8b5c800 678 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 679 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 680 {
whismanoid 0:4d7de8b5c800 681 floatVar = strtof(valueBuf, &end);
whismanoid 0:4d7de8b5c800 682 return true;
whismanoid 0:4d7de8b5c800 683 }
whismanoid 0:4d7de8b5c800 684 return false; // no match
whismanoid 0:4d7de8b5c800 685 }
whismanoid 0:4d7de8b5c800 686
whismanoid 0:4d7de8b5c800 687 /** CmdLine::parse_byteCount_byteList_hex matches hexadecimal digits separated by spaces.
whismanoid 0:4d7de8b5c800 688 * The 0x / 0X prefix is optional. The numbers must be hexadecimal.
whismanoid 0:4d7de8b5c800 689 *
whismanoid 0:4d7de8b5c800 690 * @return true if more than one hex byte found in buffer
whismanoid 0:4d7de8b5c800 691 * @param[out] byteCount is populated with the number of hex bytes found
whismanoid 0:4d7de8b5c800 692 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the hex bytes found
whismanoid 0:4d7de8b5c800 693 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 694 *
whismanoid 0:4d7de8b5c800 695 */
whismanoid 0:4d7de8b5c800 696 bool CmdLine::parse_byteCount_byteList_hex(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 697 {
whismanoid 0:4d7de8b5c800 698 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 699 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 700 byteCount = 0;
whismanoid 0:4d7de8b5c800 701 bool got_value = false;
whismanoid 0:4d7de8b5c800 702 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 703 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 704 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 705 {
whismanoid 0:4d7de8b5c800 706 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 707 break; // end of buffer
whismanoid 0:4d7de8b5c800 708 }
whismanoid 0:4d7de8b5c800 709 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 710 {
whismanoid 0:4d7de8b5c800 711 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 712 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 713 case '8': case '9':
whismanoid 0:4d7de8b5c800 714 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 715 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 716 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 717 got_value = true;
whismanoid 0:4d7de8b5c800 718 break;
whismanoid 0:4d7de8b5c800 719 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 720 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 721 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 722 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 723 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 724 got_value = true;
whismanoid 0:4d7de8b5c800 725 break;
whismanoid 0:4d7de8b5c800 726 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 727 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 728 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 729 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 730 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 731 got_value = true;
whismanoid 0:4d7de8b5c800 732 break;
whismanoid 0:4d7de8b5c800 733 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 734 temp_value = 0;
whismanoid 0:4d7de8b5c800 735 break;
whismanoid 0:4d7de8b5c800 736 case ' ': case ',':
whismanoid 0:4d7de8b5c800 737 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 738 {
whismanoid 0:4d7de8b5c800 739 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 740 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 741 temp_value = 0;
whismanoid 0:4d7de8b5c800 742 got_value = false;
whismanoid 0:4d7de8b5c800 743 }
whismanoid 0:4d7de8b5c800 744 break;
whismanoid 0:4d7de8b5c800 745 }
whismanoid 0:4d7de8b5c800 746 } // for idxSearch
whismanoid 0:4d7de8b5c800 747 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 748 {
whismanoid 0:4d7de8b5c800 749 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 750 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 751 temp_value = 0;
whismanoid 0:4d7de8b5c800 752 got_value = false;
whismanoid 0:4d7de8b5c800 753 }
whismanoid 0:4d7de8b5c800 754 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 755 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 756 }
whismanoid 0:4d7de8b5c800 757
whismanoid 0:4d7de8b5c800 758 /** CmdLine::parse_byteCount_byteList_dec matches a list of numbers, separated by spaces.
whismanoid 0:4d7de8b5c800 759 * The 0x / 0X prefix may be used to select hexadecimal instead of decimal.
whismanoid 0:4d7de8b5c800 760 *
whismanoid 0:4d7de8b5c800 761 * @return true if more than one number found in buffer
whismanoid 0:4d7de8b5c800 762 * @param[out] byteCount is populated with the number of numbers found
whismanoid 0:4d7de8b5c800 763 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the numbers found
whismanoid 0:4d7de8b5c800 764 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 765 *
whismanoid 0:4d7de8b5c800 766 */
whismanoid 0:4d7de8b5c800 767 bool CmdLine::parse_byteCount_byteList_dec(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 768 {
whismanoid 0:4d7de8b5c800 769 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 770 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 771 byteCount = 0;
whismanoid 0:4d7de8b5c800 772 bool got_value = false;
whismanoid 0:4d7de8b5c800 773 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 774 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 775 uint8_t radix = 10;
whismanoid 0:4d7de8b5c800 776 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 777 {
whismanoid 0:4d7de8b5c800 778 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 779 break; // end of buffer
whismanoid 0:4d7de8b5c800 780 }
whismanoid 0:4d7de8b5c800 781 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 782 {
whismanoid 0:4d7de8b5c800 783 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 784 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 785 case '8': case '9':
whismanoid 0:4d7de8b5c800 786 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 787 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 788 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 789 got_value = true;
whismanoid 0:4d7de8b5c800 790 break;
whismanoid 0:4d7de8b5c800 791 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 792 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 793 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 794 radix = 0x10;
whismanoid 0:4d7de8b5c800 795 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 796 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 797 got_value = true;
whismanoid 0:4d7de8b5c800 798 break;
whismanoid 0:4d7de8b5c800 799 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 800 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 801 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 802 radix = 0x10;
whismanoid 0:4d7de8b5c800 803 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 804 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 805 got_value = true;
whismanoid 0:4d7de8b5c800 806 break;
whismanoid 0:4d7de8b5c800 807 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 808 temp_value = 0;
whismanoid 0:4d7de8b5c800 809 radix = 0x10;
whismanoid 0:4d7de8b5c800 810 break;
whismanoid 0:4d7de8b5c800 811 case ' ': case ',':
whismanoid 0:4d7de8b5c800 812 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 813 {
whismanoid 0:4d7de8b5c800 814 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 815 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 816 temp_value = 0;
whismanoid 0:4d7de8b5c800 817 radix = 10;
whismanoid 0:4d7de8b5c800 818 got_value = false;
whismanoid 0:4d7de8b5c800 819 }
whismanoid 0:4d7de8b5c800 820 break;
whismanoid 0:4d7de8b5c800 821 }
whismanoid 0:4d7de8b5c800 822 } // for idxSearch
whismanoid 0:4d7de8b5c800 823 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 824 {
whismanoid 0:4d7de8b5c800 825 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 826 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 827 temp_value = 0;
whismanoid 0:4d7de8b5c800 828 got_value = false;
whismanoid 0:4d7de8b5c800 829 }
whismanoid 0:4d7de8b5c800 830 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 831 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 832 }
whismanoid 0:4d7de8b5c800 833
whismanoid 0:4d7de8b5c800 834
whismanoid 0:4d7de8b5c800 835 // End of file