Skip to content

Commit

Permalink
serial settings: use eio::Write traits
Browse files Browse the repository at this point in the history
re #917
  • Loading branch information
jordens committed Jul 4, 2024
1 parent 9e2dbfe commit 2aaae9d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
11 changes: 8 additions & 3 deletions serial-settings/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ where
}
}

impl<T> core::fmt::Write for BestEffortInterface<T>
impl<T> embedded_io::Write for BestEffortInterface<T>
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<usize, T::Error> {
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(())
}
}
Expand Down
32 changes: 24 additions & 8 deletions serial-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -71,7 +70,7 @@ pub trait Platform<const Y: usize>: 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<Y>;
Expand Down Expand Up @@ -159,7 +158,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
};

write!(
&mut interface.platform.interface_mut(),
interface.platform.interface_mut(),
"{}: {value}",
path.as_str(),
)
Expand Down Expand Up @@ -187,12 +186,12 @@ impl<'a, P: Platform<Y>, 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]"
)
}
Expand Down Expand Up @@ -254,7 +253,7 @@ impl<'a, P: Platform<Y>, 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()
)
Expand Down Expand Up @@ -397,7 +396,24 @@ impl<'a, P: Platform<Y>, 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<Y>, const Y: usize> ErrorType for Interface<'a, P, Y> {
type Error = <P::Interface as ErrorType>::Error;
}

impl<'a, P: Platform<Y>, const Y: usize> Write for Interface<'a, P, Y> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.platform.interface_mut().write(buf)
}

fn flush(&mut self) -> Result<(), Self::Error> {
self.platform.interface_mut().flush()
}
}

Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down

0 comments on commit 2aaae9d

Please sign in to comment.