1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
use stm32f1xx_hal::{
gpio::{Cr, CRL, Floating, Input, Output, PushPull, gpiob::PB2},
};
enum State {
Low,
High,
}
type LEDPin<MODE> = PB2<MODE>;
pub struct LED {
pin: LEDPin<Output<PushPull>>,
state: State,
}
impl LED {
pub fn new(pin: LEDPin<Input<Floating>>, cr: &mut Cr<CRL, 'B'>) -> Self {
let mut p = pin.into_push_pull_output(cr);
p.set_low();
Self { pin: p, state: State::Low }
}
pub fn is_on(&self) -> bool {
match self.state {
State::High => true,
State::Low => false,
}
}
pub fn on(&mut self) {
self.state = State::High;
self.pin.set_high();
}
pub fn off(&mut self) {
self.state = State::Low;
self.pin.set_low();
}
pub fn toggle(&mut self) {
if self.is_on() {
self.off();
} else {
self.on();
}
}
}
|