editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Committer:
whismanoid
Date:
Wed Nov 13 16:04:28 2019 -0800
Revision:
10:3e2ff983be1c
Parent:
9:12e37800ecdd
Child:
11:e8a4162d4fd1
CmdLine parse_and_remove_key match_is_case_sensitive=false

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 10:3e2ff983be1c 331 if (buf[idxSearch] != key[0]) { continue; }
whismanoid 10:3e2ff983be1c 332 }
whismanoid 10:3e2ff983be1c 333 else {
whismanoid 10:3e2ff983be1c 334 if (toupper(buf[idxSearch]) != toupper(key[0])) { continue; }
whismanoid 10:3e2ff983be1c 335 }
whismanoid 0:4d7de8b5c800 336 // possible match; compare buf[idxSearch..] to key[0..]
whismanoid 0:4d7de8b5c800 337 unsigned int idxKey = idxSearch; // test whether buf[idxKey..] == key[0..]
whismanoid 9:12e37800ecdd 338 unsigned int idxSeparator = idxSearch; // test whether key=value pair
whismanoid 0:4d7de8b5c800 339 unsigned int idxSpace = idxSearch; // end of key=value word
whismanoid 0:4d7de8b5c800 340 for (unsigned int offset = 0; offset < strlen(key); offset++)
whismanoid 0:4d7de8b5c800 341 {
whismanoid 0:4d7de8b5c800 342 if (buf[idxKey + offset] != key[offset]) { idxKey = 0; break; }
whismanoid 9:12e37800ecdd 343 idxSpace = idxKey + offset + 1; // assume next char is a word break
whismanoid 9:12e37800ecdd 344 idxSeparator = idxKey + offset + 1; // assume next char is a separator
whismanoid 9:12e37800ecdd 345 if ((buf[idxSeparator] != '=') && (buf[idxSeparator] != '?')) { idxSeparator = 0; }
whismanoid 0:4d7de8b5c800 346 }
whismanoid 0:4d7de8b5c800 347 if (idxKey == 0) continue; // no match at idxSearch but keep searching
whismanoid 0:4d7de8b5c800 348 // ASSERT buf[idxKey..] == key[0..]
whismanoid 0:4d7de8b5c800 349 while ((buf[idxSpace] != ' ') && idxSpace < indexOfNextEmptyCell) { idxSpace++; }
whismanoid 0:4d7de8b5c800 350 // serial().printf("\r\n parse_and_remove_key(\"%s\") match at index %d length %d, '=' index %d, ' ' index %d",
whismanoid 9:12e37800ecdd 351 // key, idxKey, strlen(key), idxSeparator, idxSpace);
whismanoid 9:12e37800ecdd 352 if (idxSeparator != 0) {
whismanoid 9:12e37800ecdd 353 // found key=value: copy buf[idxSeparator+1..' '-1] into valueBuf[0..valueBufLen-1]
whismanoid 9:12e37800ecdd 354 chSeparator = buf[idxSeparator];
whismanoid 0:4d7de8b5c800 355 for (unsigned int offset = 0; offset < valueBufLen - 1; offset++)
whismanoid 0:4d7de8b5c800 356 {
whismanoid 9:12e37800ecdd 357 if (buf[idxSeparator + 1 + offset] == ' ') break;
whismanoid 9:12e37800ecdd 358 valueBuf[offset] = buf[idxSeparator + 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 9:12e37800ecdd 363 chSeparator = '\0';
whismanoid 0:4d7de8b5c800 364 valueBuf[0] = '\0';
whismanoid 0:4d7de8b5c800 365 }
whismanoid 0:4d7de8b5c800 366 // on successful match, the key=value should be deleted from cmdbuf
whismanoid 0:4d7de8b5c800 367 //serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" before deleting key",
whismanoid 0:4d7de8b5c800 368 // key, buf, valueBuf);
whismanoid 0:4d7de8b5c800 369 for (unsigned int offset = 0; offset < indexOfNextEmptyCell; offset++)
whismanoid 0:4d7de8b5c800 370 {
whismanoid 0:4d7de8b5c800 371 unsigned int idxCopyDst = idxKey + offset;
whismanoid 0:4d7de8b5c800 372 unsigned int idxCopySrc = idxSpace + 1 + offset;
whismanoid 0:4d7de8b5c800 373 if (idxCopyDst > indexOfNextEmptyCell) break;
whismanoid 0:4d7de8b5c800 374 if (idxCopySrc > indexOfNextEmptyCell) break;
whismanoid 0:4d7de8b5c800 375 buf[idxCopyDst] = buf[idxCopySrc];
whismanoid 0:4d7de8b5c800 376 }
whismanoid 0:4d7de8b5c800 377 // serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" after deleting key",
whismanoid 0:4d7de8b5c800 378 // key, buf, valueBuf);
whismanoid 0:4d7de8b5c800 379 // serial().printf("\r\n parse_and_remove_key(\"%s\") returning true: valueBuf=\"%s\"", key, valueBuf);
whismanoid 0:4d7de8b5c800 380 return true;
whismanoid 0:4d7de8b5c800 381 }
whismanoid 0:4d7de8b5c800 382 // serial().printf("\r\n parse_and_remove_key(\"%s\") no match", key);
whismanoid 0:4d7de8b5c800 383 return false; // no match
whismanoid 0:4d7de8b5c800 384 }
whismanoid 0:4d7de8b5c800 385
whismanoid 0:4d7de8b5c800 386 /** CmdLine::parse_frequency_Hz matches "key"=digits
whismanoid 0:4d7de8b5c800 387 *
whismanoid 0:4d7de8b5c800 388 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 389 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 390 * @param[out] frequency_Hz updated from value string if match "key"=value,
whismanoid 0:4d7de8b5c800 391 * optional suffix kHz or MHz scales the value by 1000 or 10000000
whismanoid 0:4d7de8b5c800 392 *
whismanoid 0:4d7de8b5c800 393 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 394 *
whismanoid 0:4d7de8b5c800 395 */
whismanoid 0:4d7de8b5c800 396 bool CmdLine::parse_frequency_Hz(const char *key, uint32_t& frequency_Hz)
whismanoid 0:4d7de8b5c800 397 {
whismanoid 0:4d7de8b5c800 398 char valueBuf[16];
whismanoid 0:4d7de8b5c800 399 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 400 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 401 {
whismanoid 0:4d7de8b5c800 402 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 403 // parse cmdLine arg (SCLK=\d+(kHZ|MHZ)?)? --> g_SPI_SCLK_Hz
whismanoid 0:4d7de8b5c800 404 frequency_Hz = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 405 if (strstr(valueBuf, "M")) {
whismanoid 0:4d7de8b5c800 406 frequency_Hz = frequency_Hz * 1000000;
whismanoid 0:4d7de8b5c800 407 }
whismanoid 0:4d7de8b5c800 408 if (strstr(valueBuf, "k")) {
whismanoid 0:4d7de8b5c800 409 frequency_Hz = frequency_Hz * 1000;
whismanoid 0:4d7de8b5c800 410 }
whismanoid 0:4d7de8b5c800 411 return true;
whismanoid 0:4d7de8b5c800 412 }
whismanoid 0:4d7de8b5c800 413 return false; // no match
whismanoid 0:4d7de8b5c800 414 }
whismanoid 0:4d7de8b5c800 415
whismanoid 4:700b71cd3bd2 416 /** CmdLine::parse_interval_usec matches "key"=digits
whismanoid 4:700b71cd3bd2 417 *
whismanoid 4:700b71cd3bd2 418 * @return true if keyword was found in buffer
whismanoid 4:700b71cd3bd2 419 * @param[in] key string value to match
whismanoid 4:700b71cd3bd2 420 * @param[out] interval_usec updated from value string if match "key"=value,
whismanoid 4:700b71cd3bd2 421 * optional suffix Hz kHz or MHz 1/x inverts and scales the value
whismanoid 4:700b71cd3bd2 422 * optional suffix s or ms or msec or us or usec scales the value
whismanoid 4:700b71cd3bd2 423 *
whismanoid 4:700b71cd3bd2 424 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 4:700b71cd3bd2 425 *
whismanoid 4:700b71cd3bd2 426 */
whismanoid 4:700b71cd3bd2 427 bool CmdLine::parse_interval_usec(const char *key, us_timestamp_t& interval_usec)
whismanoid 4:700b71cd3bd2 428 {
whismanoid 4:700b71cd3bd2 429 char valueBuf[32];
whismanoid 4:700b71cd3bd2 430 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 4:700b71cd3bd2 431 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 4:700b71cd3bd2 432 {
whismanoid 4:700b71cd3bd2 433 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 4:700b71cd3bd2 434 interval_usec = strtoul(valueBuf, NULL, 10);
whismanoid 4:700b71cd3bd2 435 if (strstr(valueBuf, "ms")) {
whismanoid 4:700b71cd3bd2 436 interval_usec = interval_usec * 1000;
whismanoid 5:1a14de1c4d7b 437 } else if (strstr(valueBuf, "us")) {
whismanoid 5:1a14de1c4d7b 438 // avoid matching "s" which is a subset of "us"
whismanoid 5:1a14de1c4d7b 439 // interval_usec = interval_usec * 1;
whismanoid 4:700b71cd3bd2 440 } else if (strstr(valueBuf, "s")) {
whismanoid 4:700b71cd3bd2 441 interval_usec = interval_usec * 1000000;
whismanoid 4:700b71cd3bd2 442 } else if (strstr(valueBuf, "MHz")) {
whismanoid 4:700b71cd3bd2 443 interval_usec = 1. / interval_usec;
whismanoid 4:700b71cd3bd2 444 } else if (strstr(valueBuf, "kHz")) {
whismanoid 4:700b71cd3bd2 445 interval_usec = 1000. / interval_usec;
whismanoid 4:700b71cd3bd2 446 } else if (strstr(valueBuf, "Hz")) {
whismanoid 4:700b71cd3bd2 447 interval_usec = 1000000. / interval_usec;
whismanoid 4:700b71cd3bd2 448 }
whismanoid 4:700b71cd3bd2 449 return true;
whismanoid 4:700b71cd3bd2 450 }
whismanoid 4:700b71cd3bd2 451 return false; // no match
whismanoid 4:700b71cd3bd2 452 }
whismanoid 4:700b71cd3bd2 453
whismanoid 0:4d7de8b5c800 454 /** CmdLine::parse_flag matches "key"=0 or 1
whismanoid 0:4d7de8b5c800 455 *
whismanoid 0:4d7de8b5c800 456 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 457 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 458 * @param[out] nFlagVar variable to be updated by setting or clearing bits specified by nFlagBitMask.
whismanoid 0:4d7de8b5c800 459 * If match "key"=0 then the nFlagBitMask bits in nFlagVar are cleared.
whismanoid 0:4d7de8b5c800 460 * If match "key"=1 then the nFlagBitMask bits in nFlagVar are set.
whismanoid 0:4d7de8b5c800 461 * @param[in] nFlagBitMask bit mask contains binary 1 in the bit to be controlled by the key=value setting
whismanoid 0:4d7de8b5c800 462 *
whismanoid 0:4d7de8b5c800 463 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 464 *
whismanoid 0:4d7de8b5c800 465 */
whismanoid 0:4d7de8b5c800 466 bool CmdLine::parse_flag(const char *key, uint8_t& nFlagVar, const uint8_t nFlagBitMask)
whismanoid 0:4d7de8b5c800 467 {
whismanoid 0:4d7de8b5c800 468 char valueBuf[16];
whismanoid 0:4d7de8b5c800 469 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 470 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 471 {
whismanoid 0:4d7de8b5c800 472 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 473 // parse cmdLine arg (CPHA=\d)? --> g_SPI_dataMode | SPI_MODE1
whismanoid 0:4d7de8b5c800 474 int x = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 475 if (x)
whismanoid 0:4d7de8b5c800 476 {
whismanoid 0:4d7de8b5c800 477 nFlagVar |= nFlagBitMask;
whismanoid 0:4d7de8b5c800 478 }
whismanoid 0:4d7de8b5c800 479 else
whismanoid 0:4d7de8b5c800 480 {
whismanoid 0:4d7de8b5c800 481 nFlagVar &= ~nFlagBitMask;
whismanoid 0:4d7de8b5c800 482 }
whismanoid 0:4d7de8b5c800 483 return true;
whismanoid 0:4d7de8b5c800 484 }
whismanoid 0:4d7de8b5c800 485 return false; // no match
whismanoid 0:4d7de8b5c800 486 }
whismanoid 0:4d7de8b5c800 487
whismanoid 0:4d7de8b5c800 488 /** CmdLine::parse_byte_hex matches "key"=value
whismanoid 0:4d7de8b5c800 489 *
whismanoid 0:4d7de8b5c800 490 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 491 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 492 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 493 *
whismanoid 0:4d7de8b5c800 494 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 495 *
whismanoid 2:0f702da53f2a 496 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 497 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 498 */
whismanoid 0:4d7de8b5c800 499 bool CmdLine::parse_byte_hex(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 500 {
whismanoid 0:4d7de8b5c800 501 char valueBuf[16];
whismanoid 0:4d7de8b5c800 502 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 503 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 504 {
whismanoid 0:4d7de8b5c800 505 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 506 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 507 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 508 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 509 {
whismanoid 0:4d7de8b5c800 510 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 511 return true;
whismanoid 0:4d7de8b5c800 512 }
whismanoid 0:4d7de8b5c800 513 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 514 {
whismanoid 0:4d7de8b5c800 515 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 516 return true;
whismanoid 0:4d7de8b5c800 517 }
whismanoid 2:0f702da53f2a 518 nByteVar = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 0:4d7de8b5c800 519 return true;
whismanoid 0:4d7de8b5c800 520 }
whismanoid 0:4d7de8b5c800 521 return false; // no match
whismanoid 0:4d7de8b5c800 522 }
whismanoid 0:4d7de8b5c800 523
whismanoid 0:4d7de8b5c800 524 /** CmdLine::parse_byte_dec matches "key"=value
whismanoid 0:4d7de8b5c800 525 *
whismanoid 0:4d7de8b5c800 526 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 527 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 528 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 529 *
whismanoid 0:4d7de8b5c800 530 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 531 *
whismanoid 2:0f702da53f2a 532 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 533 * Default number conversion radix is base-10 decimal.
whismanoid 0:4d7de8b5c800 534 */
whismanoid 0:4d7de8b5c800 535 bool CmdLine::parse_byte_dec(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 536 {
whismanoid 0:4d7de8b5c800 537 char valueBuf[16];
whismanoid 0:4d7de8b5c800 538 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 539 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 540 {
whismanoid 0:4d7de8b5c800 541 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 542 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 543 // TODO1: parse_byte_dec take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 544 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 545 {
whismanoid 0:4d7de8b5c800 546 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 547 return true;
whismanoid 0:4d7de8b5c800 548 }
whismanoid 0:4d7de8b5c800 549 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 550 {
whismanoid 0:4d7de8b5c800 551 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 552 return true;
whismanoid 0:4d7de8b5c800 553 }
whismanoid 2:0f702da53f2a 554 nByteVar = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 555 return true;
whismanoid 0:4d7de8b5c800 556 }
whismanoid 0:4d7de8b5c800 557 return false; // no match
whismanoid 0:4d7de8b5c800 558 }
whismanoid 0:4d7de8b5c800 559
whismanoid 0:4d7de8b5c800 560 /** CmdLine::parse_uint16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 561 *
whismanoid 0:4d7de8b5c800 562 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 563 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 564 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 565 *
whismanoid 0:4d7de8b5c800 566 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 567 *
whismanoid 2:0f702da53f2a 568 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 569 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 570 */
whismanoid 0:4d7de8b5c800 571 bool CmdLine::parse_uint16_hex(const char *key, uint16_t& uint16Var)
whismanoid 0:4d7de8b5c800 572 {
whismanoid 0:4d7de8b5c800 573 char valueBuf[16];
whismanoid 0:4d7de8b5c800 574 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 575 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 576 {
whismanoid 0:4d7de8b5c800 577 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 578 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 579 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 580 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 581 {
whismanoid 0:4d7de8b5c800 582 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 583 return true;
whismanoid 0:4d7de8b5c800 584 }
whismanoid 0:4d7de8b5c800 585 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 586 {
whismanoid 0:4d7de8b5c800 587 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 588 return true;
whismanoid 0:4d7de8b5c800 589 }
whismanoid 2:0f702da53f2a 590 uint16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 591 return true;
whismanoid 2:0f702da53f2a 592 }
whismanoid 2:0f702da53f2a 593 return false; // no match
whismanoid 2:0f702da53f2a 594 }
whismanoid 2:0f702da53f2a 595
whismanoid 2:0f702da53f2a 596 /** CmdLine::parse_uint16_dec matches "key"=value
whismanoid 2:0f702da53f2a 597 *
whismanoid 2:0f702da53f2a 598 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 599 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 600 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 601 *
whismanoid 2:0f702da53f2a 602 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 603 *
whismanoid 2:0f702da53f2a 604 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 605 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 606 */
whismanoid 2:0f702da53f2a 607 bool CmdLine::parse_uint16_dec(const char *key, uint16_t& uint16Var)
whismanoid 2:0f702da53f2a 608 {
whismanoid 2:0f702da53f2a 609 char valueBuf[16];
whismanoid 2:0f702da53f2a 610 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 611 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 612 {
whismanoid 2:0f702da53f2a 613 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 614 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 615 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 616 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 617 {
whismanoid 2:0f702da53f2a 618 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 619 return true;
whismanoid 2:0f702da53f2a 620 }
whismanoid 2:0f702da53f2a 621 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 622 {
whismanoid 2:0f702da53f2a 623 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 624 return true;
whismanoid 2:0f702da53f2a 625 }
whismanoid 2:0f702da53f2a 626 uint16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 627 return true;
whismanoid 0:4d7de8b5c800 628 }
whismanoid 0:4d7de8b5c800 629 return false; // no match
whismanoid 0:4d7de8b5c800 630 }
whismanoid 0:4d7de8b5c800 631
whismanoid 0:4d7de8b5c800 632 /** CmdLine::parse_int16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 633 *
whismanoid 0:4d7de8b5c800 634 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 635 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 636 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 637 *
whismanoid 0:4d7de8b5c800 638 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 639 *
whismanoid 2:0f702da53f2a 640 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 641 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 642 */
whismanoid 0:4d7de8b5c800 643 bool CmdLine::parse_int16_hex(const char *key, int16_t& int16Var)
whismanoid 0:4d7de8b5c800 644 {
whismanoid 0:4d7de8b5c800 645 char valueBuf[16];
whismanoid 0:4d7de8b5c800 646 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 647 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 648 {
whismanoid 0:4d7de8b5c800 649 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 650 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 651 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 652 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 653 {
whismanoid 0:4d7de8b5c800 654 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 655 return true;
whismanoid 0:4d7de8b5c800 656 }
whismanoid 0:4d7de8b5c800 657 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 658 {
whismanoid 0:4d7de8b5c800 659 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 660 return true;
whismanoid 0:4d7de8b5c800 661 }
whismanoid 2:0f702da53f2a 662 int16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 663 return true;
whismanoid 2:0f702da53f2a 664 }
whismanoid 2:0f702da53f2a 665 return false; // no match
whismanoid 2:0f702da53f2a 666 }
whismanoid 2:0f702da53f2a 667
whismanoid 2:0f702da53f2a 668 /** CmdLine::parse_int16_dec matches "key"=value
whismanoid 2:0f702da53f2a 669 *
whismanoid 2:0f702da53f2a 670 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 671 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 672 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 673 *
whismanoid 2:0f702da53f2a 674 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 675 *
whismanoid 2:0f702da53f2a 676 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 677 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 678 */
whismanoid 2:0f702da53f2a 679 bool CmdLine::parse_int16_dec(const char *key, int16_t& int16Var)
whismanoid 2:0f702da53f2a 680 {
whismanoid 2:0f702da53f2a 681 char valueBuf[16];
whismanoid 2:0f702da53f2a 682 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 683 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 684 {
whismanoid 2:0f702da53f2a 685 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 686 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 687 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 688 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 689 {
whismanoid 2:0f702da53f2a 690 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 691 return true;
whismanoid 2:0f702da53f2a 692 }
whismanoid 2:0f702da53f2a 693 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 694 {
whismanoid 2:0f702da53f2a 695 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 696 return true;
whismanoid 2:0f702da53f2a 697 }
whismanoid 2:0f702da53f2a 698 int16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 699 return true;
whismanoid 0:4d7de8b5c800 700 }
whismanoid 0:4d7de8b5c800 701 return false; // no match
whismanoid 0:4d7de8b5c800 702 }
whismanoid 0:4d7de8b5c800 703
whismanoid 9:12e37800ecdd 704 /** CmdLine::parse_uint32_hex matches "key"=value
whismanoid 9:12e37800ecdd 705 *
whismanoid 9:12e37800ecdd 706 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 707 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 708 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 709 *
whismanoid 9:12e37800ecdd 710 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 711 *
whismanoid 9:12e37800ecdd 712 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 713 * Default number conversion radix is base-16 hexadecimal.
whismanoid 9:12e37800ecdd 714 */
whismanoid 9:12e37800ecdd 715 bool CmdLine::parse_uint32_hex(const char *key, uint32_t& uint32Var)
whismanoid 9:12e37800ecdd 716 {
whismanoid 9:12e37800ecdd 717 char valueBuf[16];
whismanoid 9:12e37800ecdd 718 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 719 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 720 {
whismanoid 9:12e37800ecdd 721 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 722 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 723 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 724 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 725 {
whismanoid 9:12e37800ecdd 726 uint32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 727 return true;
whismanoid 9:12e37800ecdd 728 }
whismanoid 9:12e37800ecdd 729 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 730 {
whismanoid 9:12e37800ecdd 731 uint32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 732 return true;
whismanoid 9:12e37800ecdd 733 }
whismanoid 9:12e37800ecdd 734 uint32Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 9:12e37800ecdd 735 return true;
whismanoid 9:12e37800ecdd 736 }
whismanoid 9:12e37800ecdd 737 return false; // no match
whismanoid 9:12e37800ecdd 738 }
whismanoid 9:12e37800ecdd 739
whismanoid 9:12e37800ecdd 740 /** CmdLine::parse_uint32_dec matches "key"=value
whismanoid 9:12e37800ecdd 741 *
whismanoid 9:12e37800ecdd 742 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 743 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 744 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 745 *
whismanoid 9:12e37800ecdd 746 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 747 *
whismanoid 9:12e37800ecdd 748 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 749 * Default number conversion radix is base-10 decimal.
whismanoid 9:12e37800ecdd 750 */
whismanoid 9:12e37800ecdd 751 bool CmdLine::parse_uint32_dec(const char *key, uint32_t& uint32Var)
whismanoid 9:12e37800ecdd 752 {
whismanoid 9:12e37800ecdd 753 char valueBuf[16];
whismanoid 9:12e37800ecdd 754 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 755 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 756 {
whismanoid 9:12e37800ecdd 757 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 758 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 759 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 760 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 761 {
whismanoid 9:12e37800ecdd 762 uint32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 763 return true;
whismanoid 9:12e37800ecdd 764 }
whismanoid 9:12e37800ecdd 765 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 766 {
whismanoid 9:12e37800ecdd 767 uint32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 768 return true;
whismanoid 9:12e37800ecdd 769 }
whismanoid 9:12e37800ecdd 770 uint32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 9:12e37800ecdd 771 return true;
whismanoid 9:12e37800ecdd 772 }
whismanoid 9:12e37800ecdd 773 return false; // no match
whismanoid 9:12e37800ecdd 774 }
whismanoid 9:12e37800ecdd 775
whismanoid 9:12e37800ecdd 776 /** CmdLine::parse_int32_hex matches "key"=value
whismanoid 9:12e37800ecdd 777 *
whismanoid 9:12e37800ecdd 778 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 779 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 780 * @param[out] int32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 781 *
whismanoid 9:12e37800ecdd 782 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 783 *
whismanoid 9:12e37800ecdd 784 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 785 * Default number conversion radix is base-16 hexadecimal.
whismanoid 9:12e37800ecdd 786 */
whismanoid 9:12e37800ecdd 787 bool CmdLine::parse_int32_hex(const char *key, int32_t& int32Var)
whismanoid 9:12e37800ecdd 788 {
whismanoid 9:12e37800ecdd 789 char valueBuf[16];
whismanoid 9:12e37800ecdd 790 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 791 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 792 {
whismanoid 9:12e37800ecdd 793 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 794 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 795 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 796 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 797 {
whismanoid 9:12e37800ecdd 798 int32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 799 return true;
whismanoid 9:12e37800ecdd 800 }
whismanoid 9:12e37800ecdd 801 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 802 {
whismanoid 9:12e37800ecdd 803 int32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 804 return true;
whismanoid 9:12e37800ecdd 805 }
whismanoid 9:12e37800ecdd 806 int32Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 9:12e37800ecdd 807 return true;
whismanoid 9:12e37800ecdd 808 }
whismanoid 9:12e37800ecdd 809 return false; // no match
whismanoid 9:12e37800ecdd 810 }
whismanoid 9:12e37800ecdd 811
whismanoid 9:12e37800ecdd 812 /** CmdLine::parse_int32_dec matches "key"=value
whismanoid 9:12e37800ecdd 813 *
whismanoid 9:12e37800ecdd 814 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 815 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 816 * @param[out] int32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 817 *
whismanoid 9:12e37800ecdd 818 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 819 *
whismanoid 9:12e37800ecdd 820 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 821 * Default number conversion radix is base-10 decimal.
whismanoid 9:12e37800ecdd 822 */
whismanoid 9:12e37800ecdd 823 bool CmdLine::parse_int32_dec(const char *key, int32_t& int32Var)
whismanoid 9:12e37800ecdd 824 {
whismanoid 9:12e37800ecdd 825 char valueBuf[16];
whismanoid 9:12e37800ecdd 826 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 827 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 828 {
whismanoid 9:12e37800ecdd 829 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 830 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 831 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 832 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 833 {
whismanoid 9:12e37800ecdd 834 int32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 835 return true;
whismanoid 9:12e37800ecdd 836 }
whismanoid 9:12e37800ecdd 837 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 838 {
whismanoid 9:12e37800ecdd 839 int32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 840 return true;
whismanoid 9:12e37800ecdd 841 }
whismanoid 9:12e37800ecdd 842 int32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 9:12e37800ecdd 843 return true;
whismanoid 9:12e37800ecdd 844 }
whismanoid 9:12e37800ecdd 845 return false; // no match
whismanoid 9:12e37800ecdd 846 }
whismanoid 9:12e37800ecdd 847
whismanoid 7:0bda7cfee767 848 /** CmdLine::parse_double matches "key"=value
whismanoid 7:0bda7cfee767 849 *
whismanoid 7:0bda7cfee767 850 * @return true if keyword was found in buffer
whismanoid 7:0bda7cfee767 851 * @param[in] key string value to match
whismanoid 7:0bda7cfee767 852 * @param[out] doubleVar updated from value string if match "key"=value
whismanoid 7:0bda7cfee767 853 *
whismanoid 7:0bda7cfee767 854 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 7:0bda7cfee767 855 *
whismanoid 7:0bda7cfee767 856 */
whismanoid 7:0bda7cfee767 857 bool CmdLine::parse_double(const char *key, double& doubleVar)
whismanoid 7:0bda7cfee767 858 {
whismanoid 7:0bda7cfee767 859 char valueBuf[16];
whismanoid 7:0bda7cfee767 860 char *end;
whismanoid 7:0bda7cfee767 861 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 7:0bda7cfee767 862 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 7:0bda7cfee767 863 {
whismanoid 7:0bda7cfee767 864 doubleVar = strtof(valueBuf, &end);
whismanoid 7:0bda7cfee767 865 return true;
whismanoid 7:0bda7cfee767 866 }
whismanoid 7:0bda7cfee767 867 return false; // no match
whismanoid 7:0bda7cfee767 868 }
whismanoid 7:0bda7cfee767 869
whismanoid 0:4d7de8b5c800 870 /** CmdLine::parse_float matches "key"=value
whismanoid 0:4d7de8b5c800 871 *
whismanoid 0:4d7de8b5c800 872 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 873 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 874 * @param[out] floatVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 875 *
whismanoid 0:4d7de8b5c800 876 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 877 *
whismanoid 0:4d7de8b5c800 878 */
whismanoid 0:4d7de8b5c800 879 bool CmdLine::parse_float(const char *key, float& floatVar)
whismanoid 0:4d7de8b5c800 880 {
whismanoid 0:4d7de8b5c800 881 char valueBuf[16];
whismanoid 0:4d7de8b5c800 882 char *end;
whismanoid 0:4d7de8b5c800 883 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 884 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 885 {
whismanoid 0:4d7de8b5c800 886 floatVar = strtof(valueBuf, &end);
whismanoid 0:4d7de8b5c800 887 return true;
whismanoid 0:4d7de8b5c800 888 }
whismanoid 0:4d7de8b5c800 889 return false; // no match
whismanoid 0:4d7de8b5c800 890 }
whismanoid 0:4d7de8b5c800 891
whismanoid 0:4d7de8b5c800 892 /** CmdLine::parse_byteCount_byteList_hex matches hexadecimal digits separated by spaces.
whismanoid 0:4d7de8b5c800 893 * The 0x / 0X prefix is optional. The numbers must be hexadecimal.
whismanoid 0:4d7de8b5c800 894 *
whismanoid 0:4d7de8b5c800 895 * @return true if more than one hex byte found in buffer
whismanoid 0:4d7de8b5c800 896 * @param[out] byteCount is populated with the number of hex bytes found
whismanoid 0:4d7de8b5c800 897 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the hex bytes found
whismanoid 0:4d7de8b5c800 898 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 899 *
whismanoid 0:4d7de8b5c800 900 */
whismanoid 0:4d7de8b5c800 901 bool CmdLine::parse_byteCount_byteList_hex(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 902 {
whismanoid 0:4d7de8b5c800 903 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 904 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 905 byteCount = 0;
whismanoid 0:4d7de8b5c800 906 bool got_value = false;
whismanoid 0:4d7de8b5c800 907 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 908 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 909 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 910 {
whismanoid 0:4d7de8b5c800 911 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 912 break; // end of buffer
whismanoid 0:4d7de8b5c800 913 }
whismanoid 0:4d7de8b5c800 914 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 915 {
whismanoid 0:4d7de8b5c800 916 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 917 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 918 case '8': case '9':
whismanoid 0:4d7de8b5c800 919 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 920 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 921 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 922 got_value = true;
whismanoid 0:4d7de8b5c800 923 break;
whismanoid 0:4d7de8b5c800 924 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 925 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 926 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 927 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 928 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 929 got_value = true;
whismanoid 0:4d7de8b5c800 930 break;
whismanoid 0:4d7de8b5c800 931 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 932 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 933 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 934 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 935 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 936 got_value = true;
whismanoid 0:4d7de8b5c800 937 break;
whismanoid 0:4d7de8b5c800 938 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 939 temp_value = 0;
whismanoid 0:4d7de8b5c800 940 break;
whismanoid 0:4d7de8b5c800 941 case ' ': case ',':
whismanoid 0:4d7de8b5c800 942 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 943 {
whismanoid 0:4d7de8b5c800 944 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 945 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 946 temp_value = 0;
whismanoid 0:4d7de8b5c800 947 got_value = false;
whismanoid 0:4d7de8b5c800 948 }
whismanoid 0:4d7de8b5c800 949 break;
whismanoid 0:4d7de8b5c800 950 }
whismanoid 0:4d7de8b5c800 951 } // for idxSearch
whismanoid 0:4d7de8b5c800 952 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 953 {
whismanoid 0:4d7de8b5c800 954 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 955 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 956 temp_value = 0;
whismanoid 0:4d7de8b5c800 957 got_value = false;
whismanoid 0:4d7de8b5c800 958 }
whismanoid 0:4d7de8b5c800 959 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 960 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 961 }
whismanoid 0:4d7de8b5c800 962
whismanoid 0:4d7de8b5c800 963 /** CmdLine::parse_byteCount_byteList_dec matches a list of numbers, separated by spaces.
whismanoid 0:4d7de8b5c800 964 * The 0x / 0X prefix may be used to select hexadecimal instead of decimal.
whismanoid 0:4d7de8b5c800 965 *
whismanoid 0:4d7de8b5c800 966 * @return true if more than one number found in buffer
whismanoid 0:4d7de8b5c800 967 * @param[out] byteCount is populated with the number of numbers found
whismanoid 0:4d7de8b5c800 968 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the numbers found
whismanoid 0:4d7de8b5c800 969 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 970 *
whismanoid 0:4d7de8b5c800 971 */
whismanoid 0:4d7de8b5c800 972 bool CmdLine::parse_byteCount_byteList_dec(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 973 {
whismanoid 0:4d7de8b5c800 974 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 975 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 976 byteCount = 0;
whismanoid 0:4d7de8b5c800 977 bool got_value = false;
whismanoid 0:4d7de8b5c800 978 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 979 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 980 uint8_t radix = 10;
whismanoid 0:4d7de8b5c800 981 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 982 {
whismanoid 0:4d7de8b5c800 983 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 984 break; // end of buffer
whismanoid 0:4d7de8b5c800 985 }
whismanoid 0:4d7de8b5c800 986 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 987 {
whismanoid 0:4d7de8b5c800 988 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 989 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 990 case '8': case '9':
whismanoid 0:4d7de8b5c800 991 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 992 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 993 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 994 got_value = true;
whismanoid 0:4d7de8b5c800 995 break;
whismanoid 0:4d7de8b5c800 996 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 997 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 998 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 999 radix = 0x10;
whismanoid 0:4d7de8b5c800 1000 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1001 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1002 got_value = true;
whismanoid 0:4d7de8b5c800 1003 break;
whismanoid 0:4d7de8b5c800 1004 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 1005 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 1006 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 1007 radix = 0x10;
whismanoid 0:4d7de8b5c800 1008 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1009 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1010 got_value = true;
whismanoid 0:4d7de8b5c800 1011 break;
whismanoid 0:4d7de8b5c800 1012 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 1013 temp_value = 0;
whismanoid 0:4d7de8b5c800 1014 radix = 0x10;
whismanoid 0:4d7de8b5c800 1015 break;
whismanoid 0:4d7de8b5c800 1016 case ' ': case ',':
whismanoid 0:4d7de8b5c800 1017 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1018 {
whismanoid 0:4d7de8b5c800 1019 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1020 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1021 temp_value = 0;
whismanoid 0:4d7de8b5c800 1022 radix = 10;
whismanoid 0:4d7de8b5c800 1023 got_value = false;
whismanoid 0:4d7de8b5c800 1024 }
whismanoid 0:4d7de8b5c800 1025 break;
whismanoid 0:4d7de8b5c800 1026 }
whismanoid 0:4d7de8b5c800 1027 } // for idxSearch
whismanoid 0:4d7de8b5c800 1028 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1029 {
whismanoid 0:4d7de8b5c800 1030 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1031 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1032 temp_value = 0;
whismanoid 0:4d7de8b5c800 1033 got_value = false;
whismanoid 0:4d7de8b5c800 1034 }
whismanoid 0:4d7de8b5c800 1035 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 1036 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 1037 }
whismanoid 0:4d7de8b5c800 1038
whismanoid 0:4d7de8b5c800 1039
whismanoid 0:4d7de8b5c800 1040 // End of file