editable serial input line buffer
Dependents: MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more
Diff: CmdLine.cpp
- Revision:
- 16:592df067fe14
- Parent:
- 15:34b039027e5f
- Child:
- 17:371f0c5956da
--- a/CmdLine.cpp Tue Apr 27 03:47:33 2021 -0700 +++ b/CmdLine.cpp Tue Jun 08 17:44:35 2021 -0700 @@ -904,6 +904,202 @@ return false; // no match } +/** CmdLine::parse_double_or_int16 matches "key"=value + * + * Value will be parsed as double if it contains "." or "V", + * Otherwise, + * Value will be parsed as hexadecimal if it begins with "$" or "0x", + * Otherwise, + * Value will be parsed as decimal + * + * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double + * @param[in] key string value to match + * @param[out] doubleVar updated from value string if match "key"=xx.xxx + * @param[out] uint32Var updated from value string if match "key"=value + * + * @post on successful match, the key=value substring is deleted from cmdbuf + * + */ +uint8_t CmdLine::parse_double_or_int16(const char *key, double& doubleVar, int16_t& int16Var) +{ + char valueBuf[16]; + char *end; // buffer required by strtof + // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen); + if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf))) + { + for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++) + { + switch(valueBuf[index]) + { + case '.': case 'V': case 'v': + // valueBuf contains decimal "." or letter "V" + doubleVar = strtof(valueBuf, &end); + return 2; // 2: parsed as double + } + } + if (valueBuf[0] == '$') + { + int16Var = strtoul(valueBuf + 1, NULL, 16); + return 1; // 1: parsed as integer + } + if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x')) + { + int16Var = strtoul(valueBuf + 2, NULL, 16); + return 1; // 1: parsed as integer + } + int16Var = strtoul(valueBuf, NULL, 10); // default radix decimal + return 1; // 1: parsed as integer + } + return 0; // no match +} + +/** CmdLine::parse_double_or_uint16 matches "key"=value + * + * Value will be parsed as double if it contains "." or "V", + * Otherwise, + * Value will be parsed as hexadecimal if it begins with "$" or "0x", + * Otherwise, + * Value will be parsed as decimal + * + * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double + * @param[in] key string value to match + * @param[out] doubleVar updated from value string if match "key"=xx.xxx + * @param[out] uint32Var updated from value string if match "key"=value + * + * @post on successful match, the key=value substring is deleted from cmdbuf + * + */ +uint8_t CmdLine::parse_double_or_uint16(const char *key, double& doubleVar, uint16_t& uint16Var) +{ + char valueBuf[16]; + char *end; // buffer required by strtof + // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen); + if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf))) + { + for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++) + { + switch(valueBuf[index]) + { + case '.': case 'V': case 'v': + // valueBuf contains decimal "." or letter "V" + doubleVar = strtof(valueBuf, &end); + return 2; // 2: parsed as double + } + } + if (valueBuf[0] == '$') + { + uint16Var = strtoul(valueBuf + 1, NULL, 16); + return 1; // 1: parsed as integer + } + if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x')) + { + uint16Var = strtoul(valueBuf + 2, NULL, 16); + return 1; // 1: parsed as integer + } + uint16Var = strtoul(valueBuf, NULL, 10); // default radix decimal + return 1; // 1: parsed as integer + } + return 0; // no match +} + +/** CmdLine::parse_double_or_int32 matches "key"=value + * + * Value will be parsed as double if it contains "." or "V", + * Otherwise, + * Value will be parsed as hexadecimal if it begins with "$" or "0x", + * Otherwise, + * Value will be parsed as decimal + * + * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double + * @param[in] key string value to match + * @param[out] doubleVar updated from value string if match "key"=xx.xxx + * @param[out] uint32Var updated from value string if match "key"=value + * + * @post on successful match, the key=value substring is deleted from cmdbuf + * + */ +uint8_t CmdLine::parse_double_or_int32(const char *key, double& doubleVar, int32_t& int32Var) +{ + char valueBuf[16]; + char *end; // buffer required by strtof + // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen); + if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf))) + { + for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++) + { + switch(valueBuf[index]) + { + case '.': case 'V': case 'v': + // valueBuf contains decimal "." or letter "V" + doubleVar = strtof(valueBuf, &end); + return 2; // 2: parsed as double + } + } + if (valueBuf[0] == '$') + { + int32Var = strtoul(valueBuf + 1, NULL, 16); + return 1; // 1: parsed as integer + } + if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x')) + { + int32Var = strtoul(valueBuf + 2, NULL, 16); + return 1; // 1: parsed as integer + } + int32Var = strtoul(valueBuf, NULL, 10); // default radix decimal + return 1; // 1: parsed as integer + } + return 0; // no match +} + +/** CmdLine::parse_double_or_uint32 matches "key"=value + * + * Value will be parsed as double if it contains "." or "V", + * Otherwise, + * Value will be parsed as hexadecimal if it begins with "$" or "0x", + * Otherwise, + * Value will be parsed as decimal + * + * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double + * @param[in] key string value to match + * @param[out] doubleVar updated from value string if match "key"=xx.xxx + * @param[out] uint32Var updated from value string if match "key"=value + * + * @post on successful match, the key=value substring is deleted from cmdbuf + * + */ +uint8_t CmdLine::parse_double_or_uint32(const char *key, double& doubleVar, uint32_t& uint32Var) +{ + char valueBuf[16]; + char *end; // buffer required by strtof + // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen); + if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf))) + { + for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++) + { + switch(valueBuf[index]) + { + case '.': case 'V': case 'v': + // valueBuf contains decimal "." or letter "V" + doubleVar = strtof(valueBuf, &end); + return 2; // 2: parsed as double + } + } + if (valueBuf[0] == '$') + { + uint32Var = strtoul(valueBuf + 1, NULL, 16); + return 1; // 1: parsed as integer + } + if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x')) + { + uint32Var = strtoul(valueBuf + 2, NULL, 16); + return 1; // 1: parsed as integer + } + uint32Var = strtoul(valueBuf, NULL, 10); // default radix decimal + return 1; // 1: parsed as integer + } + return 0; // no match +} + /** CmdLine::parse_double matches "key"=value * * @return true if keyword was found in buffer @@ -916,7 +1112,7 @@ bool CmdLine::parse_double(const char *key, double& doubleVar) { char valueBuf[16]; - char *end; + char *end; // buffer required by strtof // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen); if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf))) { @@ -938,7 +1134,7 @@ bool CmdLine::parse_float(const char *key, float& floatVar) { char valueBuf[16]; - char *end; + char *end; // buffer required by strtof // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen); if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf))) {