Skip to content

Commit

Permalink
Merge pull request #781 from jannic/embedded_io_reader_weiter
Browse files Browse the repository at this point in the history
Implement embedded_io traits for Reader/Writer
  • Loading branch information
jannic authored Apr 13, 2024
2 parents cdc542f + 0d819d3 commit 9306210
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rp2040-hal/src/uart/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ impl<D: UartDevice, P: ValidUartPinout<D>> Reader<D, P> {
}
}

impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::ErrorType for Reader<D, P> {
type Error = ReadErrorType;
}

impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::Read for Reader<D, P> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
nb::block!(self.read_raw(buf)).map_err(|e| e.err_type)
}
}

impl<D: UartDevice, P: ValidUartPinout<D>> Read02<u8> for Reader<D, P> {
type Error = ReadErrorType;

Expand Down
15 changes: 15 additions & 0 deletions rp2040-hal/src/uart/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ impl<D: UartDevice, P: ValidUartPinout<D>> Writer<D, P> {
}
}

impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::ErrorType for Writer<D, P> {
type Error = Infallible;
}

impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::Write for Writer<D, P> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.write_full_blocking(buf);
Ok(buf.len())
}
fn flush(&mut self) -> Result<(), Self::Error> {
nb::block!(transmit_flushed(&self.device)).unwrap(); // Infallible
Ok(())
}
}

impl<D: UartDevice, P: ValidUartPinout<D>> Write02<u8> for Writer<D, P> {
type Error = Infallible;

Expand Down

0 comments on commit 9306210

Please sign in to comment.