Read from read loop remove generator logic
Fix #224
Description
Using yield and async generator is not efficient logic for reading the serial port.
This PR introduces readloop as an always reading to a buffer and SLIP parsing from buffer. Seems to be more reliable.
This pull request refactors the serial communication logic to simplify data reading and improve reliability. The main changes focus on removing asynchronous generators in favor of simpler async functions, introducing a new sleep utility, and enhancing debugging and buffer management. The updates affect both the esploader.ts and webserial.ts files, resulting in more maintainable and understandable code.
Serial Communication Refactor:
- Replaced the asynchronous generator-based read logic in
Transportwith a simpler async function, removingnewReadand generator-basedreadLoop. The newreadmethod now waits for data with a timeout and processes available bytes in a straightforward loop. (src/webserial.ts) [1] [2] - Updated all usages of
transport.read().next()andtransport.newRead()inESPLoaderto use the newtransport.read()interface, simplifying packet reading and improving consistency. (src/esploader.ts) [1] [2] [3] [4]
Utility and Buffer Management:
- Added a new
sleeputility function inutil.tsand replaced all internal_sleepusages with this shared function. (src/util.ts,src/esploader.ts,src/webserial.ts) [1] [2] [3] [4] - Added a
peek()method toTransportfor safely inspecting the buffer contents without consuming data, improving boot log detection logic. (src/webserial.ts,src/esploader.ts) [1] [2]
Debugging and Logging Enhancements:
- Added detailed debug logs for register reads and boot/download mode detection to aid troubleshooting and development. (
src/esploader.ts) [1] [2]
Bug Fixes and Logic Improvements:
- Improved SLIP packet parsing: after a packet is found, any remaining bytes are now correctly preserved in the buffer for future reads. (
src/webserial.ts) [1] [2] [3] - Updated regular expression for boot log detection to more reliably match multiline logs. (
src/esploader.ts)
These changes collectively make the serial communication code easier to understand, more robust, and better suited for debugging and extension.
Testing
Manual testing using examples/typescript commands.
Checklist
Before submitting a Pull Request, please ensure the following:
- [ ] 🚨 This PR does not introduce breaking changes.
- [ ] All CI checks (GH Actions) pass.
- [ ] Documentation is updated as needed.
- [ ] Tests are updated or added as necessary.
- [ ] Code is well-commented, especially in complex areas.
- [ ] Git history is clean — commits are squashed to the minimum necessary.