Refactor the LedCycle struct, impl and related enums
This commit is contained in:
parent
27fe066219
commit
6ab8d98180
49
src/led.rs
49
src/led.rs
|
@ -3,45 +3,56 @@ use hal::prelude::*;
|
|||
|
||||
pub type Led = hal::gpio::gpiod::PD<Output<PushPull>>;
|
||||
|
||||
pub enum LedDirection {
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Direction {
|
||||
Clockwise,
|
||||
CounterClockwise,
|
||||
}
|
||||
|
||||
impl LedDirection {
|
||||
fn flip(&self) -> LedDirection {
|
||||
impl Direction {
|
||||
fn flip(&self) -> Direction {
|
||||
match self {
|
||||
LedDirection::Clockwise => LedDirection::CounterClockwise,
|
||||
LedDirection::CounterClockwise => LedDirection::Clockwise,
|
||||
Direction::Clockwise => Direction::CounterClockwise,
|
||||
Direction::CounterClockwise => Direction::Clockwise,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LedCycle {
|
||||
pub enabled: bool,
|
||||
pub direction: LedDirection,
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Mode {
|
||||
Off,
|
||||
Cycle,
|
||||
}
|
||||
|
||||
pub struct LedRing {
|
||||
pub direction: Direction,
|
||||
pub mode: Mode,
|
||||
pub index: usize,
|
||||
pub leds: [crate::Led; 4],
|
||||
}
|
||||
|
||||
impl LedCycle {
|
||||
impl LedRing {
|
||||
pub const PERIOD: u32 = 8_000_000;
|
||||
|
||||
pub fn from(leds: [crate::Led; 4]) -> LedCycle {
|
||||
LedCycle {
|
||||
enabled: true,
|
||||
direction: LedDirection::Clockwise,
|
||||
pub fn from(leds: [crate::Led; 4]) -> LedRing {
|
||||
LedRing {
|
||||
direction: Direction::Clockwise,
|
||||
mode: Mode::Cycle,
|
||||
index: 0,
|
||||
leds,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disable(&mut self) {
|
||||
self.enabled = false;
|
||||
pub fn enable_cycle(&mut self) {
|
||||
self.mode = Mode::Cycle;
|
||||
}
|
||||
|
||||
pub fn enable(&mut self) {
|
||||
self.enabled = true;
|
||||
pub fn disable(&mut self) {
|
||||
self.mode = Mode::Off;
|
||||
}
|
||||
|
||||
pub fn is_mode_cycle(&self) -> bool {
|
||||
self.mode == Mode::Cycle
|
||||
}
|
||||
|
||||
pub fn reverse(&mut self) {
|
||||
|
@ -55,8 +66,8 @@ impl LedCycle {
|
|||
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,
|
||||
Direction::Clockwise => (self.index + 1) % num_leds,
|
||||
Direction::CounterClockwise => (self.index + 3) % num_leds,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
40
src/main.rs
40
src/main.rs
|
@ -7,7 +7,7 @@ extern crate panic_semihosting;
|
|||
|
||||
mod led;
|
||||
|
||||
use crate::led::{Led, LedCycle};
|
||||
use crate::led::{Led, LedRing};
|
||||
use core::fmt::Write;
|
||||
use cortex_m_semihosting::hprintln;
|
||||
use hal::block;
|
||||
|
@ -27,14 +27,14 @@ type UserButton = hal::gpio::gpioa::PA0<Input<Floating>>;
|
|||
const APP: () = {
|
||||
static mut button: UserButton = ();
|
||||
static mut buffer: Vec<u8, U8> = ();
|
||||
static mut led_cycle: LedCycle = ();
|
||||
static mut led_ring: LedRing = ();
|
||||
static mut exti: EXTI = ();
|
||||
static mut serial_rx: SerialRx = ();
|
||||
static mut serial_tx: SerialTx = ();
|
||||
|
||||
#[init(spawn = [switch_leds])]
|
||||
fn init() -> init::LateResources {
|
||||
// Set up the LED cycle and spawn the LEDs switch task.
|
||||
// Set up the LED ring and spawn the LEDs switch task.
|
||||
let gpiod = device.GPIOD.split();
|
||||
let leds = [
|
||||
gpiod.pd12.into_push_pull_output().downgrade(),
|
||||
|
@ -42,7 +42,7 @@ const APP: () = {
|
|||
gpiod.pd14.into_push_pull_output().downgrade(),
|
||||
gpiod.pd15.into_push_pull_output().downgrade(),
|
||||
];
|
||||
let led_cycle = LedCycle::from(leds);
|
||||
let led_ring = LedRing::from(leds);
|
||||
spawn.switch_leds().unwrap();
|
||||
|
||||
// Set up the EXTI0 interrupt for the user button.
|
||||
|
@ -69,19 +69,19 @@ const APP: () = {
|
|||
button,
|
||||
buffer,
|
||||
exti,
|
||||
led_cycle,
|
||||
led_ring,
|
||||
serial_tx,
|
||||
serial_rx,
|
||||
}
|
||||
}
|
||||
|
||||
#[task(schedule = [switch_leds], resources = [led_cycle])]
|
||||
#[task(schedule = [switch_leds], resources = [led_ring])]
|
||||
fn switch_leds() {
|
||||
resources.led_cycle.lock(|led_cycle| {
|
||||
if led_cycle.enabled {
|
||||
led_cycle.advance();
|
||||
resources.led_ring.lock(|led_ring| {
|
||||
if led_ring.is_mode_cycle() {
|
||||
led_ring.advance();
|
||||
schedule
|
||||
.switch_leds(scheduled + LedCycle::PERIOD.cycles())
|
||||
.switch_leds(scheduled + LedRing::PERIOD.cycles())
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
|
@ -89,7 +89,7 @@ const APP: () = {
|
|||
|
||||
#[interrupt(binds = EXTI0, resources = [button, exti, led_cycle, serial_tx])]
|
||||
fn button_pressed() {
|
||||
resources.led_cycle.lock(|led_cycle| led_cycle.reverse());
|
||||
resources.led_ring.lock(|led_ring| led_ring.reverse());
|
||||
|
||||
// Write the fact that the button has been pressed to the serial port.
|
||||
resources
|
||||
|
@ -102,7 +102,7 @@ const APP: () = {
|
|||
#[interrupt(
|
||||
binds = USART2,
|
||||
priority = 2,
|
||||
resources = [buffer, led_cycle, serial_rx, serial_tx],
|
||||
resources = [buffer, led_ring, serial_rx, serial_tx],
|
||||
spawn = [switch_leds]
|
||||
)]
|
||||
fn handle_serial() {
|
||||
|
@ -119,22 +119,22 @@ const APP: () = {
|
|||
block!(resources.serial_tx.write(b'\n')).unwrap();
|
||||
match &buffer[..] {
|
||||
b"flip" => {
|
||||
resources.led_cycle.reverse();
|
||||
resources.led_ring.reverse();
|
||||
}
|
||||
b"stop" => {
|
||||
resources.led_cycle.disable();
|
||||
resources.led_ring.disable();
|
||||
}
|
||||
b"start" => {
|
||||
resources.led_cycle.enable();
|
||||
b"cycle" => {
|
||||
resources.led_ring.enable_cycle();
|
||||
spawn.switch_leds().unwrap();
|
||||
}
|
||||
b"off" => {
|
||||
resources.led_cycle.disable();
|
||||
resources.led_cycle.all_off();
|
||||
resources.led_ring.disable();
|
||||
resources.led_ring.all_off();
|
||||
}
|
||||
b"on" => {
|
||||
resources.led_cycle.disable();
|
||||
resources.led_cycle.all_on();
|
||||
resources.led_ring.disable();
|
||||
resources.led_ring.all_on();
|
||||
}
|
||||
_ => {
|
||||
writeln!(resources.serial_tx, "?\r").unwrap();
|
||||
|
|
Loading…
Reference in New Issue