Skip to content

Commit 0be5557

Browse files
committed
Adds Direction iterator
Also splits the led examples into their own files
1 parent 5768163 commit 0be5557

File tree

5 files changed

+141
-38
lines changed

5 files changed

+141
-38
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ documentation = "https://docs.rs/crate/stm32f3-discovery"
99
categories = ["embedded", "hardware-support", "no-std"]
1010
keywords = ["discovery", "stm32f3", "bsp", "arm"]
1111
license = "MIT OR Apache-2.0"
12-
version = "0.7.0"
12+
version = "0.7.1"
1313
exclude = [
1414
".vscode/*",
1515
]

examples/roulette.rs renamed to examples/leds_array_indexing.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,9 @@ 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::{Direction, Leds};
12+
use stm32f3_discovery::leds::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-
3815
#[entry]
3916
fn main() -> ! {
4017
let device_periphs = pac::Peripherals::take().unwrap();
@@ -47,7 +24,7 @@ fn main() -> ! {
4724

4825
// initialize user leds
4926
let mut gpioe = device_periphs.GPIOE.split(&mut reset_and_clock_control.ahb);
50-
let mut leds = Leds::new(
27+
let leds = Leds::new(
5128
gpioe.pe8,
5229
gpioe.pe9,
5330
gpioe.pe10,
@@ -60,10 +37,6 @@ fn main() -> ! {
6037
&mut gpioe.otyper,
6138
);
6239

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.
6740
let mut compass = leds.into_array();
6841

6942
loop {
@@ -77,13 +50,5 @@ fn main() -> ! {
7750
compass[curr].off().ok();
7851
delay.delay_ms(ms_delay);
7952
}
80-
81-
// Alternative way to iterate through lights
82-
// for led in compass.iter_mut() {
83-
// led.on().ok();
84-
// delay.delay_ms(ms_delay);
85-
// led.off().ok();
86-
// delay.delay_ms(ms_delay);
87-
// }
8853
}
8954
}

examples/leds_array_iterator.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
extern crate panic_itm;
5+
6+
use cortex_m_rt::entry;
7+
8+
use stm32f3_discovery::stm32f3xx_hal::delay::Delay;
9+
use stm32f3_discovery::stm32f3xx_hal::prelude::*;
10+
use stm32f3_discovery::stm32f3xx_hal::pac;
11+
12+
use stm32f3_discovery::leds::Leds;
13+
use stm32f3_discovery::switch_hal::OutputSwitch;
14+
15+
#[entry]
16+
fn main() -> ! {
17+
let device_periphs = pac::Peripherals::take().unwrap();
18+
let mut reset_and_clock_control = device_periphs.RCC.constrain();
19+
20+
let core_periphs = cortex_m::Peripherals::take().unwrap();
21+
let mut flash = device_periphs.FLASH.constrain();
22+
let clocks = reset_and_clock_control.cfgr.freeze(&mut flash.acr);
23+
let mut delay = Delay::new(core_periphs.SYST, clocks);
24+
25+
// initialize user leds
26+
let mut gpioe = device_periphs.GPIOE.split(&mut reset_and_clock_control.ahb);
27+
let leds = Leds::new(
28+
gpioe.pe8,
29+
gpioe.pe9,
30+
gpioe.pe10,
31+
gpioe.pe11,
32+
gpioe.pe12,
33+
gpioe.pe13,
34+
gpioe.pe14,
35+
gpioe.pe15,
36+
&mut gpioe.moder,
37+
&mut gpioe.otyper,
38+
);
39+
40+
let mut compass = leds.into_array();
41+
42+
loop {
43+
let ms_delay = 50u16;
44+
// Alternative way to iterate through lights
45+
// do it backwards because it's fun and easy
46+
for led in compass.iter_mut().rev() {
47+
led.on().ok();
48+
delay.delay_ms(ms_delay);
49+
led.off().ok();
50+
delay.delay_ms(ms_delay);
51+
}
52+
}
53+
}

examples/leds_directions.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
extern crate panic_itm;
5+
6+
use cortex_m_rt::entry;
7+
8+
use stm32f3_discovery::stm32f3xx_hal::delay::Delay;
9+
use stm32f3_discovery::stm32f3xx_hal::prelude::*;
10+
use stm32f3_discovery::stm32f3xx_hal::pac;
11+
12+
use stm32f3xx_hal::gpio::gpioe;
13+
use stm32f3xx_hal::gpio::{Output, PushPull};
14+
15+
use stm32f3_discovery::leds::{Direction, Leds};
16+
use stm32f3_discovery::switch_hal::{Switch, ActiveHigh, OutputSwitch};
17+
18+
#[entry]
19+
fn main() -> ! {
20+
let device_periphs = pac::Peripherals::take().unwrap();
21+
let mut reset_and_clock_control = device_periphs.RCC.constrain();
22+
23+
let core_periphs = cortex_m::Peripherals::take().unwrap();
24+
let mut flash = device_periphs.FLASH.constrain();
25+
let clocks = reset_and_clock_control.cfgr.freeze(&mut flash.acr);
26+
let mut delay = Delay::new(core_periphs.SYST, clocks);
27+
28+
// initialize user leds
29+
let mut gpioe = device_periphs.GPIOE.split(&mut reset_and_clock_control.ahb);
30+
let mut leds = Leds::new(
31+
gpioe.pe8,
32+
gpioe.pe9,
33+
gpioe.pe10,
34+
gpioe.pe11,
35+
gpioe.pe12,
36+
gpioe.pe13,
37+
gpioe.pe14,
38+
gpioe.pe15,
39+
&mut gpioe.moder,
40+
&mut gpioe.otyper,
41+
);
42+
43+
loop {
44+
let fast_delay = 50u16;
45+
for direction in Direction::iter() {
46+
let led = &mut leds.for_direction(*direction);
47+
48+
led.on().ok();
49+
delay.delay_ms(fast_delay);
50+
led.off().ok();
51+
delay.delay_ms(fast_delay);
52+
}
53+
54+
slow_blink(leds.for_direction(Direction::North), &mut delay);
55+
slow_blink(leds.for_direction(Direction::South), &mut delay);
56+
slow_blink(leds.for_direction(Direction::East), &mut delay);
57+
slow_blink(leds.for_direction(Direction::West), &mut delay);
58+
}
59+
}
60+
61+
fn slow_blink(switch: &mut Switch<gpioe::PEx<Output<PushPull>>, ActiveHigh>, delay: &mut Delay) {
62+
let slow_delay = 250u16;
63+
switch.on().ok();
64+
delay.delay_ms(slow_delay);
65+
switch.off().ok();
66+
delay.delay_ms(slow_delay);
67+
}

src/leds.rs

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

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

7+
use core::slice::Iter;
8+
79
/// LED compass direction as noted on the board
810
#[derive(Clone, Copy, Eq, PartialEq)]
911
pub enum Direction
@@ -18,6 +20,22 @@ pub enum Direction
1820
NorthWest,
1921
}
2022

23+
impl Direction {
24+
pub fn iter() -> Iter<'static, Direction> {
25+
static DIRECTIONS: [Direction; 8] = [
26+
Direction::North,
27+
Direction::NorthEast,
28+
Direction::East,
29+
Direction::SouthEast,
30+
Direction::South,
31+
Direction::SouthWest,
32+
Direction::West,
33+
Direction::NorthWest
34+
];
35+
DIRECTIONS.iter()
36+
}
37+
}
38+
2139
pub struct Leds {
2240
/// North
2341
pub ld3: Switch<gpioe::PEx<Output<PushPull>>, ActiveHigh>,

0 commit comments

Comments
 (0)