FastLED 3.10.9
Loading...
Searching...
No Matches

◆ readSerialStringUntil()

template<typename SerialIn>
fl::optional< fl::string > fl::readSerialStringUntil ( SerialIn & serial,
char delimiter,
char skipChar,
fl::optional< u32 > timeoutMs )

Definition at line 264 of file serial.h.

264 {
265 // Follows Arduino Serial.readStringUntil() API - blocks until delimiter found
266 fl::sstream buffer;
267
268 u32 startTime = fl::millis();
269
270 // Read characters until we find delimiter or timeout
271 while (true) {
272 // Check timeout (only if timeout is set)
273 if (timeoutMs.has_value()) {
274 if (fl::millis() - startTime >= timeoutMs.value()) {
275 // Timeout occurred - return nullopt
276 return fl::nullopt;
277 }
278 }
279
280 // Try to read next character
281 int c = serial.read();
282
283 // Handle -1 (no data available) like Arduino's timedRead():
284 // Keep trying until timeout (or forever if no timeout set)
285 if (c == -1) {
286 // Brief 1us yield instead of 1ms delay for fast USB CDC polling.
288 continue;
289 }
290
291 // Found delimiter - complete
292 if (c == delimiter) {
293 break;
294 }
295
296 // Skip specified character (e.g., '\r' for cross-platform line endings)
297 if (c == skipChar) {
298 continue;
299 }
300
301 // Valid character - add to buffer
302 buffer << static_cast<char>(c);
303 }
304
305 // Convert to string and trim whitespace
306 fl::string result = buffer.str();
307 result.trim();
308 return result;
309}
T & value() FL_NO_EXCEPT
Definition optional.h:112
bool has_value() const FL_NO_EXCEPT
Definition optional.h:42
string str() const FL_NO_EXCEPT
Definition strstream.h:43
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
constexpr nullopt_t nullopt
Definition optional.h:13
void delayMicroseconds(u32 us) FL_NO_EXCEPT
Delay for a given number of microseconds.

References delayMicroseconds(), FL_NO_EXCEPT, millis(), nullopt, and fl::sstream::str().

Referenced by readSerialLine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: