From 3fe2d062f2a4f89ccce2ea97d1b8407765f74c37 Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Fri, 20 Dec 2019 13:13:05 +0100 Subject: [PATCH] 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 --- Cargo.toml | 2 +- src/led_ring.rs | 27 ++++++++++++++++----------- src/main.rs | 8 ++++---- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b6cff98..050be06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/led_ring.rs b/src/led_ring.rs index 7fc7f49..2d48946 100644 --- a/src/led_ring.rs +++ b/src/led_ring.rs @@ -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 { impl LedRing where - LED: OutputPin, + LED: OutputPin, { /// Sets up the LED ring using using four LED GPIO outputs. pub fn from(leds: [LED; 4]) -> LedRing { @@ -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(()) } } diff --git a/src/main.rs b/src/main.rs index ae75b15..a6e2b33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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