FastLED 3.10.8
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 247 of file serial.h.

247 {
248 // Follows Arduino Serial.readStringUntil() API - blocks until delimiter found
249 fl::sstream buffer;
250
251 u32 startTime = fl::millis();
252
253 // Read characters until we find delimiter or timeout
254 while (true) {
255 // Check timeout (only if timeout is set)
256 if (timeoutMs.has_value()) {
257 if (fl::millis() - startTime >= timeoutMs.value()) {
258 // Timeout occurred - return nullopt
259 return fl::nullopt;
260 }
261 }
262
263 // Try to read next character
264 int c = serial.read();
265
266 // Handle -1 (no data available) like Arduino's timedRead():
267 // Keep trying until timeout (or forever if no timeout set)
268 if (c == -1) {
269 // Brief 1us yield instead of 1ms delay for fast USB CDC polling.
271 continue;
272 }
273
274 // Found delimiter - complete
275 if (c == delimiter) {
276 break;
277 }
278
279 // Skip specified character (e.g., '\r' for cross-platform line endings)
280 if (c == skipChar) {
281 continue;
282 }
283
284 // Valid character - add to buffer
285 buffer << static_cast<char>(c);
286 }
287
288 // Convert to string and trim whitespace
289 fl::string result = buffer.str();
290 result.trim();
291 return result;
292}
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: