Skip to content

Commit 5768163

Browse files
Merge pull request rubberduck203#39 from sirhcel/led-directions
Add access to LEDs via compass directions
2 parents 6445227 + 5cc36c6 commit 5768163

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

examples/roulette.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,32 @@ use stm32f3_discovery::stm32f3xx_hal::delay::Delay;
99
use stm32f3_discovery::stm32f3xx_hal::prelude::*;
1010
use stm32f3_discovery::stm32f3xx_hal::pac;
1111

12-
use stm32f3_discovery::leds::Leds;
12+
use stm32f3_discovery::leds::{Direction, Leds};
1313
use stm32f3_discovery::switch_hal::OutputSwitch;
1414

15+
fn one_round_by_direction(leds: &mut Leds, delay: &mut Delay) {
16+
let slow_delay = 500u16;
17+
let directions = [
18+
Direction::North,
19+
Direction::NorthEast,
20+
Direction::East,
21+
Direction::SouthEast,
22+
Direction::South,
23+
Direction::SouthWest,
24+
Direction::West,
25+
Direction::NorthWest
26+
];
27+
28+
for direction in &directions {
29+
let led = &mut leds.for_direction(*direction);
30+
31+
led.on().ok();
32+
delay.delay_ms(slow_delay);
33+
led.off().ok();
34+
delay.delay_ms(slow_delay);
35+
}
36+
}
37+
1538
#[entry]
1639
fn main() -> ! {
1740
let device_periphs = pac::Peripherals::take().unwrap();
@@ -24,7 +47,7 @@ fn main() -> ! {
2447

2548
// initialize user leds
2649
let mut gpioe = device_periphs.GPIOE.split(&mut reset_and_clock_control.ahb);
27-
let leds = Leds::new(
50+
let mut leds = Leds::new(
2851
gpioe.pe8,
2952
gpioe.pe9,
3053
gpioe.pe10,
@@ -37,6 +60,10 @@ fn main() -> ! {
3760
&mut gpioe.otyper,
3861
);
3962

63+
// Light them one round by direction.
64+
one_round_by_direction(&mut leds, &mut delay);
65+
66+
// Finally let go by the compass array.
4067
let mut compass = leds.into_array();
4168

4269
loop {

src/leds.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ use stm32f3xx_hal::gpio::{Output, PushPull};
44

55
use switch_hal::{ActiveHigh, IntoSwitch, OutputSwitch, Switch};
66

7+
/// LED compass direction as noted on the board
8+
#[derive(Clone, Copy, Eq, PartialEq)]
9+
pub enum Direction
10+
{
11+
North,
12+
NorthEast,
13+
East,
14+
SouthEast,
15+
South,
16+
SouthWest,
17+
West,
18+
NorthWest,
19+
}
20+
721
pub struct Leds {
822
/// North
923
pub ld3: Switch<gpioe::PEx<Output<PushPull>>, ActiveHigh>,
@@ -85,6 +99,20 @@ impl Leds {
8599
leds
86100
}
87101

102+
/// Mutably borrow a LED by the given direction (as noted on the board)
103+
pub fn for_direction(&mut self, direction: Direction) -> &mut Switch<gpioe::PEx<Output<PushPull>>, ActiveHigh> {
104+
match direction {
105+
Direction::North => &mut self.ld3,
106+
Direction::NorthEast => &mut self.ld5,
107+
Direction::East => &mut self.ld7,
108+
Direction::SouthEast => &mut self.ld9,
109+
Direction::South => &mut self.ld10,
110+
Direction::SouthWest => &mut self.ld8,
111+
Direction::West => &mut self.ld6,
112+
Direction::NorthWest => &mut self.ld4,
113+
}
114+
}
115+
88116
/// Consumes the `Leds` struct and returns an array
89117
/// where index 0 is N and each incrementing index
90118
/// rotates clockwise around the compass

0 commit comments

Comments
 (0)