Auto merge of #34 - hannobraun:blocking-serial-write, r=japaric

Add blocking serial write implementation

Also adds all blocking traits to the prelude that weren't there already.

There are failign doc tests, but those seem unrelated to this change.
This commit is contained in:
homunkulus 2018-02-08 13:35:00 +00:00
commit 59ef38186f
3 changed files with 68 additions and 0 deletions

View File

@ -6,4 +6,5 @@
pub mod delay;
pub mod i2c;
pub mod serial;
pub mod spi;

57
src/blocking/serial.rs Normal file
View File

@ -0,0 +1,57 @@
//! Blocking serial API
/// Write half of a serial interface (blocking variant)
pub trait Write<Word> {
/// 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<Word>: ::serial::Write<Word> {}
impl<S, Word> ::blocking::serial::Write<Word> for S
where
S : Default<Word>,
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(())
}
}
}

View File

@ -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;