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
use eps_api::{EpsError, EpsResult};
#[macro_export]
macro_rules! make_telemetry {
(
$(
$(#[$meta:meta])+
$type: ident => {$data: expr, $parser: expr},
)+
) => {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Type {
$(
$(#[$meta])+
$type,
)+
}
pub fn parse(data: &[u8], telem_type: Type) -> EpsResult<f64> {
let adc_data = get_adc_result(data)?;
Ok(match telem_type {
$(Type::$type => $parser(adc_data),)+
})
}
pub fn command(telem_type: Type) -> (Command, usize) {
(
Command {
cmd: TELEM_CMD,
data: match telem_type {
$(Type::$type => $data,)+
}
},
2
)
}
}
}
pub fn get_adc_result(data: &[u8]) -> EpsResult<f64> {
if data.len() < 2 {
Err(EpsError::parsing_failure("ADC Result"))
} else {
let be_val = u16::from(data[0]) | u16::from(data[1]) << 8;
let native_val = u16::from_be(be_val);
Ok(f64::from(native_val))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_adcs_result() {
let raw = vec![0x01, 0x23];
let adc = get_adc_result(&raw).unwrap();
assert_eq!(adc, 291.0);
}
#[test]
fn test_make_telemetry() {
use rust_i2c::Command;
const TELEM_CMD: u8 = 0x00;
make_telemetry!(
TestVal1 => {vec![0xE1], |d| (10.0 * d) - 10.0},
);
assert_eq!(
command(Type::TestVal1),
(
Command {
cmd: TELEM_CMD,
data: vec![0xE1],
},
2
)
);
assert_eq!(parse(&vec![0x01, 0x23], Type::TestVal1), Ok(2900.0));
}
}