diff --git a/src/blocking/mod.rs b/src/blocking/mod.rs index 51d444c..ce8497d 100644 --- a/src/blocking/mod.rs +++ b/src/blocking/mod.rs @@ -6,4 +6,5 @@ pub mod delay; pub mod i2c; +pub mod serial; pub mod spi; diff --git a/src/blocking/serial.rs b/src/blocking/serial.rs new file mode 100644 index 0000000..26d30fe --- /dev/null +++ b/src/blocking/serial.rs @@ -0,0 +1,57 @@ +//! Blocking serial API + + +/// Write half of a serial interface (blocking variant) +pub trait Write { + /// The type of error that can occur when writing + type Error; + + /// Writes a slice, blocking until everything has been written + /// + /// An implementation can choose to buffer the write, returning `Ok(())` + /// after the complete slice has been written to a buffer, but before all + /// words have been sent via the serial interface. To make sure that + /// everything has been sent, call [`bflush`] after this function returns. + /// + /// [`bflush`]: #tymethod.bflush + fn bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error>; + + /// Block until the serial interface has sent all buffered words + fn bflush(&mut self) -> Result<(), Self::Error>; +} + + +/// Blocking serial write +pub mod write { + /// Marker trait to opt into default blocking write implementation + /// + /// Implementers of [`serial::Write`] can implement this marker trait + /// for their type. Doing so will automatically provide the default + /// implementation of [`blocking::serial::Write`] for the type. + /// + /// [`serial::Write`]: ../../serial/trait.Write.html + /// [`blocking::serial::Write`]: ../trait.Write.html + pub trait Default: ::serial::Write {} + + impl ::blocking::serial::Write for S + where + S : Default, + Word: Clone, + { + type Error = S::Error; + + + fn bwrite_all(&mut self, buffer: &[Word]) -> Result<(), Self::Error> { + for word in buffer { + block!(self.write(word.clone()))?; + } + + Ok(()) + } + + fn bflush(&mut self) -> Result<(), Self::Error> { + block!(self.flush())?; + Ok(()) + } + } +} diff --git a/src/prelude.rs b/src/prelude.rs index 437709d..a1fdba4 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -13,6 +13,16 @@ pub use ::Qei as _embedded_hal_Qei; pub use ::timer::CountDown as _embedded_hal_timer_CountDown; pub use ::blocking::delay::DelayMs as _embedded_hal_blocking_delay_DelayMs; pub use ::blocking::delay::DelayUs as _embedded_hal_blocking_delay_DelayUs; +pub use ::blocking::i2c::{ + Read as _embedded_hal_blocking_i2c_Read, + Write as _embedded_hal_blocking_i2c_Write, + WriteRead as _embedded_hal_blocking_i2c_WriteRead, +}; +pub use ::blocking::serial::Write as _embedded_hal_blocking_serial_Write; +pub use ::blocking::spi::{ + Transfer as _embedded_hal_blocking_spi_Transfer, + Write as _embedded_hal_blocking_spi_Write, +}; pub use ::digital::OutputPin as _embedded_hal_digital_OutputPin; #[cfg(feature = "unproven")] pub use ::digital::InputPin as _embedded_hal_digital_InputPin;