editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Committer:
whismanoid
Date:
Mon Nov 25 02:20:43 2019 -0800
Revision:
11:e8a4162d4fd1
Parent:
10:3e2ff983be1c
Child:
12:447a747099e6
CmdLine parse_and_remove_key match_is_case_sensitive works now

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 10:3e2ff983be1c 46 chSeparator = 0;
whismanoid 10:3e2ff983be1c 47 match_is_case_sensitive = false;
whismanoid 0:4d7de8b5c800 48 }
whismanoid 0:4d7de8b5c800 49 /** CmdLine::clear empties the command-line buffer */
whismanoid 0:4d7de8b5c800 50 void CmdLine::clear(void)
whismanoid 0:4d7de8b5c800 51 {
whismanoid 0:4d7de8b5c800 52 indexOfNextEmptyCell = 0;
whismanoid 0:4d7de8b5c800 53 memset(buf, 0, COMMAND_BUFFER_LENGTH);
whismanoid 0:4d7de8b5c800 54 }
whismanoid 0:4d7de8b5c800 55 //void CmdLine::idleAppendIfReadable()
whismanoid 0:4d7de8b5c800 56 //{
whismanoid 0:4d7de8b5c800 57 // // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 58 // // Polymorphism fail: associatedSerialPort.readable()
whismanoid 0:4d7de8b5c800 59 // if (associatedSerialPort.readable()) {
whismanoid 0:4d7de8b5c800 60 // append(associatedSerialPort.getc());
whismanoid 0:4d7de8b5c800 61 // //
whismanoid 0:4d7de8b5c800 62 // // TODO1: set EOL timeout, so that we don't get lingering buffer cruft
whismanoid 0:4d7de8b5c800 63 // //
whismanoid 0:4d7de8b5c800 64 // }
whismanoid 0:4d7de8b5c800 65 //}
whismanoid 0:4d7de8b5c800 66 /** CmdLine::append handles an input character by appending the buffer,
whismanoid 0:4d7de8b5c800 67 * or handling an immediate function like backspace/delete
whismanoid 0:4d7de8b5c800 68 * or other custom immediate motor control functions.
whismanoid 0:4d7de8b5c800 69 */
whismanoid 0:4d7de8b5c800 70 void CmdLine::append(char ch)
whismanoid 0:4d7de8b5c800 71 {
whismanoid 6:88e92f832c9a 72 // void diagnostic_led_EOF();
whismanoid 0:4d7de8b5c800 73 void main_menu_status(CmdLine & cmdLine);
whismanoid 0:4d7de8b5c800 74
whismanoid 0:4d7de8b5c800 75 // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 76 switch (ch)
whismanoid 0:4d7de8b5c800 77 {
whismanoid 0:4d7de8b5c800 78 case '\b': // Unicode (U+0008) BS BACKSPACE as destructive backspace
whismanoid 0:4d7de8b5c800 79 case '\x7f': // Unicode (U+007F) DEL DELETE as destructive backspace
whismanoid 0:4d7de8b5c800 80 if (indexOfNextEmptyCell > 0)
whismanoid 0:4d7de8b5c800 81 {
whismanoid 0:4d7de8b5c800 82 buf[--indexOfNextEmptyCell] = '\0'; // pre-decrement index, overwrite with null
whismanoid 0:4d7de8b5c800 83 associatedSerialPort.printf("\b \b"); // tty: backspace, overwrite with space, backspace
whismanoid 0:4d7de8b5c800 84 }
whismanoid 0:4d7de8b5c800 85 break;
whismanoid 0:4d7de8b5c800 86 case '\r': // Unicode (U+000D) CR CARRIAGE RETURN(CR) as EOL end of line
whismanoid 0:4d7de8b5c800 87 case '\n': // Unicode (U+000A) LF LINE FEED(LF) as EOL end of line
whismanoid 0:4d7de8b5c800 88 //associatedSerialPort.printf("%c", ch); // echo line end
whismanoid 0:4d7de8b5c800 89 associatedSerialPort.printf("\r\n"); // echo line end
whismanoid 0:4d7de8b5c800 90 //~ associatedSerialPort.printf("\r\n~%s~\r\n", buf); // DIAGNOSTIC: print line buffer
whismanoid 0:4d7de8b5c800 91 // parse and handle the command by invoking onEOLcommandParser callback
whismanoid 0:4d7de8b5c800 92 if (onEOLcommandParser) {
whismanoid 0:4d7de8b5c800 93 onEOLcommandParser(*this);
whismanoid 0:4d7de8b5c800 94 }
whismanoid 0:4d7de8b5c800 95 clear();
whismanoid 0:4d7de8b5c800 96 break;
whismanoid 0:4d7de8b5c800 97 #define ECHO_EOF_IMMEDIATELY 1
whismanoid 0:4d7de8b5c800 98 #if ECHO_EOF_IMMEDIATELY
whismanoid 0:4d7de8b5c800 99 case '\x04': // Unicode (U+0004) EOT END OF TRANSMISSION = CTRL+D as EOF end of file
whismanoid 6:88e92f832c9a 100 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
whismanoid 0:4d7de8b5c800 101 //~ main_menu_status(*this);
whismanoid 0:4d7de8b5c800 102 associatedSerialPort.printf("** U+0004 EOT = EOF **"); // immediately echo EOF for test scripting
whismanoid 6:88e92f832c9a 103 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
whismanoid 0:4d7de8b5c800 104 associatedSerialPort.printf("\r\n\x04\r\n"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 105 //~ associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 106 //~ associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 107 //~ associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 108 clear(); // EOF discard any pending commands, to avoid surprise
whismanoid 0:4d7de8b5c800 109 break;
whismanoid 0:4d7de8b5c800 110 case '\x1a': // Unicode (U+001A) SUB SUBSTITUTE = CTRL+Z as EOF end of file
whismanoid 6:88e92f832c9a 111 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
whismanoid 0:4d7de8b5c800 112 //~ main_menu_status(*this);
whismanoid 0:4d7de8b5c800 113 associatedSerialPort.printf("** U+001A SUB = EOF **"); // immediately echo EOF for test scripting
whismanoid 6:88e92f832c9a 114 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
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 associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 118 associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 119 clear(); // EOF discard any pending commands, to avoid surprise
whismanoid 0:4d7de8b5c800 120 break;
whismanoid 0:4d7de8b5c800 121 #endif
whismanoid 0:4d7de8b5c800 122 //
whismanoid 0:4d7de8b5c800 123 // Support commands that get handled immediately w/o waiting for EOL
whismanoid 0:4d7de8b5c800 124 // Avoid using characters that may appear in other commands,
whismanoid 0:4d7de8b5c800 125 // such as 0-9 A-Z a-z and some punctuation %*+-./=
whismanoid 0:4d7de8b5c800 126 // so these 25 characters are available: !"#$&'(),:;<>?@[\]^_`{|}~
whismanoid 0:4d7de8b5c800 127 //case '!': // immediate command !) example
whismanoid 0:4d7de8b5c800 128 // do_some_immediate_action();
whismanoid 0:4d7de8b5c800 129 // clear();
whismanoid 0:4d7de8b5c800 130 // break;
whismanoid 0:4d7de8b5c800 131 //case ' ':
whismanoid 0:4d7de8b5c800 132 // on_immediate_0x20(); // Unicode (U+0020) SPACE
whismanoid 0:4d7de8b5c800 133 // break;
whismanoid 0:4d7de8b5c800 134 case '!':
whismanoid 1:5b33e7447601 135 //~ on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 1:5b33e7447601 136 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 137 if (on_immediate_0x21) {
whismanoid 1:5b33e7447601 138 on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 1:5b33e7447601 139 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 140 }
whismanoid 0:4d7de8b5c800 141 case '{':
whismanoid 1:5b33e7447601 142 //~ on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 1:5b33e7447601 143 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 144 if (on_immediate_0x7b) {
whismanoid 1:5b33e7447601 145 on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 1:5b33e7447601 146 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 147 }
whismanoid 0:4d7de8b5c800 148 case '}':
whismanoid 1:5b33e7447601 149 //~ on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 1:5b33e7447601 150 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 151 if (on_immediate_0x7d) {
whismanoid 1:5b33e7447601 152 on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 1:5b33e7447601 153 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 154 }
whismanoid 0:4d7de8b5c800 155 //
whismanoid 0:4d7de8b5c800 156 // default:
whismanoid 0:4d7de8b5c800 157 case '"':
whismanoid 1:5b33e7447601 158 if (on_immediate_0x22) {
whismanoid 1:5b33e7447601 159 on_immediate_0x22(); // Unicode (U+0022) " QUOTATION MARK
whismanoid 1:5b33e7447601 160 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 161 }
whismanoid 0:4d7de8b5c800 162 case '#':
whismanoid 1:5b33e7447601 163 if (on_immediate_0x23) {
whismanoid 1:5b33e7447601 164 on_immediate_0x23(); // Unicode (U+0023) # NUMBER SIGN = pound sign, hash, crosshatch
whismanoid 1:5b33e7447601 165 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 166 }
whismanoid 0:4d7de8b5c800 167 case '$':
whismanoid 1:5b33e7447601 168 if (on_immediate_0x24) {
whismanoid 1:5b33e7447601 169 on_immediate_0x24(); // Unicode (U+0024) $ DOLLAR SIGN
whismanoid 1:5b33e7447601 170 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 171 }
whismanoid 0:4d7de8b5c800 172 //~ on_immediate_0x24(); // Unicode (U+0024) $ DOLLAR SIGN
whismanoid 0:4d7de8b5c800 173 //~ break; // handled as immediate command, do not append to buffer
whismanoid 0:4d7de8b5c800 174 //case '%':
whismanoid 0:4d7de8b5c800 175 // on_immediate_0x25(); // Unicode (U+0025) % PERCENT SIGN
whismanoid 0:4d7de8b5c800 176 // break;
whismanoid 0:4d7de8b5c800 177 case '&':
whismanoid 1:5b33e7447601 178 if (on_immediate_0x26) {
whismanoid 1:5b33e7447601 179 on_immediate_0x26(); // Unicode (U+0026) & AMPERSAND
whismanoid 1:5b33e7447601 180 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 181 }
whismanoid 0:4d7de8b5c800 182 case '\'':
whismanoid 1:5b33e7447601 183 if (on_immediate_0x27) {
whismanoid 1:5b33e7447601 184 on_immediate_0x27(); // Unicode (U+0027) ' APOSTROPHE
whismanoid 1:5b33e7447601 185 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 186 }
whismanoid 0:4d7de8b5c800 187 case '(':
whismanoid 1:5b33e7447601 188 if (on_immediate_0x28) {
whismanoid 1:5b33e7447601 189 on_immediate_0x28(); // Unicode (U+0028) ( LEFT PARENTHESIS
whismanoid 1:5b33e7447601 190 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 191 }
whismanoid 0:4d7de8b5c800 192 case ')':
whismanoid 1:5b33e7447601 193 if (on_immediate_0x29) {
whismanoid 1:5b33e7447601 194 on_immediate_0x29(); // Unicode (U+0029) ) RIGHT PARENTHESIS
whismanoid 1:5b33e7447601 195 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 196 }
whismanoid 0:4d7de8b5c800 197 //case '*':
whismanoid 0:4d7de8b5c800 198 // on_immediate_0x2a(); // Unicode (U+002A) * ASTERISK
whismanoid 0:4d7de8b5c800 199 // break;
whismanoid 0:4d7de8b5c800 200 //case '+':
whismanoid 0:4d7de8b5c800 201 // on_immediate_0x2b(); // Unicode (U+002B) + PLUS SIGN
whismanoid 0:4d7de8b5c800 202 // break;
whismanoid 0:4d7de8b5c800 203 case ',':
whismanoid 1:5b33e7447601 204 if (on_immediate_0x2c) {
whismanoid 1:5b33e7447601 205 on_immediate_0x2c(); // Unicode (U+002C) , COMMA
whismanoid 1:5b33e7447601 206 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 207 }
whismanoid 0:4d7de8b5c800 208 //case '-':
whismanoid 0:4d7de8b5c800 209 // on_immediate_0x2d(); // Unicode (U+002D) - HYPHEN-MINUS
whismanoid 0:4d7de8b5c800 210 // break;
whismanoid 0:4d7de8b5c800 211 //case '.':
whismanoid 0:4d7de8b5c800 212 // on_immediate_0x2e(); // Unicode (U+002E) . FULL STOP
whismanoid 0:4d7de8b5c800 213 // break;
whismanoid 0:4d7de8b5c800 214 //case '/':
whismanoid 0:4d7de8b5c800 215 // on_immediate_0x2f(); // Unicode (U+002F) / SOLIDUS =SLASH
whismanoid 0:4d7de8b5c800 216 // break;
whismanoid 0:4d7de8b5c800 217 case ':':
whismanoid 1:5b33e7447601 218 if (on_immediate_0x3a) {
whismanoid 1:5b33e7447601 219 on_immediate_0x3a(); // Unicode (U+003A) : COLON
whismanoid 1:5b33e7447601 220 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 221 }
whismanoid 0:4d7de8b5c800 222 case ';':
whismanoid 1:5b33e7447601 223 if (on_immediate_0x3b) {
whismanoid 1:5b33e7447601 224 on_immediate_0x3b(); // Unicode (U+003B) ; SEMICOLON
whismanoid 1:5b33e7447601 225 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 226 }
whismanoid 0:4d7de8b5c800 227 case '<':
whismanoid 1:5b33e7447601 228 if (on_immediate_0x3c) {
whismanoid 1:5b33e7447601 229 on_immediate_0x3c(); // Unicode (U+003C) < LESS-THAN SIGN
whismanoid 1:5b33e7447601 230 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 231 }
whismanoid 0:4d7de8b5c800 232 //case '=':
whismanoid 0:4d7de8b5c800 233 // on_immediate_0x3d(); // Unicode (U+003D) = EQUALS SIGN
whismanoid 0:4d7de8b5c800 234 // break;
whismanoid 0:4d7de8b5c800 235 case '>':
whismanoid 1:5b33e7447601 236 if (on_immediate_0x3e) {
whismanoid 1:5b33e7447601 237 on_immediate_0x3e(); // Unicode (U+003E) > GREATER-THAN SIGN
whismanoid 1:5b33e7447601 238 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 239 }
whismanoid 0:4d7de8b5c800 240 case '?':
whismanoid 1:5b33e7447601 241 if (on_immediate_0x3f) {
whismanoid 1:5b33e7447601 242 on_immediate_0x3f(); // Unicode (U+003F) ? QUESTION MARK
whismanoid 1:5b33e7447601 243 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 244 }
whismanoid 0:4d7de8b5c800 245 case '@':
whismanoid 1:5b33e7447601 246 if (on_immediate_0x40) {
whismanoid 1:5b33e7447601 247 on_immediate_0x40(); // Unicode (U+0040) @ COMMERCIAL AT = at sign
whismanoid 1:5b33e7447601 248 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 249 }
whismanoid 0:4d7de8b5c800 250 case '[':
whismanoid 1:5b33e7447601 251 if (on_immediate_0x5b) {
whismanoid 1:5b33e7447601 252 on_immediate_0x5b(); // Unicode (U+005B) [ LEFT SQUARE BRACKET
whismanoid 1:5b33e7447601 253 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 254 }
whismanoid 0:4d7de8b5c800 255 case '\\':
whismanoid 1:5b33e7447601 256 if (on_immediate_0x5c) {
whismanoid 1:5b33e7447601 257 on_immediate_0x5c(); // Unicode (U+005C) \ REVERSE SOLIDUS
whismanoid 1:5b33e7447601 258 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 259 }
whismanoid 0:4d7de8b5c800 260 case ']':
whismanoid 1:5b33e7447601 261 if (on_immediate_0x5d) {
whismanoid 1:5b33e7447601 262 on_immediate_0x5d(); // Unicode (U+005D) ] RIGHT SQUARE BRACKET
whismanoid 1:5b33e7447601 263 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 264 }
whismanoid 0:4d7de8b5c800 265 case '^':
whismanoid 1:5b33e7447601 266 if (on_immediate_0x5e) {
whismanoid 1:5b33e7447601 267 on_immediate_0x5e(); // Unicode (U+005E) ^ CIRCUMFLEX ACCENT
whismanoid 1:5b33e7447601 268 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 269 }
whismanoid 0:4d7de8b5c800 270 case '_':
whismanoid 1:5b33e7447601 271 if (on_immediate_0x5f) {
whismanoid 1:5b33e7447601 272 on_immediate_0x5f(); // Unicode (U+005F) _ LOW LINE =SPACING UNDERSCORE
whismanoid 1:5b33e7447601 273 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 274 }
whismanoid 0:4d7de8b5c800 275 case '`':
whismanoid 1:5b33e7447601 276 if (on_immediate_0x60) {
whismanoid 1:5b33e7447601 277 on_immediate_0x60(); // Unicode (U+0060) ` GRAVE ACCENT (also called backtick)
whismanoid 1:5b33e7447601 278 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 279 }
whismanoid 0:4d7de8b5c800 280 case '|':
whismanoid 1:5b33e7447601 281 if (on_immediate_0x7c) {
whismanoid 1:5b33e7447601 282 on_immediate_0x7c(); // Unicode (U+007C) | VERTICAL LINE
whismanoid 1:5b33e7447601 283 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 284 }
whismanoid 0:4d7de8b5c800 285 case '~':
whismanoid 1:5b33e7447601 286 if (on_immediate_0x7e) {
whismanoid 1:5b33e7447601 287 on_immediate_0x7e(); // Unicode (U+007E) ~ TILDE
whismanoid 1:5b33e7447601 288 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 289 }
whismanoid 0:4d7de8b5c800 290 //
whismanoid 0:4d7de8b5c800 291 default:
whismanoid 0:4d7de8b5c800 292 MBED_ASSERT(indexOfNextEmptyCell <= COMMAND_BUFFER_LENGTH - 2);
whismanoid 0:4d7de8b5c800 293 buf[indexOfNextEmptyCell++] = ch; // append character, post-increment index
whismanoid 0:4d7de8b5c800 294 buf[indexOfNextEmptyCell] = '\0'; // null-terminate the buffer
whismanoid 0:4d7de8b5c800 295 MBED_ASSERT(indexOfNextEmptyCell <= COMMAND_BUFFER_LENGTH - 1);
whismanoid 0:4d7de8b5c800 296 associatedSerialPort.printf("%c", ch); // echo
whismanoid 0:4d7de8b5c800 297 if (indexOfNextEmptyCell == COMMAND_BUFFER_LENGTH - 1)
whismanoid 0:4d7de8b5c800 298 {
whismanoid 0:4d7de8b5c800 299 // buffer is full, parse what we've got
whismanoid 0:4d7de8b5c800 300 if (onEOLcommandParser) {
whismanoid 0:4d7de8b5c800 301 onEOLcommandParser(*this);
whismanoid 0:4d7de8b5c800 302 }
whismanoid 0:4d7de8b5c800 303 clear();
whismanoid 0:4d7de8b5c800 304 }
whismanoid 0:4d7de8b5c800 305 break;
whismanoid 0:4d7de8b5c800 306 }
whismanoid 0:4d7de8b5c800 307 }
whismanoid 1:5b33e7447601 308
whismanoid 0:4d7de8b5c800 309 /** CmdLine::parse_and_remove_key matches "key"
whismanoid 0:4d7de8b5c800 310 *
whismanoid 0:4d7de8b5c800 311 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 312 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 313 * @param[out] valueBuf buffer is populated with the substring between key= and the first space delimiter (or end of string)
whismanoid 0:4d7de8b5c800 314 * @param[in] valueBufLen limits the size of valueBuf
whismanoid 0:4d7de8b5c800 315 *
whismanoid 0:4d7de8b5c800 316 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 317 * @post chSeparator is populated with the separator character '=' or '?' following key=, or null character if no separator
whismanoid 0:4d7de8b5c800 318 *
whismanoid 0:4d7de8b5c800 319 */
whismanoid 0:4d7de8b5c800 320 bool CmdLine::parse_and_remove_key(const char *key, char *valueBuf, size_t valueBufLen)
whismanoid 0:4d7de8b5c800 321 {
whismanoid 0:4d7de8b5c800 322 // serial().printf("\r\n parse_and_remove_key(\"%s\")...", key);
whismanoid 0:4d7de8b5c800 323 // match key inside buf[]?
whismanoid 0:4d7de8b5c800 324 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 325 {
whismanoid 0:4d7de8b5c800 326 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 327 // serial().printf("\r\n parse_and_remove_key(\"%s\") no match", key);
whismanoid 0:4d7de8b5c800 328 return false; /* no match */
whismanoid 0:4d7de8b5c800 329 }
whismanoid 10:3e2ff983be1c 330 if (match_is_case_sensitive) {
whismanoid 11:e8a4162d4fd1 331 // case-sensitive string comparison
whismanoid 10:3e2ff983be1c 332 if (buf[idxSearch] != key[0]) { continue; }
whismanoid 10:3e2ff983be1c 333 }
whismanoid 10:3e2ff983be1c 334 else {
whismanoid 11:e8a4162d4fd1 335 // case-insensitive string comparison using toupper()
whismanoid 10:3e2ff983be1c 336 if (toupper(buf[idxSearch]) != toupper(key[0])) { continue; }
whismanoid 10:3e2ff983be1c 337 }
whismanoid 0:4d7de8b5c800 338 // possible match; compare buf[idxSearch..] to key[0..]
whismanoid 0:4d7de8b5c800 339 unsigned int idxKey = idxSearch; // test whether buf[idxKey..] == key[0..]
whismanoid 9:12e37800ecdd 340 unsigned int idxSeparator = idxSearch; // test whether key=value pair
whismanoid 0:4d7de8b5c800 341 unsigned int idxSpace = idxSearch; // end of key=value word
whismanoid 0:4d7de8b5c800 342 for (unsigned int offset = 0; offset < strlen(key); offset++)
whismanoid 0:4d7de8b5c800 343 {
whismanoid 11:e8a4162d4fd1 344 if (match_is_case_sensitive) {
whismanoid 11:e8a4162d4fd1 345 // case-sensitive string comparison
whismanoid 11:e8a4162d4fd1 346 if (buf[idxKey + offset] != key[offset]) { idxKey = 0; break; }
whismanoid 11:e8a4162d4fd1 347 }
whismanoid 11:e8a4162d4fd1 348 else {
whismanoid 11:e8a4162d4fd1 349 // case-insensitive string comparison using toupper()
whismanoid 11:e8a4162d4fd1 350 if (toupper(buf[idxKey + offset]) != toupper(key[offset])) { idxKey = 0; break; }
whismanoid 11:e8a4162d4fd1 351 }
whismanoid 9:12e37800ecdd 352 idxSpace = idxKey + offset + 1; // assume next char is a word break
whismanoid 9:12e37800ecdd 353 idxSeparator = idxKey + offset + 1; // assume next char is a separator
whismanoid 9:12e37800ecdd 354 if ((buf[idxSeparator] != '=') && (buf[idxSeparator] != '?')) { idxSeparator = 0; }
whismanoid 0:4d7de8b5c800 355 }
whismanoid 0:4d7de8b5c800 356 if (idxKey == 0) continue; // no match at idxSearch but keep searching
whismanoid 0:4d7de8b5c800 357 // ASSERT buf[idxKey..] == key[0..]
whismanoid 0:4d7de8b5c800 358 while ((buf[idxSpace] != ' ') && idxSpace < indexOfNextEmptyCell) { idxSpace++; }
whismanoid 0:4d7de8b5c800 359 // serial().printf("\r\n parse_and_remove_key(\"%s\") match at index %d length %d, '=' index %d, ' ' index %d",
whismanoid 9:12e37800ecdd 360 // key, idxKey, strlen(key), idxSeparator, idxSpace);
whismanoid 9:12e37800ecdd 361 if (idxSeparator != 0) {
whismanoid 9:12e37800ecdd 362 // found key=value: copy buf[idxSeparator+1..' '-1] into valueBuf[0..valueBufLen-1]
whismanoid 9:12e37800ecdd 363 chSeparator = buf[idxSeparator];
whismanoid 0:4d7de8b5c800 364 for (unsigned int offset = 0; offset < valueBufLen - 1; offset++)
whismanoid 0:4d7de8b5c800 365 {
whismanoid 9:12e37800ecdd 366 if (buf[idxSeparator + 1 + offset] == ' ') break;
whismanoid 9:12e37800ecdd 367 valueBuf[offset] = buf[idxSeparator + 1 + offset];
whismanoid 0:4d7de8b5c800 368 valueBuf[offset + 1] = '\0';
whismanoid 0:4d7de8b5c800 369 }
whismanoid 0:4d7de8b5c800 370 } else {
whismanoid 0:4d7de8b5c800 371 // found key but no =value: valueBuf[] = ""
whismanoid 9:12e37800ecdd 372 chSeparator = '\0';
whismanoid 0:4d7de8b5c800 373 valueBuf[0] = '\0';
whismanoid 0:4d7de8b5c800 374 }
whismanoid 0:4d7de8b5c800 375 // on successful match, the key=value should be deleted from cmdbuf
whismanoid 0:4d7de8b5c800 376 //serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" before deleting key",
whismanoid 0:4d7de8b5c800 377 // key, buf, valueBuf);
whismanoid 0:4d7de8b5c800 378 for (unsigned int offset = 0; offset < indexOfNextEmptyCell; offset++)
whismanoid 0:4d7de8b5c800 379 {
whismanoid 0:4d7de8b5c800 380 unsigned int idxCopyDst = idxKey + offset;
whismanoid 0:4d7de8b5c800 381 unsigned int idxCopySrc = idxSpace + 1 + offset;
whismanoid 0:4d7de8b5c800 382 if (idxCopyDst > indexOfNextEmptyCell) break;
whismanoid 0:4d7de8b5c800 383 if (idxCopySrc > indexOfNextEmptyCell) break;
whismanoid 0:4d7de8b5c800 384 buf[idxCopyDst] = buf[idxCopySrc];
whismanoid 0:4d7de8b5c800 385 }
whismanoid 0:4d7de8b5c800 386 // serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" after deleting key",
whismanoid 0:4d7de8b5c800 387 // key, buf, valueBuf);
whismanoid 0:4d7de8b5c800 388 // serial().printf("\r\n parse_and_remove_key(\"%s\") returning true: valueBuf=\"%s\"", key, valueBuf);
whismanoid 0:4d7de8b5c800 389 return true;
whismanoid 0:4d7de8b5c800 390 }
whismanoid 0:4d7de8b5c800 391 // serial().printf("\r\n parse_and_remove_key(\"%s\") no match", key);
whismanoid 0:4d7de8b5c800 392 return false; // no match
whismanoid 0:4d7de8b5c800 393 }
whismanoid 0:4d7de8b5c800 394
whismanoid 0:4d7de8b5c800 395 /** CmdLine::parse_frequency_Hz matches "key"=digits
whismanoid 0:4d7de8b5c800 396 *
whismanoid 0:4d7de8b5c800 397 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 398 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 399 * @param[out] frequency_Hz updated from value string if match "key"=value,
whismanoid 0:4d7de8b5c800 400 * optional suffix kHz or MHz scales the value by 1000 or 10000000
whismanoid 0:4d7de8b5c800 401 *
whismanoid 0:4d7de8b5c800 402 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 403 *
whismanoid 0:4d7de8b5c800 404 */
whismanoid 0:4d7de8b5c800 405 bool CmdLine::parse_frequency_Hz(const char *key, uint32_t& frequency_Hz)
whismanoid 0:4d7de8b5c800 406 {
whismanoid 0:4d7de8b5c800 407 char valueBuf[16];
whismanoid 0:4d7de8b5c800 408 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 409 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 410 {
whismanoid 0:4d7de8b5c800 411 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 412 // parse cmdLine arg (SCLK=\d+(kHZ|MHZ)?)? --> g_SPI_SCLK_Hz
whismanoid 0:4d7de8b5c800 413 frequency_Hz = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 414 if (strstr(valueBuf, "M")) {
whismanoid 0:4d7de8b5c800 415 frequency_Hz = frequency_Hz * 1000000;
whismanoid 0:4d7de8b5c800 416 }
whismanoid 0:4d7de8b5c800 417 if (strstr(valueBuf, "k")) {
whismanoid 0:4d7de8b5c800 418 frequency_Hz = frequency_Hz * 1000;
whismanoid 0:4d7de8b5c800 419 }
whismanoid 0:4d7de8b5c800 420 return true;
whismanoid 0:4d7de8b5c800 421 }
whismanoid 0:4d7de8b5c800 422 return false; // no match
whismanoid 0:4d7de8b5c800 423 }
whismanoid 0:4d7de8b5c800 424
whismanoid 4:700b71cd3bd2 425 /** CmdLine::parse_interval_usec matches "key"=digits
whismanoid 4:700b71cd3bd2 426 *
whismanoid 4:700b71cd3bd2 427 * @return true if keyword was found in buffer
whismanoid 4:700b71cd3bd2 428 * @param[in] key string value to match
whismanoid 4:700b71cd3bd2 429 * @param[out] interval_usec updated from value string if match "key"=value,
whismanoid 4:700b71cd3bd2 430 * optional suffix Hz kHz or MHz 1/x inverts and scales the value
whismanoid 4:700b71cd3bd2 431 * optional suffix s or ms or msec or us or usec scales the value
whismanoid 4:700b71cd3bd2 432 *
whismanoid 4:700b71cd3bd2 433 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 4:700b71cd3bd2 434 *
whismanoid 4:700b71cd3bd2 435 */
whismanoid 4:700b71cd3bd2 436 bool CmdLine::parse_interval_usec(const char *key, us_timestamp_t& interval_usec)
whismanoid 4:700b71cd3bd2 437 {
whismanoid 4:700b71cd3bd2 438 char valueBuf[32];
whismanoid 4:700b71cd3bd2 439 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 4:700b71cd3bd2 440 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 4:700b71cd3bd2 441 {
whismanoid 4:700b71cd3bd2 442 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 4:700b71cd3bd2 443 interval_usec = strtoul(valueBuf, NULL, 10);
whismanoid 4:700b71cd3bd2 444 if (strstr(valueBuf, "ms")) {
whismanoid 4:700b71cd3bd2 445 interval_usec = interval_usec * 1000;
whismanoid 5:1a14de1c4d7b 446 } else if (strstr(valueBuf, "us")) {
whismanoid 5:1a14de1c4d7b 447 // avoid matching "s" which is a subset of "us"
whismanoid 5:1a14de1c4d7b 448 // interval_usec = interval_usec * 1;
whismanoid 4:700b71cd3bd2 449 } else if (strstr(valueBuf, "s")) {
whismanoid 4:700b71cd3bd2 450 interval_usec = interval_usec * 1000000;
whismanoid 4:700b71cd3bd2 451 } else if (strstr(valueBuf, "MHz")) {
whismanoid 4:700b71cd3bd2 452 interval_usec = 1. / interval_usec;
whismanoid 4:700b71cd3bd2 453 } else if (strstr(valueBuf, "kHz")) {
whismanoid 4:700b71cd3bd2 454 interval_usec = 1000. / interval_usec;
whismanoid 4:700b71cd3bd2 455 } else if (strstr(valueBuf, "Hz")) {
whismanoid 4:700b71cd3bd2 456 interval_usec = 1000000. / interval_usec;
whismanoid 4:700b71cd3bd2 457 }
whismanoid 4:700b71cd3bd2 458 return true;
whismanoid 4:700b71cd3bd2 459 }
whismanoid 4:700b71cd3bd2 460 return false; // no match
whismanoid 4:700b71cd3bd2 461 }
whismanoid 4:700b71cd3bd2 462
whismanoid 0:4d7de8b5c800 463 /** CmdLine::parse_flag matches "key"=0 or 1
whismanoid 0:4d7de8b5c800 464 *
whismanoid 0:4d7de8b5c800 465 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 466 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 467 * @param[out] nFlagVar variable to be updated by setting or clearing bits specified by nFlagBitMask.
whismanoid 0:4d7de8b5c800 468 * If match "key"=0 then the nFlagBitMask bits in nFlagVar are cleared.
whismanoid 0:4d7de8b5c800 469 * If match "key"=1 then the nFlagBitMask bits in nFlagVar are set.
whismanoid 0:4d7de8b5c800 470 * @param[in] nFlagBitMask bit mask contains binary 1 in the bit to be controlled by the key=value setting
whismanoid 0:4d7de8b5c800 471 *
whismanoid 0:4d7de8b5c800 472 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 473 *
whismanoid 0:4d7de8b5c800 474 */
whismanoid 0:4d7de8b5c800 475 bool CmdLine::parse_flag(const char *key, uint8_t& nFlagVar, const uint8_t nFlagBitMask)
whismanoid 0:4d7de8b5c800 476 {
whismanoid 0:4d7de8b5c800 477 char valueBuf[16];
whismanoid 0:4d7de8b5c800 478 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 479 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 480 {
whismanoid 0:4d7de8b5c800 481 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 482 // parse cmdLine arg (CPHA=\d)? --> g_SPI_dataMode | SPI_MODE1
whismanoid 0:4d7de8b5c800 483 int x = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 484 if (x)
whismanoid 0:4d7de8b5c800 485 {
whismanoid 0:4d7de8b5c800 486 nFlagVar |= nFlagBitMask;
whismanoid 0:4d7de8b5c800 487 }
whismanoid 0:4d7de8b5c800 488 else
whismanoid 0:4d7de8b5c800 489 {
whismanoid 0:4d7de8b5c800 490 nFlagVar &= ~nFlagBitMask;
whismanoid 0:4d7de8b5c800 491 }
whismanoid 0:4d7de8b5c800 492 return true;
whismanoid 0:4d7de8b5c800 493 }
whismanoid 0:4d7de8b5c800 494 return false; // no match
whismanoid 0:4d7de8b5c800 495 }
whismanoid 0:4d7de8b5c800 496
whismanoid 0:4d7de8b5c800 497 /** CmdLine::parse_byte_hex matches "key"=value
whismanoid 0:4d7de8b5c800 498 *
whismanoid 0:4d7de8b5c800 499 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 500 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 501 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 502 *
whismanoid 0:4d7de8b5c800 503 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 504 *
whismanoid 2:0f702da53f2a 505 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 506 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 507 */
whismanoid 0:4d7de8b5c800 508 bool CmdLine::parse_byte_hex(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 509 {
whismanoid 0:4d7de8b5c800 510 char valueBuf[16];
whismanoid 0:4d7de8b5c800 511 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 512 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 513 {
whismanoid 0:4d7de8b5c800 514 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 515 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 516 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 517 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 518 {
whismanoid 0:4d7de8b5c800 519 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 520 return true;
whismanoid 0:4d7de8b5c800 521 }
whismanoid 0:4d7de8b5c800 522 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 523 {
whismanoid 0:4d7de8b5c800 524 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 525 return true;
whismanoid 0:4d7de8b5c800 526 }
whismanoid 2:0f702da53f2a 527 nByteVar = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 0:4d7de8b5c800 528 return true;
whismanoid 0:4d7de8b5c800 529 }
whismanoid 0:4d7de8b5c800 530 return false; // no match
whismanoid 0:4d7de8b5c800 531 }
whismanoid 0:4d7de8b5c800 532
whismanoid 0:4d7de8b5c800 533 /** CmdLine::parse_byte_dec matches "key"=value
whismanoid 0:4d7de8b5c800 534 *
whismanoid 0:4d7de8b5c800 535 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 536 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 537 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 538 *
whismanoid 0:4d7de8b5c800 539 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 540 *
whismanoid 2:0f702da53f2a 541 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 542 * Default number conversion radix is base-10 decimal.
whismanoid 0:4d7de8b5c800 543 */
whismanoid 0:4d7de8b5c800 544 bool CmdLine::parse_byte_dec(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 545 {
whismanoid 0:4d7de8b5c800 546 char valueBuf[16];
whismanoid 0:4d7de8b5c800 547 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 548 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 549 {
whismanoid 0:4d7de8b5c800 550 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 551 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 552 // TODO1: parse_byte_dec take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 553 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 554 {
whismanoid 0:4d7de8b5c800 555 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 556 return true;
whismanoid 0:4d7de8b5c800 557 }
whismanoid 0:4d7de8b5c800 558 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 559 {
whismanoid 0:4d7de8b5c800 560 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 561 return true;
whismanoid 0:4d7de8b5c800 562 }
whismanoid 2:0f702da53f2a 563 nByteVar = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 564 return true;
whismanoid 0:4d7de8b5c800 565 }
whismanoid 0:4d7de8b5c800 566 return false; // no match
whismanoid 0:4d7de8b5c800 567 }
whismanoid 0:4d7de8b5c800 568
whismanoid 0:4d7de8b5c800 569 /** CmdLine::parse_uint16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 570 *
whismanoid 0:4d7de8b5c800 571 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 572 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 573 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 574 *
whismanoid 0:4d7de8b5c800 575 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 576 *
whismanoid 2:0f702da53f2a 577 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 578 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 579 */
whismanoid 0:4d7de8b5c800 580 bool CmdLine::parse_uint16_hex(const char *key, uint16_t& uint16Var)
whismanoid 0:4d7de8b5c800 581 {
whismanoid 0:4d7de8b5c800 582 char valueBuf[16];
whismanoid 0:4d7de8b5c800 583 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 584 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 585 {
whismanoid 0:4d7de8b5c800 586 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 587 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 588 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 589 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 590 {
whismanoid 0:4d7de8b5c800 591 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 592 return true;
whismanoid 0:4d7de8b5c800 593 }
whismanoid 0:4d7de8b5c800 594 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 595 {
whismanoid 0:4d7de8b5c800 596 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 597 return true;
whismanoid 0:4d7de8b5c800 598 }
whismanoid 2:0f702da53f2a 599 uint16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 600 return true;
whismanoid 2:0f702da53f2a 601 }
whismanoid 2:0f702da53f2a 602 return false; // no match
whismanoid 2:0f702da53f2a 603 }
whismanoid 2:0f702da53f2a 604
whismanoid 2:0f702da53f2a 605 /** CmdLine::parse_uint16_dec matches "key"=value
whismanoid 2:0f702da53f2a 606 *
whismanoid 2:0f702da53f2a 607 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 608 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 609 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 610 *
whismanoid 2:0f702da53f2a 611 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 612 *
whismanoid 2:0f702da53f2a 613 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 614 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 615 */
whismanoid 2:0f702da53f2a 616 bool CmdLine::parse_uint16_dec(const char *key, uint16_t& uint16Var)
whismanoid 2:0f702da53f2a 617 {
whismanoid 2:0f702da53f2a 618 char valueBuf[16];
whismanoid 2:0f702da53f2a 619 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 620 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 621 {
whismanoid 2:0f702da53f2a 622 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 623 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 624 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 625 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 626 {
whismanoid 2:0f702da53f2a 627 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 628 return true;
whismanoid 2:0f702da53f2a 629 }
whismanoid 2:0f702da53f2a 630 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 631 {
whismanoid 2:0f702da53f2a 632 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 633 return true;
whismanoid 2:0f702da53f2a 634 }
whismanoid 2:0f702da53f2a 635 uint16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 636 return true;
whismanoid 0:4d7de8b5c800 637 }
whismanoid 0:4d7de8b5c800 638 return false; // no match
whismanoid 0:4d7de8b5c800 639 }
whismanoid 0:4d7de8b5c800 640
whismanoid 0:4d7de8b5c800 641 /** CmdLine::parse_int16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 642 *
whismanoid 0:4d7de8b5c800 643 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 644 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 645 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 646 *
whismanoid 0:4d7de8b5c800 647 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 648 *
whismanoid 2:0f702da53f2a 649 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 650 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 651 */
whismanoid 0:4d7de8b5c800 652 bool CmdLine::parse_int16_hex(const char *key, int16_t& int16Var)
whismanoid 0:4d7de8b5c800 653 {
whismanoid 0:4d7de8b5c800 654 char valueBuf[16];
whismanoid 0:4d7de8b5c800 655 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 656 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 657 {
whismanoid 0:4d7de8b5c800 658 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 659 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 660 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 661 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 662 {
whismanoid 0:4d7de8b5c800 663 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 664 return true;
whismanoid 0:4d7de8b5c800 665 }
whismanoid 0:4d7de8b5c800 666 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 667 {
whismanoid 0:4d7de8b5c800 668 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 669 return true;
whismanoid 0:4d7de8b5c800 670 }
whismanoid 2:0f702da53f2a 671 int16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 672 return true;
whismanoid 2:0f702da53f2a 673 }
whismanoid 2:0f702da53f2a 674 return false; // no match
whismanoid 2:0f702da53f2a 675 }
whismanoid 2:0f702da53f2a 676
whismanoid 2:0f702da53f2a 677 /** CmdLine::parse_int16_dec matches "key"=value
whismanoid 2:0f702da53f2a 678 *
whismanoid 2:0f702da53f2a 679 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 680 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 681 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 682 *
whismanoid 2:0f702da53f2a 683 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 684 *
whismanoid 2:0f702da53f2a 685 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 686 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 687 */
whismanoid 2:0f702da53f2a 688 bool CmdLine::parse_int16_dec(const char *key, int16_t& int16Var)
whismanoid 2:0f702da53f2a 689 {
whismanoid 2:0f702da53f2a 690 char valueBuf[16];
whismanoid 2:0f702da53f2a 691 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 692 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 693 {
whismanoid 2:0f702da53f2a 694 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 695 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 696 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 697 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 698 {
whismanoid 2:0f702da53f2a 699 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 700 return true;
whismanoid 2:0f702da53f2a 701 }
whismanoid 2:0f702da53f2a 702 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 703 {
whismanoid 2:0f702da53f2a 704 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 705 return true;
whismanoid 2:0f702da53f2a 706 }
whismanoid 2:0f702da53f2a 707 int16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 708 return true;
whismanoid 0:4d7de8b5c800 709 }
whismanoid 0:4d7de8b5c800 710 return false; // no match
whismanoid 0:4d7de8b5c800 711 }
whismanoid 0:4d7de8b5c800 712
whismanoid 9:12e37800ecdd 713 /** CmdLine::parse_uint32_hex matches "key"=value
whismanoid 9:12e37800ecdd 714 *
whismanoid 9:12e37800ecdd 715 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 716 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 717 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 718 *
whismanoid 9:12e37800ecdd 719 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 720 *
whismanoid 9:12e37800ecdd 721 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 722 * Default number conversion radix is base-16 hexadecimal.
whismanoid 9:12e37800ecdd 723 */
whismanoid 9:12e37800ecdd 724 bool CmdLine::parse_uint32_hex(const char *key, uint32_t& uint32Var)
whismanoid 9:12e37800ecdd 725 {
whismanoid 9:12e37800ecdd 726 char valueBuf[16];
whismanoid 9:12e37800ecdd 727 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 728 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 729 {
whismanoid 9:12e37800ecdd 730 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 731 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 732 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 733 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 734 {
whismanoid 9:12e37800ecdd 735 uint32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 736 return true;
whismanoid 9:12e37800ecdd 737 }
whismanoid 9:12e37800ecdd 738 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 739 {
whismanoid 9:12e37800ecdd 740 uint32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 741 return true;
whismanoid 9:12e37800ecdd 742 }
whismanoid 9:12e37800ecdd 743 uint32Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 9:12e37800ecdd 744 return true;
whismanoid 9:12e37800ecdd 745 }
whismanoid 9:12e37800ecdd 746 return false; // no match
whismanoid 9:12e37800ecdd 747 }
whismanoid 9:12e37800ecdd 748
whismanoid 9:12e37800ecdd 749 /** CmdLine::parse_uint32_dec matches "key"=value
whismanoid 9:12e37800ecdd 750 *
whismanoid 9:12e37800ecdd 751 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 752 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 753 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 754 *
whismanoid 9:12e37800ecdd 755 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 756 *
whismanoid 9:12e37800ecdd 757 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 758 * Default number conversion radix is base-10 decimal.
whismanoid 9:12e37800ecdd 759 */
whismanoid 9:12e37800ecdd 760 bool CmdLine::parse_uint32_dec(const char *key, uint32_t& uint32Var)
whismanoid 9:12e37800ecdd 761 {
whismanoid 9:12e37800ecdd 762 char valueBuf[16];
whismanoid 9:12e37800ecdd 763 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 764 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 765 {
whismanoid 9:12e37800ecdd 766 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 767 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 768 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 769 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 770 {
whismanoid 9:12e37800ecdd 771 uint32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 772 return true;
whismanoid 9:12e37800ecdd 773 }
whismanoid 9:12e37800ecdd 774 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 775 {
whismanoid 9:12e37800ecdd 776 uint32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 777 return true;
whismanoid 9:12e37800ecdd 778 }
whismanoid 9:12e37800ecdd 779 uint32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 9:12e37800ecdd 780 return true;
whismanoid 9:12e37800ecdd 781 }
whismanoid 9:12e37800ecdd 782 return false; // no match
whismanoid 9:12e37800ecdd 783 }
whismanoid 9:12e37800ecdd 784
whismanoid 9:12e37800ecdd 785 /** CmdLine::parse_int32_hex matches "key"=value
whismanoid 9:12e37800ecdd 786 *
whismanoid 9:12e37800ecdd 787 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 788 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 789 * @param[out] int32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 790 *
whismanoid 9:12e37800ecdd 791 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 792 *
whismanoid 9:12e37800ecdd 793 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 794 * Default number conversion radix is base-16 hexadecimal.
whismanoid 9:12e37800ecdd 795 */
whismanoid 9:12e37800ecdd 796 bool CmdLine::parse_int32_hex(const char *key, int32_t& int32Var)
whismanoid 9:12e37800ecdd 797 {
whismanoid 9:12e37800ecdd 798 char valueBuf[16];
whismanoid 9:12e37800ecdd 799 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 800 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 801 {
whismanoid 9:12e37800ecdd 802 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 803 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 804 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 805 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 806 {
whismanoid 9:12e37800ecdd 807 int32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 808 return true;
whismanoid 9:12e37800ecdd 809 }
whismanoid 9:12e37800ecdd 810 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 811 {
whismanoid 9:12e37800ecdd 812 int32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 813 return true;
whismanoid 9:12e37800ecdd 814 }
whismanoid 9:12e37800ecdd 815 int32Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 9:12e37800ecdd 816 return true;
whismanoid 9:12e37800ecdd 817 }
whismanoid 9:12e37800ecdd 818 return false; // no match
whismanoid 9:12e37800ecdd 819 }
whismanoid 9:12e37800ecdd 820
whismanoid 9:12e37800ecdd 821 /** CmdLine::parse_int32_dec matches "key"=value
whismanoid 9:12e37800ecdd 822 *
whismanoid 9:12e37800ecdd 823 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 824 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 825 * @param[out] int32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 826 *
whismanoid 9:12e37800ecdd 827 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 828 *
whismanoid 9:12e37800ecdd 829 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 830 * Default number conversion radix is base-10 decimal.
whismanoid 9:12e37800ecdd 831 */
whismanoid 9:12e37800ecdd 832 bool CmdLine::parse_int32_dec(const char *key, int32_t& int32Var)
whismanoid 9:12e37800ecdd 833 {
whismanoid 9:12e37800ecdd 834 char valueBuf[16];
whismanoid 9:12e37800ecdd 835 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 836 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 837 {
whismanoid 9:12e37800ecdd 838 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 839 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 840 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 841 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 842 {
whismanoid 9:12e37800ecdd 843 int32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 844 return true;
whismanoid 9:12e37800ecdd 845 }
whismanoid 9:12e37800ecdd 846 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 847 {
whismanoid 9:12e37800ecdd 848 int32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 849 return true;
whismanoid 9:12e37800ecdd 850 }
whismanoid 9:12e37800ecdd 851 int32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 9:12e37800ecdd 852 return true;
whismanoid 9:12e37800ecdd 853 }
whismanoid 9:12e37800ecdd 854 return false; // no match
whismanoid 9:12e37800ecdd 855 }
whismanoid 9:12e37800ecdd 856
whismanoid 7:0bda7cfee767 857 /** CmdLine::parse_double matches "key"=value
whismanoid 7:0bda7cfee767 858 *
whismanoid 7:0bda7cfee767 859 * @return true if keyword was found in buffer
whismanoid 7:0bda7cfee767 860 * @param[in] key string value to match
whismanoid 7:0bda7cfee767 861 * @param[out] doubleVar updated from value string if match "key"=value
whismanoid 7:0bda7cfee767 862 *
whismanoid 7:0bda7cfee767 863 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 7:0bda7cfee767 864 *
whismanoid 7:0bda7cfee767 865 */
whismanoid 7:0bda7cfee767 866 bool CmdLine::parse_double(const char *key, double& doubleVar)
whismanoid 7:0bda7cfee767 867 {
whismanoid 7:0bda7cfee767 868 char valueBuf[16];
whismanoid 7:0bda7cfee767 869 char *end;
whismanoid 7:0bda7cfee767 870 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 7:0bda7cfee767 871 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 7:0bda7cfee767 872 {
whismanoid 7:0bda7cfee767 873 doubleVar = strtof(valueBuf, &end);
whismanoid 7:0bda7cfee767 874 return true;
whismanoid 7:0bda7cfee767 875 }
whismanoid 7:0bda7cfee767 876 return false; // no match
whismanoid 7:0bda7cfee767 877 }
whismanoid 7:0bda7cfee767 878
whismanoid 0:4d7de8b5c800 879 /** CmdLine::parse_float matches "key"=value
whismanoid 0:4d7de8b5c800 880 *
whismanoid 0:4d7de8b5c800 881 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 882 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 883 * @param[out] floatVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 884 *
whismanoid 0:4d7de8b5c800 885 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 886 *
whismanoid 0:4d7de8b5c800 887 */
whismanoid 0:4d7de8b5c800 888 bool CmdLine::parse_float(const char *key, float& floatVar)
whismanoid 0:4d7de8b5c800 889 {
whismanoid 0:4d7de8b5c800 890 char valueBuf[16];
whismanoid 0:4d7de8b5c800 891 char *end;
whismanoid 0:4d7de8b5c800 892 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 893 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 894 {
whismanoid 0:4d7de8b5c800 895 floatVar = strtof(valueBuf, &end);
whismanoid 0:4d7de8b5c800 896 return true;
whismanoid 0:4d7de8b5c800 897 }
whismanoid 0:4d7de8b5c800 898 return false; // no match
whismanoid 0:4d7de8b5c800 899 }
whismanoid 0:4d7de8b5c800 900
whismanoid 0:4d7de8b5c800 901 /** CmdLine::parse_byteCount_byteList_hex matches hexadecimal digits separated by spaces.
whismanoid 0:4d7de8b5c800 902 * The 0x / 0X prefix is optional. The numbers must be hexadecimal.
whismanoid 0:4d7de8b5c800 903 *
whismanoid 0:4d7de8b5c800 904 * @return true if more than one hex byte found in buffer
whismanoid 0:4d7de8b5c800 905 * @param[out] byteCount is populated with the number of hex bytes found
whismanoid 0:4d7de8b5c800 906 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the hex bytes found
whismanoid 0:4d7de8b5c800 907 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 908 *
whismanoid 0:4d7de8b5c800 909 */
whismanoid 0:4d7de8b5c800 910 bool CmdLine::parse_byteCount_byteList_hex(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 911 {
whismanoid 0:4d7de8b5c800 912 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 913 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 914 byteCount = 0;
whismanoid 0:4d7de8b5c800 915 bool got_value = false;
whismanoid 0:4d7de8b5c800 916 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 917 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 918 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 919 {
whismanoid 0:4d7de8b5c800 920 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 921 break; // end of buffer
whismanoid 0:4d7de8b5c800 922 }
whismanoid 0:4d7de8b5c800 923 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 924 {
whismanoid 0:4d7de8b5c800 925 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 926 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 927 case '8': case '9':
whismanoid 0:4d7de8b5c800 928 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 929 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 930 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 931 got_value = true;
whismanoid 0:4d7de8b5c800 932 break;
whismanoid 0:4d7de8b5c800 933 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 934 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 935 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 936 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 937 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 938 got_value = true;
whismanoid 0:4d7de8b5c800 939 break;
whismanoid 0:4d7de8b5c800 940 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 941 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 942 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 943 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 944 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 945 got_value = true;
whismanoid 0:4d7de8b5c800 946 break;
whismanoid 0:4d7de8b5c800 947 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 948 temp_value = 0;
whismanoid 0:4d7de8b5c800 949 break;
whismanoid 0:4d7de8b5c800 950 case ' ': case ',':
whismanoid 0:4d7de8b5c800 951 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 952 {
whismanoid 0:4d7de8b5c800 953 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 954 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 955 temp_value = 0;
whismanoid 0:4d7de8b5c800 956 got_value = false;
whismanoid 0:4d7de8b5c800 957 }
whismanoid 0:4d7de8b5c800 958 break;
whismanoid 0:4d7de8b5c800 959 }
whismanoid 0:4d7de8b5c800 960 } // for idxSearch
whismanoid 0:4d7de8b5c800 961 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 962 {
whismanoid 0:4d7de8b5c800 963 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 964 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 965 temp_value = 0;
whismanoid 0:4d7de8b5c800 966 got_value = false;
whismanoid 0:4d7de8b5c800 967 }
whismanoid 0:4d7de8b5c800 968 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 969 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 970 }
whismanoid 0:4d7de8b5c800 971
whismanoid 0:4d7de8b5c800 972 /** CmdLine::parse_byteCount_byteList_dec matches a list of numbers, separated by spaces.
whismanoid 0:4d7de8b5c800 973 * The 0x / 0X prefix may be used to select hexadecimal instead of decimal.
whismanoid 0:4d7de8b5c800 974 *
whismanoid 0:4d7de8b5c800 975 * @return true if more than one number found in buffer
whismanoid 0:4d7de8b5c800 976 * @param[out] byteCount is populated with the number of numbers found
whismanoid 0:4d7de8b5c800 977 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the numbers found
whismanoid 0:4d7de8b5c800 978 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 979 *
whismanoid 0:4d7de8b5c800 980 */
whismanoid 0:4d7de8b5c800 981 bool CmdLine::parse_byteCount_byteList_dec(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 982 {
whismanoid 0:4d7de8b5c800 983 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 984 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 985 byteCount = 0;
whismanoid 0:4d7de8b5c800 986 bool got_value = false;
whismanoid 0:4d7de8b5c800 987 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 988 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 989 uint8_t radix = 10;
whismanoid 0:4d7de8b5c800 990 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 991 {
whismanoid 0:4d7de8b5c800 992 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 993 break; // end of buffer
whismanoid 0:4d7de8b5c800 994 }
whismanoid 0:4d7de8b5c800 995 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 996 {
whismanoid 0:4d7de8b5c800 997 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 998 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 999 case '8': case '9':
whismanoid 0:4d7de8b5c800 1000 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 1001 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1002 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1003 got_value = true;
whismanoid 0:4d7de8b5c800 1004 break;
whismanoid 0:4d7de8b5c800 1005 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 1006 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 1007 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 1008 radix = 0x10;
whismanoid 0:4d7de8b5c800 1009 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1010 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1011 got_value = true;
whismanoid 0:4d7de8b5c800 1012 break;
whismanoid 0:4d7de8b5c800 1013 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 1014 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 1015 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 1016 radix = 0x10;
whismanoid 0:4d7de8b5c800 1017 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1018 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1019 got_value = true;
whismanoid 0:4d7de8b5c800 1020 break;
whismanoid 0:4d7de8b5c800 1021 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 1022 temp_value = 0;
whismanoid 0:4d7de8b5c800 1023 radix = 0x10;
whismanoid 0:4d7de8b5c800 1024 break;
whismanoid 0:4d7de8b5c800 1025 case ' ': case ',':
whismanoid 0:4d7de8b5c800 1026 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1027 {
whismanoid 0:4d7de8b5c800 1028 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1029 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1030 temp_value = 0;
whismanoid 0:4d7de8b5c800 1031 radix = 10;
whismanoid 0:4d7de8b5c800 1032 got_value = false;
whismanoid 0:4d7de8b5c800 1033 }
whismanoid 0:4d7de8b5c800 1034 break;
whismanoid 0:4d7de8b5c800 1035 }
whismanoid 0:4d7de8b5c800 1036 } // for idxSearch
whismanoid 0:4d7de8b5c800 1037 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1038 {
whismanoid 0:4d7de8b5c800 1039 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1040 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1041 temp_value = 0;
whismanoid 0:4d7de8b5c800 1042 got_value = false;
whismanoid 0:4d7de8b5c800 1043 }
whismanoid 0:4d7de8b5c800 1044 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 1045 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 1046 }
whismanoid 0:4d7de8b5c800 1047
whismanoid 0:4d7de8b5c800 1048
whismanoid 0:4d7de8b5c800 1049 // End of file