use core::fmt::Write;
struct Inner<W> {
uart: W,
}
pub struct UartWriter<W> {
inner: Inner<W>,
}
impl<W> UartWriter<W>
where
W: embedded_io::Write,
{
pub fn new(uart: W) -> Self {
Self {
inner: Inner { uart },
}
}
pub fn write_fmt(&mut self, args: core::fmt::Arguments<'_>) {
self.inner.write_fmt(args).unwrap();
}
}
impl<W> core::fmt::Write for Inner<W>
where
W: embedded_io::Write,
{
fn write_str(&mut self, s: &str) -> core::fmt::Result {
assert!(s.is_ascii());
for b in s.bytes() {
match b {
b'\r' => (),
b'\n' => {
self.uart.write_all(b"\r\n").unwrap()
}
_ => self.uart.write_all(core::slice::from_ref(&b)).unwrap(),
}
}
self.uart.flush().unwrap();
Ok(())
}
}