Keisuke Sehara
/
STM32_Whisking
fast-feedback virtual target task code on STM Nucleo
Diff: utils.h
- Revision:
- 1:871d3066c2ab
- Child:
- 8:973dcd190672
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils.h Mon May 14 17:37:26 2018 +0000 @@ -0,0 +1,43 @@ +#ifndef UTILS_H_ +#define UTILS_H_ +#include "IO.h" + +inline bool isWhitespace(const char& ch) { + return (ch == ' ') || + (ch == '\t') || + (ch == '\r') || + (ch == '\n') || + (ch == '\v') || + (ch == '\f'); +} + +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