FastLED 3.10.8
Loading...
Searching...
No Matches

◆ parseInt()

long fl::SerialPort::parseInt ( )

Parse integer from serial input.

Returns
Parsed integer value, or 0 if no valid integer found

Skips non-numeric characters until a number is found, then parses it. Stops at first non-numeric character after the number.

Definition at line 222 of file serial.cpp.hpp.

222 {
223 bool negative = false;
224 long value = 0;
225 bool foundDigit = false;
226 u32 startTime = fl::millis();
227
228 // Skip non-numeric characters
229 while (fl::millis() - startTime < mTimeoutMs) {
230 if (available() > 0) {
231 int c = peek();
232 if (c == -1) {
233 continue;
234 }
235
236 // Check for sign
237 if (c == '-') {
238 negative = true;
239 read(); // Consume the sign
240 break;
241 } else if (c == '+') {
242 read(); // Consume the sign
243 break;
244 } else if (c >= '0' && c <= '9') {
245 break; // Found a digit
246 } else {
247 read(); // Skip non-numeric character
248 startTime = fl::millis(); // Reset timeout
249 }
250 }
251 }
252
253 // Parse digits
254 startTime = fl::millis();
255 while (fl::millis() - startTime < mTimeoutMs) {
256 if (available() > 0) {
257 int c = peek();
258 if (c == -1) {
259 continue;
260 }
261
262 if (c >= '0' && c <= '9') {
263 value = value * 10 + (c - '0');
264 read(); // Consume the digit
265 foundDigit = true;
266 startTime = fl::millis(); // Reset timeout
267 } else {
268 break; // Non-digit, stop parsing
269 }
270 }
271 }
272
273 return foundDigit ? (negative ? -value : value) : 0;
274}
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.

References available(), fl::FL_NO_EXCEPT, fl::millis(), mTimeoutMs, peek(), read(), and fl::type_rank< T >::value.

+ Here is the call graph for this function: