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
#[allow(unused_imports)] use num_traits::float::FloatCore;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Measurement {
pub co2eq_ppm: u16,
pub tvoc_ppb: u16,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RawSignals {
pub h2: u16,
pub ethanol: u16,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Baseline {
pub co2eq: u16,
pub tvoc: u16,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Humidity {
integer: u8, fractional: u8, }
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum HumidityError {
ZeroValue,
OutOfRange,
}
impl Humidity {
pub fn new(integer: u8, fractional: u8) -> Result<Self, HumidityError> {
if integer == 0 && fractional == 0 {
return Err(HumidityError::ZeroValue);
}
Ok(Humidity { integer, fractional })
}
pub fn from_f32(val: f32) -> Result<Self, HumidityError> {
if val.is_nan() {
return Err(HumidityError::OutOfRange);
}
let integer = if val >= 256.0 || val < 0.0 {
return Err(HumidityError::OutOfRange);
} else {
val.trunc() as u8
};
let fractional_f32 = val.fract() * 256.0f32;
let fractional = if fractional_f32 > 255.0 {
255
} else if fractional_f32 < 0.0 {
0
} else {
fractional_f32 as u8
};
Humidity::new(integer, fractional)
}
pub fn as_bytes(&self) -> [u8; 2] {
[self.integer, self.fractional]
}
}
impl Into<f32> for Humidity {
fn into(self) -> f32 {
f32::from(self.integer) + (f32::from(self.fractional) / 256.0)
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ProductType {
Sgp30,
Unknown(u8),
}
impl ProductType {
pub fn parse(val: u8) -> Self {
match val {
0 => ProductType::Sgp30,
_ => ProductType::Unknown(val),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct FeatureSet {
pub product_type: ProductType,
pub product_version: u8,
}
impl FeatureSet {
pub fn parse(msb: u8, lsb: u8) -> Self {
FeatureSet {
product_type: ProductType::parse(msb >> 4),
product_version: lsb,
}
}
}
#[cfg(test)]
mod tests {
use std::f32;
use super::*;
#[test]
fn humidity_as_bytes() {
assert_eq!(Humidity::new(0x00, 0x01).unwrap().as_bytes(), [0x00, 0x01]);
assert_eq!(Humidity::new(0xFF, 0xFF).unwrap().as_bytes(), [0xFF, 0xFF]);
assert_eq!(Humidity::new(0x10, 0x80).unwrap().as_bytes(), [0x10, 0x80]);
}
#[test]
fn humidity_from_f32_ok() {
assert_eq!(Humidity::from_f32(0.00390625f32), Ok(Humidity::new(0x00, 0x01).unwrap()));
assert_eq!(Humidity::from_f32(255.99609375f32), Ok(Humidity::new(0xFF, 0xFF).unwrap()));
assert_eq!(Humidity::from_f32(16.5f32), Ok(Humidity::new(0x10, 0x80).unwrap()));
assert_eq!(Humidity::from_f32(16.999999f32), Ok(Humidity::new(0x10, 0xFF).unwrap()));
}
#[test]
fn humidity_from_f32_err() {
assert_eq!(Humidity::from_f32(-3.0f32), Err(HumidityError::OutOfRange));
assert_eq!(Humidity::from_f32(0.0f32), Err(HumidityError::ZeroValue));
assert_eq!(Humidity::from_f32(-0.0f32), Err(HumidityError::ZeroValue));
assert_eq!(Humidity::from_f32(f32::NAN), Err(HumidityError::OutOfRange));
}
#[test]
fn humidity_into_f32() {
let float: f32 = Humidity::new(0x00, 0x01).unwrap().into();
assert_eq!(float, 0.00390625f32);
let float: f32 = Humidity::new(0xFF, 0xFF).unwrap().into();
assert_eq!(float, 255.99609375);
let float: f32 = Humidity::new(0x10, 0x80).unwrap().into();
assert_eq!(float, 16.5);
}
}