From 2aaae9db4c7e1e82447e4a348a024f5cffad43a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 4 Jul 2024 13:21:56 +0200 Subject: [PATCH] serial settings: use eio::Write traits re #917 --- serial-settings/src/interface.rs | 11 ++++++++--- serial-settings/src/lib.rs | 32 ++++++++++++++++++++++++-------- src/settings.rs | 1 + 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/serial-settings/src/interface.rs b/serial-settings/src/interface.rs index 167833203..dc4162fff 100644 --- a/serial-settings/src/interface.rs +++ b/serial-settings/src/interface.rs @@ -28,14 +28,19 @@ where } } -impl core::fmt::Write for BestEffortInterface +impl embedded_io::Write for BestEffortInterface where T: embedded_io::Write + embedded_io::WriteReady, { - fn write_str(&mut self, s: &str) -> core::fmt::Result { + fn write(&mut self, buf: &[u8]) -> Result { if let Ok(true) = self.0.write_ready() { - self.0.write(s.as_bytes()).ok(); + self.0.write(buf) + } else { + Ok(0) } + } + + fn flush(&mut self) -> Result<(), Self::Error> { Ok(()) } } diff --git a/serial-settings/src/lib.rs b/serial-settings/src/lib.rs index 07d81ae5b..c729e776b 100644 --- a/serial-settings/src/lib.rs +++ b/serial-settings/src/lib.rs @@ -49,8 +49,7 @@ //! can be changed if needed. #![no_std] -use core::fmt::Write; -use embedded_io::{Read, ReadReady}; +use embedded_io::{ErrorType, Read, ReadReady, Write}; use heapless::String; use miniconf::{JsonCoreSlash, Path, TreeKey}; @@ -71,7 +70,7 @@ pub trait Platform: Sized { /// This type specifies the interface to the user, for example, a USB CDC-ACM serial port. type Interface: embedded_io::Read + embedded_io::ReadReady - + core::fmt::Write; + + embedded_io::Write; /// Specifies the settings that are used on the device. type Settings: Settings; @@ -159,7 +158,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { }; write!( - &mut interface.platform.interface_mut(), + interface.platform.interface_mut(), "{}: {value}", path.as_str(), ) @@ -187,12 +186,12 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { let default_hash: u64 = yafnv::fnv1a(default_value); if default_hash != value_hash { writeln!( - &mut interface.platform.interface_mut(), + interface.platform.interface_mut(), " [default: {default_value}]" ) } else { writeln!( - &mut interface.platform.interface_mut(), + interface.platform.interface_mut(), " [default]" ) } @@ -254,7 +253,7 @@ impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> { } Ok(len) => { writeln!( - &mut interface.platform.interface_mut(), + interface.platform.interface_mut(), "{key}: {}", core::str::from_utf8(&interface.buffer[..len]).unwrap() ) @@ -397,7 +396,24 @@ impl<'a, P: Platform, const Y: usize> core::fmt::Write for Interface<'a, P, Y> { fn write_str(&mut self, s: &str) -> core::fmt::Result { - self.platform.interface_mut().write_str(s) + self.platform + .interface_mut() + .write_all(s.as_bytes()) + .or(Err(core::fmt::Error)) + } +} + +impl<'a, P: Platform, const Y: usize> ErrorType for Interface<'a, P, Y> { + type Error = ::Error; +} + +impl<'a, P: Platform, const Y: usize> Write for Interface<'a, P, Y> { + fn write(&mut self, buf: &[u8]) -> Result { + self.platform.interface_mut().write(buf) + } + + fn flush(&mut self) -> Result<(), Self::Error> { + self.platform.interface_mut().flush() } } diff --git a/src/settings.rs b/src/settings.rs index ee7358342..466673b44 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -25,6 +25,7 @@ use crate::hardware::{flash::Flash, metadata::ApplicationMetadata, platform}; use core::fmt::Write; use embassy_futures::block_on; +use embedded_io::Write as EioWrite; use heapless::{String, Vec}; use miniconf::{JsonCoreSlash, Path, Postcard, Tree}; use sequential_storage::{