| 137 | } |
| 138 | |
| 139 | bool readStringUntil(sstream& out, char delimiter, char skipChar, fl::optional<u32> timeoutMs) { |
| 140 | // Follows Arduino Serial.readStringUntil() API - blocks until delimiter found |
| 141 | u32 startTime = fl::millis(); |
| 142 | |
| 143 | // Read characters until we find delimiter or timeout |
| 144 | while (true) { |
| 145 | // Check timeout (only if timeout is set) |
| 146 | if (timeoutMs.has_value()) { |
| 147 | if (fl::millis() - startTime >= timeoutMs.value()) { |
| 148 | // Timeout occurred |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Try to read next character |
| 154 | int c = read(); |
| 155 | |
| 156 | // Handle -1 (no data available) like Arduino's timedRead(): |
| 157 | // Keep trying until timeout (or forever if no timeout set) |
| 158 | if (c == -1) { |
| 159 | // Brief 1us yield to prevent busy loop without the 1ms |
| 160 | // minimum sleep that fl::delay(1) imposes on FreeRTOS |
| 161 | // (vTaskDelay(1) = 1 tick = 1ms). The 1ms delay was too |
| 162 | // slow for USB CDC multi-packet assembly (64-byte packets). |
| 163 | fl::delayMicroseconds(1); |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | // Found delimiter - complete |
| 168 | if (c == delimiter) { |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | // Skip specified character (e.g., '\r' for cross-platform line endings) |
| 173 | if (c == skipChar) { |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | // Valid character - add to output stream |
| 178 | out << static_cast<char>(c); |
| 179 | } |
| 180 | |
| 181 | // Successfully read until delimiter |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | fl::optional<fl::string> readLine(char delimiter, char skipChar, fl::optional<u32> timeoutMs) { |
| 186 | // Try platform-native line reading first (e.g., Arduino's Serial.readStringUntil). |
no test coverage detected