editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Revision:
2:0f702da53f2a
Parent:
1:5b33e7447601
Child:
3:cf3de26aa444
--- a/CmdLine.cpp	Thu May 30 00:14:34 2019 +0000
+++ b/CmdLine.cpp	Thu Jun 06 01:50:32 2019 +0000
@@ -454,6 +454,8 @@
  *
  * @post on successful match, the key=value substring is deleted from cmdbuf
  *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-16 hexadecimal.
  */
 bool CmdLine::parse_byte_hex(const char *key, uint8_t& nByteVar)
 {
@@ -474,7 +476,7 @@
             nByteVar = strtoul(valueBuf + 2, NULL, 16);
             return true;
         }
-        nByteVar = strtoul(valueBuf, NULL, 16);
+        nByteVar = strtoul(valueBuf, NULL, 16); // default radix hex
         return true;
     }
     return false; // no match
@@ -488,6 +490,8 @@
  *
  * @post on successful match, the key=value substring is deleted from cmdbuf
  *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-10 decimal.
  */
 bool CmdLine::parse_byte_dec(const char *key, uint8_t& nByteVar)
 {
@@ -508,7 +512,7 @@
             nByteVar = strtoul(valueBuf + 2, NULL, 16);
             return true;
         }
-        nByteVar = strtoul(valueBuf, NULL, 10);
+        nByteVar = strtoul(valueBuf, NULL, 10); // default radix decimal
         return true;
     }
     return false; // no match
@@ -522,6 +526,8 @@
  *
  * @post on successful match, the key=value substring is deleted from cmdbuf
  *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-16 hexadecimal.
  */
 bool CmdLine::parse_uint16_hex(const char *key, uint16_t& uint16Var)
 {
@@ -542,7 +548,43 @@
             uint16Var = strtoul(valueBuf + 2, NULL, 16);
             return true;
         }
-        uint16Var = strtoul(valueBuf, NULL, 16);
+        uint16Var = strtoul(valueBuf, NULL, 16); // default radix hex
+        return true;
+    }
+    return false; // no match
+}
+
+/** CmdLine::parse_uint16_dec matches "key"=value
+ *
+ * @return true if keyword was found in buffer
+ * @param[in] key string value to match
+ * @param[out] uint16Var updated from value string if match "key"=value
+ *
+ * @post on successful match, the key=value substring is deleted from cmdbuf
+ *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-10 decimal.
+ */
+bool CmdLine::parse_uint16_dec(const char *key, uint16_t& uint16Var)
+{
+    char valueBuf[16];
+    // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
+    if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
+    {
+        // ASSERT: buf[matched_index] contains '=' followed by value
+        // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
+        // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
+        if (valueBuf[0] == '$')
+        {
+            uint16Var = strtoul(valueBuf + 1, NULL, 16);
+            return true;
+        }
+        if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
+        {
+            uint16Var = strtoul(valueBuf + 2, NULL, 16);
+            return true;
+        }
+        uint16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
         return true;
     }
     return false; // no match
@@ -556,6 +598,8 @@
  *
  * @post on successful match, the key=value substring is deleted from cmdbuf
  *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-16 hexadecimal.
  */
 bool CmdLine::parse_int16_hex(const char *key, int16_t& int16Var)
 {
@@ -576,7 +620,43 @@
             int16Var = strtoul(valueBuf + 2, NULL, 16);
             return true;
         }
-        int16Var = strtoul(valueBuf, NULL, 16);
+        int16Var = strtoul(valueBuf, NULL, 16); // default radix hex
+        return true;
+    }
+    return false; // no match
+}
+
+/** CmdLine::parse_int16_dec matches "key"=value
+ *
+ * @return true if keyword was found in buffer
+ * @param[in] key string value to match
+ * @param[out] int16Var updated from value string if match "key"=value
+ *
+ * @post on successful match, the key=value substring is deleted from cmdbuf
+ *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-10 decimal.
+ */
+bool CmdLine::parse_int16_dec(const char *key, int16_t& int16Var)
+{
+    char valueBuf[16];
+    // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
+    if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
+    {
+        // ASSERT: buf[matched_index] contains '=' followed by value
+        // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
+        // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
+        if (valueBuf[0] == '$')
+        {
+            int16Var = strtoul(valueBuf + 1, NULL, 16);
+            return true;
+        }
+        if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
+        {
+            int16Var = strtoul(valueBuf + 2, NULL, 16);
+            return true;
+        }
+        int16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
         return true;
     }
     return false; // no match