f3/
led.rs

1//! On-board user LEDs
2
3use core::ops;
4
5use hal::prelude::*;
6
7use hal::gpio::gpioe::{self, PE10, PE11, PE12, PE13, PE14, PE15, PE8, PE9, PEx};
8use hal::gpio::{Output, PushPull};
9
10///  North LED
11pub type LD3 = PE9<Output<PushPull>>;
12
13/// Northeast LED
14pub type LD5 = PE10<Output<PushPull>>;
15
16/// East LED
17pub type LD7 = PE11<Output<PushPull>>;
18
19/// Southeast LED
20pub type LD9 = PE12<Output<PushPull>>;
21
22/// South LED
23pub type LD10 = PE13<Output<PushPull>>;
24
25/// Southwest LED
26pub type LD8 = PE14<Output<PushPull>>;
27
28/// West LED
29pub type LD6 = PE15<Output<PushPull>>;
30
31/// Northwest LED
32pub type LD4 = PE8<Output<PushPull>>;
33
34/// Cardinal directions. Each one matches one of the user LEDs.
35pub enum Direction {
36    /// North / LD3
37    North,
38    /// Northeast / LD5
39    Northeast,
40    /// East / LD7
41    East,
42    /// Southeast / LD9
43    Southeast,
44    /// South / LD10
45    South,
46    /// Southwest / LD8
47    Southwest,
48    /// West / LD6
49    West,
50    /// Northwest / LD4
51    Northwest,
52}
53
54/// Array of all the user LEDs on the board
55pub struct Leds {
56    leds: [Led; 8],
57}
58
59impl Leds {
60    /// Initializes all the user LEDs
61    pub fn new(mut gpioe: gpioe::Parts) -> Self {
62        let n = gpioe
63            .pe9
64            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
65        let ne = gpioe
66            .pe10
67            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
68        let e = gpioe
69            .pe11
70            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
71        let se = gpioe
72            .pe12
73            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
74        let s = gpioe
75            .pe13
76            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
77        let sw = gpioe
78            .pe14
79            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
80        let w = gpioe
81            .pe15
82            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
83        let nw = gpioe
84            .pe8
85            .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
86
87        Leds {
88            leds: [
89                n.into(),
90                ne.into(),
91                e.into(),
92                se.into(),
93                s.into(),
94                sw.into(),
95                w.into(),
96                nw.into(),
97            ],
98        }
99    }
100}
101
102impl ops::Deref for Leds {
103    type Target = [Led];
104
105    fn deref(&self) -> &[Led] {
106        &self.leds
107    }
108}
109
110impl ops::DerefMut for Leds {
111    fn deref_mut(&mut self) -> &mut [Led] {
112        &mut self.leds
113    }
114}
115
116impl ops::Index<usize> for Leds {
117    type Output = Led;
118
119    fn index(&self, i: usize) -> &Led {
120        &self.leds[i]
121    }
122}
123
124impl ops::Index<Direction> for Leds {
125    type Output = Led;
126
127    fn index(&self, d: Direction) -> &Led {
128        &self.leds[d as usize]
129    }
130}
131
132impl ops::IndexMut<usize> for Leds {
133    fn index_mut(&mut self, i: usize) -> &mut Led {
134        &mut self.leds[i]
135    }
136}
137
138impl ops::IndexMut<Direction> for Leds {
139    fn index_mut(&mut self, d: Direction) -> &mut Led {
140        &mut self.leds[d as usize]
141    }
142}
143
144/// One of the on-board user LEDs
145pub struct Led {
146    pex: PEx<Output<PushPull>>,
147}
148
149macro_rules! ctor {
150    ($($ldx:ident),+) => {
151        $(
152            impl Into<Led> for $ldx {
153                fn into(self) -> Led {
154                    Led {
155                        pex: self.downgrade(),
156                    }
157                }
158            }
159        )+
160    }
161}
162
163ctor!(LD3, LD4, LD5, LD6, LD7, LD8, LD9, LD10);
164
165impl Led {
166    /// Turns the LED off
167    pub fn off(&mut self) {
168        self.pex.set_low()
169    }
170
171    /// Turns the LED on
172    pub fn on(&mut self) {
173        self.pex.set_high()
174    }
175}