Keisuke Sehara
/
STM32_Whisking
fast-feedback virtual target task code on STM Nucleo
Diff: core/utils.h
- Revision:
- 26:b4421d1ee57a
- Parent:
- 23:61af0317e404
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/utils.h Thu Jul 05 20:15:37 2018 +0000 @@ -0,0 +1,66 @@ +#ifndef UTILS_H_ +#define UTILS_H_ +#include <string> +#include "IO.h" + +inline bool isWhitespace(const char& ch) { + return (ch == ' ') || + (ch == '\t') || + (ch == '\r') || + (ch == '\n') || + (ch == '\v') || + (ch == '\f'); +} + +inline us_timestamp_t ms_to_us(const uint16_t& ms) +{ + return ((us_timestamp_t)ms)*1000; +} + +inline std::string uint64_to_str(uint64_t value, const uint16_t& base=10) +{ + std::string result; + + do { + char c = value % base; + value /= base; + + if (c < 10) + c +='0'; + else + c += 'A' - 10; + result = c + result; + } while (value); + return result; +} + +template <typename V> +V parseUnsignedFromSerial(const V& defaultvalue) +{ + V value = 0; + while(true) { + + int readChar = IO::getc(); + + // only accepts digits + if ((readChar >= 48) && (readChar <= 57)) { + value = value * 10 + (readChar - 48); + // continues parsing + + } else if ( isWhitespace((char)readChar) || (readChar == 59) ) { + // space or ';' + // ends parsing + break; + + } else { + IO::error("%c",(char)readChar); + // set value back to original + value = defaultvalue; + break; + + } + } + + return value; +} +#endif \ No newline at end of file