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