Skip to content

Change traits to take &mut self, where appropriate #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 17, 2017
169 changes: 105 additions & 64 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@
//! fn get_timeout(&self) -> Self::Time;
//!
//! /// Pauses the timer
//! fn pause(&self);
//! fn pause(&mut self);
//!
//! /// Restarts the timer count
//! fn restart(&self);
//! fn restart(&mut self);
//!
//! /// Resumes the timer count
//! fn resume(&self);
//! fn resume(&mut self);
//!
//! /// Sets a new timeout
//! fn set_timeout<T>(&self, ticks: T) where T: Into<Self::Time>;
//! fn set_timeout<T>(&mut self, ticks: T) where T: Into<Self::Time>;
//!
//! /// "waits" until the timer times out
//! fn wait(&self) -> nb::Result<(), !>;
Expand All @@ -117,6 +117,7 @@
//! ``` ignore
//! //! An implementation of the `embedded-hal` for STM32F103xx microcontrollers
//!
//! extern crate core;
//! extern crate embedded_hal as hal;
//! extern crate nb;
//!
Expand All @@ -130,40 +131,50 @@
//! // and USART3
//! pub struct Serial<'a, U>(pub &'a U)
//! where
//! U: Deref<stm32f103xx::usart1::RegisterBlock> + 'static;
//! U: Deref<Target=stm32f103xx::usart1::RegisterBlock> + 'static;
//!
//! /// Serial interface error
//! pub enum Error {
//! /// Buffer overrun
//! Overrun,
//! ..
//! // add more error variants here
//! }
//!
//! impl<'a, U> hal::Serial for Serial<'a, U> {
//! impl<'a, U> hal::serial::Read<u8> for Serial<'a, U>
//! where
//! U: Deref<Target=stm32f103xx::usart1::RegisterBlock> + 'static
//! {
//! type Error = Error;
//!
//! fn read(&self) -> nb::Result<u8, Error> {
//! fn read(&mut self) -> nb::Result<u8, Error> {
//! // read the status register
//! let sr = self.sr.read();
//! let sr = self.0.sr.read();
//!
//! if sr.ovr().bit_is_set() {
//! if sr.ore().bit_is_set() {
//! // Error: Buffer overrun
//! Err(nb::Error::Other(Error::Overrun))
//! } else if .. {
//! // Other error conditions
//! ..
//! } else if sr.rxne().bit_is_set() {
//! }
//! // Add additional `else if` statements to check for other errors
//! else if sr.rxne().bit_is_set() {
//! // Data available: read the data register
//! Ok(self.dr.read())
//! } else {
//! Ok(self.0.dr.read().bits() as u8)
//! }
//! else {
//! // No data available yet
//! Err(nb::Error::WouldBlock)
//! }
//! }
//! }
//!
//! impl<'a, U> hal::serial::Write<u8> for Serial<'a, U>
//! where
//! U: Deref<Target=stm32f103xx::usart1::RegisterBlock> + 'static
//! {
//! type Error = Error;
//!
//! fn write(&self, byte: u8) -> nb::Result<(), Error> {
//! fn write(&mut self, byte: u8) -> nb::Result<(), Error> {
//! // Very similar to the above implementation
//! ..
//! Ok(())
//! }
//! }
//! ```
Expand All @@ -179,16 +190,21 @@
//! implementation:
//!
//! ``` ignore
//! extern crate embedded_hal as hal;
//! extern crate nb;
//!
//! /// A synchronized serial interface
//! // NOTE This is a global singleton
//! pub struct Serial1;
//!
//! // NOTE private
//! static USART1: Mutex<_> = Mutex::new(..);
//!
//! impl hal::Serial for Serial {
//! fn read(&self) -> Result<u8, nb::Error<Error>> {
//! hal::Serial::read(&Serial(USART1.lock()))
//! impl hal::serial::Read<u8> for Serial1 {
//! type Error = !;
//!
//! fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
//! hal::serial::Read::read(&Serial(&*USART1.lock()))
//! }
//! }
//! ```
Expand Down Expand Up @@ -242,41 +258,58 @@
//! extern crate nb;
//!
//! use hal::prelude::*;
//! use futures::{
//! future,
//! Async,
//! Future,
//! };
//! use futures::future::Loop;
//! use stm32f103xx_hal_impl::{Serial, Timer};
//!
//! /// `futures` version of `Timer.wait`
//! ///
//! /// This returns a future that must be polled to completion
//! fn wait<T>(timer: &T) -> impl Future<Item = (), Error = !>
//! fn wait<T>(timer: T) -> impl Future<Item = T, Error = !>
//! where
//! T: hal::Timer,
//! {
//! future::poll_fn(|| {
//! Ok(Async::Ready(try_nb!(timer.wait())))
//! future::loop_fn(timer, |timer| {
//! match timer.wait() {
//! Ok(()) => Ok(Loop::Break(timer)),
//! Err(nb::Error::WouldBlock) => Ok(Loop::Continue(timer)),
//! }
//! })
//! }
//!
//! /// `futures` version of `Serial.read`
//! ///
//! /// This returns a future that must be polled to completion
//! fn read<S>(serial: &S) -> impl Future<Item = u8, Error = Error>
//! fn read<S>(serial: S) -> impl Future<Item = (S, u8), Error = S::Error>
//! where
//! S: hal::Serial,
//! S: hal::serial::Read<u8>,
//! {
//! future::poll_fn(|| {
//! Ok(Async::Ready(try_nb!(serial.read())))
//! future::loop_fn(serial, |mut serial| {
//! match serial.read() {
//! Ok(byte) => Ok(Loop::Break((serial, byte))),
//! Err(nb::Error::WouldBlock) => Ok(Loop::Continue(serial)),
//! Err(nb::Error::Other(error)) => Err(error),
//! }
//! })
//! }
//!
//! /// `futures` version of `Serial.write`
//! ///
//! /// This returns a future that must be polled to completion
//! fn write<S>(byte: u8) -> impl Future<Item = (), Error = Error>
//! fn write<S>(serial: S, byte: u8) -> impl Future<Item = S, Error = S::Error>
//! where
//! S: hal::Serial,
//! S: hal::serial::Write<u8>,
//! {
//! future::poll_fn(|| {
//! Ok(Async::Ready(try_nb!(serial.write(byte))))
//! future::loop_fn(serial, move |mut serial| {
//! match serial.write(byte) {
//! Ok(()) => Ok(Loop::Break(serial)),
//! Err(nb::Error::WouldBlock) => Ok(Loop::Continue(serial)),
//! Err(nb::Error::Other(error)) => Err(error),
//! }
//! })
//! }
//!
Expand All @@ -285,7 +318,7 @@
//! let serial = Serial(usart1);
//!
//! // Tasks
//! let mut blinky = future::loop_fn(true, |_| {
//! let mut blinky = future::loop_fn(true, |state| {
//! wait(timer).map(|_| {
//! if state {
//! Led.on();
Expand Down Expand Up @@ -381,14 +414,16 @@
//!
//! ``` ignore
//! extern crate embedded_hal as hal;
//! #[macro_use]
//! extern crate nb;
//!
//! use hal::prelude::*;
//!
//! fn write_all<S>(serial: &S, buffer: &[u8]) -> Result<(), S::Error>
//! fn write_all<S>(serial: &mut S, buffer: &[u8]) -> Result<(), S::Error>
//! where
//! S: hal::Serial
//! S: hal::serial::Write<u8>
//! {
//! for byte in buffer {
//! for &byte in buffer {
//! block!(serial.write(byte))?;
//! }
//!
Expand All @@ -400,23 +435,24 @@
//!
//! ``` ignore
//! extern crate embedded_hal as hal;
//! extern crate nb;
//!
//! use hal::prelude::*;
//!
//! enum Error<E> {
//! /// Serial interface error
//! Serial(E)
//! Serial(E),
//! TimedOut,
//! }
//!
//! fn read_with_timeout(
//! serial: &S,
//! timer: &T,
//! timeout: T::Ticks,
//! fn read_with_timeout<S, T>(
//! serial: &mut S,
//! timer: &mut T,
//! timeout: T::Time,
//! ) -> Result<u8, Error<S::Error>>
//! where
//! T: hal::Timer,
//! S: hal::Serial,
//! S: hal::serial::Read<u8>,
//! {
//! timer.pause();
//! timer.restart();
Expand All @@ -433,6 +469,12 @@
//! }
//!
//! match timer.wait() {
//! Err(nb::Error::Other(e)) => {
//! // The error type specified by `timer.wait()` is `!`, which
//! // means no error can actually occur. The Rust compiler
//! // still forces us to provide this match arm, though.
//! e
//! },
//! Err(nb::Error::WouldBlock) => continue,
//! Ok(()) => {
//! timer.pause();
Expand Down Expand Up @@ -477,9 +519,9 @@
//!
//! use hal::prelude::*;
//!
//! fn flush(serial: &S, cb: &mut CircularBuffer) -> Result<(), S::Error>
//! fn flush<S>(serial: &mut S, cb: &mut CircularBuffer) -> Result<(), S::Error>
//! where
//! S: hal::Serial,
//! S: hal::serial::Write<u8>,
//! {
//! loop {
//! if let Some(byte) = cb.peek() {
Expand All @@ -501,30 +543,29 @@
//!
//! // NOTE private
//! static BUFFER: Mutex<CircularBuffer> = ..;
//! static SERIAL: Mutex<impl hal::Serial> = ..;
//! static SERIAL: Mutex<impl hal::serial::Write<u8>> = ..;
//!
//! impl BufferedSerial {
//! pub fn write(&self, byte: u8) {
//! pub fn write(&self, bytes: &[u8]) {
//! let mut buffer = BUFFER.lock();
//! for byte in byte {
//! for byte in bytes {
//! buffer.push(*byte).unwrap();
//! }
//! }
//!
//! pub fn write_all(&self, bytes: &[u8]) {
//! let mut buffer = BUFFER.lock();
//! for byte in bytes {
//! cb.push(*byte).unwrap();
//! buffer.push(*byte).unwrap();
//! }
//! }
//!
//! }
//!
//! fn interrupt_handler() {
//! let serial = SERIAL.lock();
//! let buffer = BUFFER.lock();
//!
//! flush(&serial, &mut buffer).unwrap();
//! flush(&mut serial, &mut buffer).unwrap();
//! }
//! ```
//!
Expand Down Expand Up @@ -619,21 +660,21 @@ pub trait Capture {
/// NOTE that you must multiply the returned value by the *resolution* of
/// this `Capture` interface to get a human time unit (e.g. seconds)
fn capture(
&self,
&mut self,
channel: Self::Channel,
) -> nb::Result<Self::Capture, Self::Error>;

/// Disables a capture `channel`
fn disable(&self, channel: Self::Channel);
fn disable(&mut self, channel: Self::Channel);

/// Enables a capture `channel`
fn enable(&self, channel: Self::Channel);
fn enable(&mut self, channel: Self::Channel);

/// Returns the current resolution
fn get_resolution(&self) -> Self::Time;

/// Sets the resolution of the capture timer
fn set_resolution<R>(&self, resolution: R)
fn set_resolution<R>(&mut self, resolution: R)
where
R: Into<Self::Time>;
}
Expand Down Expand Up @@ -674,10 +715,10 @@ pub trait Pwm {
type Duty;

/// Disables a PWM `channel`
fn disable(&self, channel: Self::Channel);
fn disable(&mut self, channel: Self::Channel);

/// Enables a PWM `channel`
fn enable(&self, channel: Self::Channel);
fn enable(&mut self, channel: Self::Channel);

/// Returns the current PWM period
fn get_period(&self) -> Self::Time;
Expand All @@ -689,10 +730,10 @@ pub trait Pwm {
fn get_max_duty(&self) -> Self::Duty;

/// Sets a new duty cycle
fn set_duty(&self, channel: Self::Channel, duty: Self::Duty);
fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty);

/// Sets a new PWM period
fn set_period<P>(&self, period: P)
fn set_period<P>(&mut self, period: P)
where
P: Into<Self::Time>;
}
Expand Down Expand Up @@ -764,10 +805,10 @@ pub trait Spi<Word> {
///
/// **NOTE** A word must be sent to the slave before attempting to call this
/// method.
fn read(&self) -> nb::Result<Word, Self::Error>;
fn read(&mut self) -> nb::Result<Word, Self::Error>;

/// Sends a word to the slave
fn send(&self, word: Word) -> nb::Result<(), Self::Error>;
fn send(&mut self, word: Word) -> nb::Result<(), Self::Error>;
}

/// Timer used for timeouts
Expand Down Expand Up @@ -796,16 +837,16 @@ pub trait Timer {
fn get_timeout(&self) -> Self::Time;

/// Pauses the timer
fn pause(&self);
fn pause(&mut self);

/// Restarts the timer count to zero
fn restart(&self);
fn restart(&mut self);

/// Resumes the timer count
fn resume(&self);
fn resume(&mut self);

/// Sets a new timeout
fn set_timeout<T>(&self, timeout: T)
fn set_timeout<T>(&mut self, timeout: T)
where
T: Into<Self::Time>;

Expand Down
Loading