diff --git a/src/led.rs b/src/led.rs new file mode 100644 index 0000000..28f2a29 --- /dev/null +++ b/src/led.rs @@ -0,0 +1,74 @@ +use hal::gpio::{Output, PushPull}; +use hal::prelude::*; + +pub type Led = hal::gpio::gpiod::PD>; + +pub enum LedDirection { + Clockwise, + CounterClockwise, +} + +impl LedDirection { + fn flip(&self) -> LedDirection { + match self { + LedDirection::Clockwise => LedDirection::CounterClockwise, + LedDirection::CounterClockwise => LedDirection::Clockwise, + } + } +} + +pub struct LedCycle { + pub enabled: bool, + pub direction: LedDirection, + pub index: usize, + pub leds: [crate::Led; 4], +} + +impl LedCycle { + pub const PERIOD: u32 = 8_000_000; + + pub fn from(leds: [crate::Led; 4]) -> LedCycle { + LedCycle { + enabled: true, + direction: LedDirection::Clockwise, + index: 0, + leds, + } + } + + pub fn disable(&mut self) { + self.enabled = false; + } + + pub fn enable(&mut self) { + self.enabled = true; + } + + pub fn reverse(&mut self) { + self.direction = self.direction.flip(); + } + + 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.index = match self.direction { + LedDirection::Clockwise => (self.index + 1) % num_leds, + LedDirection::CounterClockwise => (self.index + 3) % num_leds, + }; + } + + pub fn all_on(&mut self) { + for led in self.leds.iter_mut() { + led.set_high(); + } + } + + pub fn all_off(&mut self) { + for led in self.leds.iter_mut() { + led.set_low(); + } + } +} diff --git a/src/main.rs b/src/main.rs index 5f30d2d..8db16fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,12 @@ extern crate panic_semihosting; +mod led; + +use crate::led::{Led, LedCycle}; use core::fmt::Write; use cortex_m_semihosting::hprintln; -use hal::gpio::{Edge, ExtiPin, Floating, Input, Output, PushPull}; +use hal::gpio::{Edge, ExtiPin, Floating, Input}; use hal::prelude::*; use hal::serial::{self, config::Config as SerialConfig, Serial}; use hal::stm32::{EXTI, USART1}; @@ -15,81 +18,10 @@ use heapless::consts::U8; use heapless::Vec; use rtfm::app; -type Led = hal::gpio::gpiod::PD>; type SerialTx = hal::serial::Tx; type SerialRx = hal::serial::Rx; type UserButton = hal::gpio::gpioa::PA0>; -pub enum LedDirection { - Clockwise, - CounterClockwise, -} - -impl LedDirection { - fn flip(&self) -> LedDirection { - match self { - LedDirection::Clockwise => LedDirection::CounterClockwise, - LedDirection::CounterClockwise => LedDirection::Clockwise, - } - } -} - -pub struct LedCycle { - pub enabled: bool, - pub direction: LedDirection, - pub index: usize, - pub leds: [Led; 4] -} - -impl LedCycle { - const PERIOD: u32 = 8_000_000; - - fn from(leds: [Led; 4]) -> LedCycle { - LedCycle { - enabled: true, - direction: LedDirection::Clockwise, - index: 0, - leds - } - } - - fn disable(&mut self) { - self.enabled = false; - } - - fn enable(&mut self) { - self.enabled = true; - } - - fn reverse(&mut self) { - self.direction = self.direction.flip(); - } - - 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.index = match self.direction { - LedDirection::Clockwise => (self.index + 1) % num_leds, - LedDirection::CounterClockwise => (self.index + 3) % num_leds, - }; - } - - fn all_on(&mut self) { - for led in self.leds.iter_mut() { - led.set_high(); - } - } - - fn all_off(&mut self) { - for led in self.leds.iter_mut() { - led.set_low(); - } - } -} - #[app(device = hal::stm32)] const APP: () = { static mut button: UserButton = (); @@ -147,7 +79,9 @@ const APP: () = { resources.led_cycle.lock(|led_cycle| { if led_cycle.enabled { led_cycle.advance(); - schedule.switch_leds(scheduled + LedCycle::PERIOD.cycles()).unwrap(); + schedule + .switch_leds(scheduled + LedCycle::PERIOD.cycles()) + .unwrap(); } }); }