Parse floating-point number from serial input.
Skips non-numeric characters until a number is found, then parses it. Stops at first non-numeric character after the number.
276 {
277 bool negative = false;
278 long intPart = 0;
279 long fracPart = 0;
280 int fracDigits = 0;
281 bool foundDigit = false;
282 bool inFraction = false;
284
285
289 if (c == -1) {
290 continue;
291 }
292
293
294 if (c == '-') {
295 negative = true;
297 break;
298 } else if (c == '+') {
300 break;
301 } else if (c >= '0' && c <= '9') {
302 break;
303 } else if (c == '.') {
304 break;
305 } else {
308 }
309 }
310 }
311
312
317 if (c == -1) {
318 continue;
319 }
320
321 if (c == '.' && !inFraction) {
322 inFraction = true;
324 foundDigit = true;
326 } else if (c >= '0' && c <= '9') {
327 if (inFraction) {
328 fracPart = fracPart * 10 + (c - '0');
329 fracDigits++;
330 } else {
331 intPart = intPart * 10 + (c - '0');
332 }
334 foundDigit = true;
336 } else {
337 break;
338 }
339 }
340 }
341
342 if (!foundDigit) {
343 return 0.0f;
344 }
345
346
347 float value =
static_cast<float>(intPart);
348 if (fracDigits > 0) {
349 float divisor = 1.0f;
350 for (int i = 0; i < fracDigits; i++) {
351 divisor *= 10.0f;
352 }
353 value +=
static_cast<float>(fracPart) / divisor;
354 }
355
357}
int read() FL_NO_EXCEPT
Read next byte from serial input.
int peek() FL_NO_EXCEPT
Peek at next byte without removing it from buffer.
int available() FL_NO_EXCEPT
Check how many bytes are available to read.
constexpr int type_rank< T >::value
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.