Port example to stm32f4xx-hal version 0.6

* Switch to the `embedded_hal::digital::v2::OutputPin` trait
* Handle all infallable `set_low`/`set_high` calls within the
  `led_ring` module
This commit is contained in:
Paul van Tilburg 2019-12-20 13:13:05 +01:00
parent 4a57a751fb
commit 3fe2d062f2
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
3 changed files with 21 additions and 16 deletions

View File

@ -20,7 +20,7 @@ version = "0.5.0"
[dependencies.hal]
package = "stm32f4xx-hal"
version = "0.3.0"
version = "0.6.0"
features = ["rt", "stm32f407"]
[lib]

View File

@ -1,6 +1,7 @@
//! Module for manipulating the LED ring.
use hal::prelude::_embedded_hal_digital_OutputPin as OutputPin;
use core::convert::Infallible;
use hal::prelude::_embedded_hal_digital_v2_OutputPin as OutputPin;
/// The cycle direction of the LED ring.
///
@ -52,7 +53,7 @@ pub struct LedRing<LED> {
impl<LED> LedRing<LED>
where
LED: OutputPin,
LED: OutputPin<Error = Infallible>,
{
/// Sets up the LED ring using using four LED GPIO outputs.
pub fn from(leds: [LED; 4]) -> LedRing<LED> {
@ -114,8 +115,8 @@ where
pub fn advance(&mut self) {
let num_leds = self.leds.len();
self.leds[self.index].set_high();
self.leds[(self.index + 2) % num_leds].set_low();
self.leds[self.index].set_high().unwrap();
self.leds[(self.index + 2) % num_leds].set_low().unwrap();
self.index = match self.direction {
Direction::Clockwise => (self.index + 1) % num_leds,
@ -128,7 +129,7 @@ where
/// This is done immediately, regardless of the current mode.
pub fn all_on(&mut self) {
for led in self.leds.iter_mut() {
led.set_high();
led.set_high().unwrap();
}
}
@ -137,7 +138,7 @@ where
/// This is done immediately, regardless of the current mode.
pub fn all_off(&mut self) {
for led in self.leds.iter_mut() {
led.set_low();
led.set_low().unwrap();
}
}
@ -148,9 +149,9 @@ where
pub fn specific_on(&mut self, directions: [bool; 4]) {
for (led, on_off) in self.leds.iter_mut().zip(directions.iter()) {
if *on_off {
led.set_high();
led.set_high().unwrap();
} else {
led.set_low();
led.set_low().unwrap();
}
}
}
@ -164,7 +165,7 @@ where
#[cfg(test)]
mod tests {
use super::{Direction, LedRing, Mode, OutputPin};
use super::{Direction, Infallible, LedRing, Mode, OutputPin};
#[derive(Debug, Eq, PartialEq)]
struct MockOutputPin {
@ -183,12 +184,16 @@ mod tests {
}
impl OutputPin for MockOutputPin {
fn set_high(&mut self) {
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.state = true;
Ok(())
}
fn set_low(&mut self) {
fn set_low(&mut self) -> Result<(), Self::Error> {
self.state = false;
Ok(())
}
}

View File

@ -108,9 +108,9 @@ const APP: () = {
let mut accel_cs = gpioe.pe3.into_push_pull_output();
// Initialize the accelerometer.
accel_cs.set_low();
accel_cs.set_low().unwrap();
let _ = accel.transfer(&mut [0x20, 0b01000111]).unwrap();
accel_cs.set_high();
accel_cs.set_high().unwrap();
// Output to the serial interface that initialisation is finished.
writeln!(serial_tx, "init\r").unwrap();
@ -150,13 +150,13 @@ const APP: () = {
/// and schedules the next trigger (if enabled).
#[task(schedule = [accel_leds], resources = [accel, accel_cs, led_ring, serial_tx])]
fn accel_leds(mut cx: accel_leds::Context) {
cx.resources.accel_cs.set_low();
cx.resources.accel_cs.set_low().unwrap();
let read_command = (1 << 7) | (1 << 6) | 0x29;
let mut commands = [read_command, 0x0, 0x0, 0x0];
let result = cx.resources.accel.transfer(&mut commands[..]).unwrap();
let acc_x = result[1] as i8;
let acc_y = result[3] as i8;
cx.resources.accel_cs.set_high();
cx.resources.accel_cs.set_high().unwrap();
if acc_x == 0 && acc_y == 0 {
cx.resources