pw_sys_io: Add TryReadByte(byte* dest) function
This adds a non-blocking version of ReadByte() that enables writing, for
example, a superloop that combines custom processing simultaneously with
byte input handling for the RPC subsystem.
Change-Id: I9cb1e6ae8c944ca7cdff6d4bff101ac52e80b53a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/23980
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Keir Mierle <keir@google.com>
diff --git a/pw_sys_io_arduino/sys_io_arduino.cc b/pw_sys_io_arduino/sys_io_arduino.cc
index c06f3a9..de6c344 100644
--- a/pw_sys_io_arduino/sys_io_arduino.cc
+++ b/pw_sys_io_arduino/sys_io_arduino.cc
@@ -35,11 +35,17 @@
Status ReadByte(std::byte* dest) {
while (true) {
- if (Serial.available()) {
- *dest = static_cast<std::byte>(Serial.read());
- break;
+ if (TryReadByte(dest).ok()) {
+ return Status::Ok();
}
}
+}
+
+Status TryReadByte(std::byte* dest) {
+ if (!Serial.available()) {
+ return Status::Unavailable();
+ }
+ *dest = static_cast<std::byte>(Serial.read());
return Status::Ok();
}