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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#[derive(Debug)]
pub enum Error<CommE, PinE> {
Comm(CommE),
Pin(PinE),
InvalidInputData,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SensorPowerMode {
pub accel: AccelerometerPowerMode,
pub gyro: GyroscopePowerMode,
pub magnet: MagnetometerPowerMode,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AccelerometerPowerMode {
Normal,
Suspend,
LowPower,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroscopePowerMode {
Normal,
Suspend,
FastStartUp,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MagnetometerPowerMode {
Normal,
Suspend,
LowPower,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Status {
pub accel_data_ready: bool,
pub gyro_data_ready: bool,
pub magnet_data_ready: bool,
pub nvm_ready: bool,
pub foc_ready: bool,
pub magnet_manual_op: bool,
pub gyro_self_test_ok: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SensorSelector {
pub(crate) accel: bool,
pub(crate) gyro: bool,
pub(crate) magnet: bool,
pub(crate) time: bool,
}
impl SensorSelector {
pub fn new() -> Self {
SensorSelector {
accel: false,
gyro: false,
magnet: false,
time: false,
}
}
pub fn accel(mut self) -> Self {
self.accel = true;
self
}
pub fn gyro(mut self) -> Self {
self.gyro = true;
self
}
pub fn magnet(mut self) -> Self {
self.magnet = true;
self
}
pub fn time(mut self) -> Self {
self.time = true;
self
}
pub fn all() -> Self {
SensorSelector {
accel: true,
gyro: true,
magnet: true,
time: true,
}
}
}
impl Default for SensorSelector {
fn default() -> Self {
SensorSelector::all()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Sensor3DData {
pub x: i16,
pub y: i16,
pub z: i16,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MagnetometerData {
pub axes: Sensor3DData,
pub hall_resistence: u16,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Data {
pub accel: Option<Sensor3DData>,
pub gyro: Option<Sensor3DData>,
pub magnet: Option<MagnetometerData>,
pub time: Option<u32>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn selector_default_all() {
assert_eq!(SensorSelector::all(), SensorSelector::default());
}
#[test]
fn selector_all() {
assert_eq!(
SensorSelector::all(),
SensorSelector {
accel: true,
gyro: true,
magnet: true,
time: true
}
);
}
macro_rules! selector_test {
($name:ident, $method:ident) => {
#[test]
fn $name() {
let mut expected = SensorSelector {
accel: false,
gyro: false,
magnet: false,
time: false,
};
expected.$method = true;
assert_eq!(SensorSelector::new().$method(), expected);
}
};
}
selector_test!(selector_accel, accel);
selector_test!(selector_gyro, gyro);
selector_test!(selector_magnet, magnet);
selector_test!(selector_time, time);
#[test]
fn selector_combine_all() {
assert_eq!(
SensorSelector::all(),
SensorSelector::new().accel().gyro().magnet().time()
);
}
}