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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::digital::v2::PinState::{High, Low};
use embedded_hal::digital::v2::{InputPin, OutputPin};
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Normal;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Wakeup;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Monitor;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Program;
pub trait Mode {
fn id(&self) -> u8;
fn set_pins<Aux, M0, M1, D>(aux: &mut Aux, m0: &mut M0, m1: &mut M1, delay: &mut D)
where
Aux: InputPin,
M0: OutputPin,
M1: OutputPin,
D: DelayMs<u32>;
}
macro_rules! impl_mode {
($($type: ty)*, $id: literal, $m0_state: path, $m1_state: path) => {
$(
impl Mode for $type {
fn id(&self) -> u8 {
$id
}
fn set_pins<Aux, M0, M1, D>(aux: &mut Aux, m0: &mut M0, m1: &mut M1, delay: &mut D)
where
Aux: InputPin,
M0: OutputPin,
M1: OutputPin,
D: DelayMs<u32> {
delay.delay_ms(40);
let _m0 = m0.set_state($m0_state);
let _m1 = m1.set_state($m1_state);
delay.delay_ms(40);
loop {
match aux.is_low() {
Ok(true) => continue,
Ok(false) => break,
Err(_e) => panic!("failed to wait for aux pin"),
}
}
}
}
)*
};
}
impl_mode!(Normal, 0, Low, Low);
impl_mode!(Wakeup, 1, High, Low);
impl_mode!(Monitor, 2, Low, High);
impl_mode!(Program, 3, High, High);
#[cfg(test)]
mod test {
use super::*;
use embedded_hal_mock::delay::MockNoop;
use embedded_hal_mock::pin::Mock as Pin;
use embedded_hal_mock::pin::{
State::{High, Low},
Transaction,
};
#[test]
fn id() {
let mode = Normal;
assert_eq!(mode.id(), 0);
let mode = Wakeup;
assert_eq!(mode.id(), 1);
let mode = Monitor;
assert_eq!(mode.id(), 2);
let mode = Program;
assert_eq!(mode.id(), 3);
}
#[test]
fn pins_normal() {
let mut m0 = Pin::new(&vec![Transaction::set(Low)]);
let mut m1 = Pin::new(&vec![Transaction::set(Low)]);
let mut aux = Pin::new(&vec![
Transaction::get(Low),
Transaction::get(Low),
Transaction::get(High),
]);
Normal::set_pins(&mut aux, &mut m0, &mut m1, &mut MockNoop);
m0.done();
m1.done();
aux.done();
}
#[test]
fn pins_wakeup() {
let mut m0 = Pin::new(&vec![Transaction::set(High)]);
let mut m1 = Pin::new(&vec![Transaction::set(Low)]);
let mut aux = Pin::new(&vec![
Transaction::get(Low),
Transaction::get(Low),
Transaction::get(High),
]);
Wakeup::set_pins(&mut aux, &mut m0, &mut m1, &mut MockNoop);
m0.done();
m1.done();
aux.done();
}
#[test]
fn pins_powerdown() {
let mut m0 = Pin::new(&vec![Transaction::set(Low)]);
let mut m1 = Pin::new(&vec![Transaction::set(High)]);
let mut aux = Pin::new(&vec![
Transaction::get(Low),
Transaction::get(Low),
Transaction::get(High),
]);
Monitor::set_pins(&mut aux, &mut m0, &mut m1, &mut MockNoop);
m0.done();
m1.done();
aux.done();
}
#[test]
fn pins_program() {
let mut m0 = Pin::new(&vec![Transaction::set(High)]);
let mut m1 = Pin::new(&vec![Transaction::set(High)]);
let mut aux = Pin::new(&vec![
Transaction::get(Low),
Transaction::get(Low),
Transaction::get(High),
]);
Program::set_pins(&mut aux, &mut m0, &mut m1, &mut MockNoop);
m0.done();
m1.done();
aux.done();
}
}