editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Committer:
whismanoid
Date:
Thu May 23 22:08:35 2019 +0000
Revision:
0:4d7de8b5c800
Child:
1:5b33e7447601
initial commit class CmdLine from test fixture / sandbox

Who changed what in which revision?

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