FastLED 3.9.15
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 225 of file serial.cpp.hpp.

225 {
226 bool negative = false;
227 long value = 0;
228 bool foundDigit = false;
229 u32 startTime = fl::millis();
230
231 // Skip non-numeric characters
232 while (fl::millis() - startTime < mTimeoutMs) {
233 if (available() > 0) {
234 int c = peek();
235 if (c == -1) {
236 continue;
237 }
238
239 // Check for sign
240 if (c == '-') {
241 negative = true;
242 read(); // Consume the sign
243 break;
244 } else if (c == '+') {
245 read(); // Consume the sign
246 break;
247 } else if (c >= '0' && c <= '9') {
248 break; // Found a digit
249 } else {
250 read(); // Skip non-numeric character
251 startTime = fl::millis(); // Reset timeout
252 }
253 }
254 }
255
256 // Parse digits
257 startTime = fl::millis();
258 while (fl::millis() - startTime < mTimeoutMs) {
259 if (available() > 0) {
260 int c = peek();
261 if (c == -1) {
262 continue;
263 }
264
265 if (c >= '0' && c <= '9') {
266 value = value * 10 + (c - '0');
267 read(); // Consume the digit
268 foundDigit = true;
269 startTime = fl::millis(); // Reset timeout
270 } else {
271 break; // Non-digit, stop parsing
272 }
273 }
274 }
275
276 return foundDigit ? (negative ? -value : value) : 0;
277}
int peek()
Peek at next byte without removing it from buffer.
int available()
Check how many bytes are available to read.
int read()
Read next byte from serial input.
constexpr int type_rank< T >::value
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.

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

+ Here is the call graph for this function: