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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use crate::{
error::{DataError, DeviceError},
util::{check_deserialization, is_set},
};
/// Represents the state of the sensor.
#[derive(Debug, PartialEq)]
pub enum SensorState {
/// Sensor is in idle state. Either after power-on, a reset or when calling
/// [`stop_measurement`](crate::asynch::Sen66::stop_measurement).
Idle,
/// Sensor is in measuring state. Entered by calling
/// [`start_measurement`](crate::asynch::Sen66::start_measurement).
Measuring,
}
#[cfg(feature = "defmt")]
impl defmt::Format for SensorState {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{}", self)
}
}
/// Sensor status register.
#[derive(Debug, PartialEq)]
pub struct DeviceStatusRegister(u32);
impl DeviceStatusRegister {
/// Returns whether a fan speed warning is present, as the speed is off more than 10% for
/// multiple measurement intervals. Disappears if the issue disappears.
pub fn fan_speed_warning(&self) -> bool {
is_set(self.0, 21)
}
/// Returns whether the PM sensor exhibits an error.
/// <div class="warning">Persists even if the error disappears. Requires reseting the devices
/// status, the device or performing a power cycle.</div>
pub fn pm_sensor_error(&self) -> bool {
is_set(self.0, 11)
}
/// Returns whether the CO2 sensor exhibits an error.
/// <div class="warning">Persists even if the error disappears. Requires reseting the devices
/// status, the device or performing a power cycle.</div>
pub fn co2_sensor_error(&self) -> bool {
is_set(self.0, 9)
}
/// Returns whether the Gas sensor exhibits an error.
/// <div class="warning">Persists even if the error disappears. Requires reseting the devices
/// status, the device or performing a power cycle.</div>
pub fn gas_sensor_error(&self) -> bool {
is_set(self.0, 7)
}
/// Returns whether the RH/T sensor exhibits an error.
/// <div class="warning">Persists even if the error disappears. Requires reseting the devices
/// status, the device or performing a power cycle.</div>
pub fn rht_sensor_error(&self) -> bool {
is_set(self.0, 6)
}
/// Returns whether the fan exhibits an error: It is turned on, but 0RPM are reported over
/// multiple measurement intervals.
/// <div class="warning">Persists even if the error disappears. Requires reseting the devices
/// status, the device or performing a power cycle.</div>
pub fn fan_error(&self) -> bool {
is_set(self.0, 4)
}
/// Checks whether any error has occured
///
/// # Errors
///
/// - [`DeviceError`](crate::error::DeviceError): Returned when any error is present, flags
/// indicate which errors are present.
pub fn has_error(&self) -> Result<(), DeviceError> {
let pm = self.pm_sensor_error();
let co2 = self.co2_sensor_error();
let gas = self.gas_sensor_error();
let rht = self.rht_sensor_error();
let fan = self.fan_error();
if [pm, co2, gas, rht, fan].iter().any(|&err| err) {
Err(DeviceError {
pm,
co2,
gas,
rht,
fan,
})
} else {
Ok(())
}
}
}
impl TryFrom<&[u8]> for DeviceStatusRegister {
type Error = DataError;
/// Parse the device status register from the received data.
///
/// # Errors
///
/// - [`CrcFailed`](crate::error::DataError::CrcFailed): If the received data CRC indicates
/// corruption.
/// - [`ReceivedBufferWrongSize`](crate::error::DataError::ReceivedBufferWrongSize): If the
/// received data buffer is not the expected size.
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
check_deserialization(data, 6)?;
Ok(DeviceStatusRegister(u32::from_be_bytes([
data[0], data[1], data[3], data[4],
])))
}
}
/// Indicates whether automatic self calibration (ASC) is enabled.
#[derive(Debug, PartialEq)]
pub enum AscState {
/// ASC is enabled.
Enabled,
/// ASC is disabled.
Disabled,
}
impl TryFrom<&[u8]> for AscState {
type Error = DataError;
/// Parse the ASC state from the received data.
///
/// # Errors
///
/// - [`CrcFailed`](crate::error::DataError::CrcFailed): If the received data CRC indicates
/// corruption.
/// - [`ReceivedBufferWrongSize`](crate::error::DataError::ReceivedBufferWrongSize): If the
/// received data buffer is not the expected size.
/// - [UnexpectedValueReceived](crate::error::DataError::UnexpectedValueReceived) if the
/// received value is not `0` or `1`.
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
check_deserialization(data, 3)?;
match data[1] {
0x00 => Ok(Self::Disabled),
0x01 => Ok(Self::Enabled),
val => Err(DataError::UnexpectedValueReceived {
parameter: "ASC State",
expected: "0 or 1",
actual: val as u16,
}),
}
}
}
impl From<AscState> for u16 {
fn from(value: AscState) -> Self {
match value {
AscState::Enabled => 0x0001,
AscState::Disabled => 0x0000,
}
}
}
/// Stores the VOC algorithm state, which can be used to skip the learning phase after a power
/// cycle.
#[derive(Debug, PartialEq)]
pub struct VocAlgorithmState([u8; 8]);
impl TryFrom<&[u8]> for VocAlgorithmState {
type Error = DataError;
/// Parse the VOC algorithm state from the received data.
///
/// # Errors
///
/// - [`CrcFailed`](crate::error::DataError::CrcFailed): If the received data CRC indicates
/// corruption.
/// - [`ReceivedBufferWrongSize`](crate::error::DataError::ReceivedBufferWrongSize): If the
/// received data buffer is not the expected size.
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
check_deserialization(data, 12)?;
Ok(VocAlgorithmState([
data[0], data[1], data[3], data[4], data[6], data[7], data[9], data[10],
]))
}
}
impl From<VocAlgorithmState> for [u16; 4] {
fn from(value: VocAlgorithmState) -> Self {
[
u16::from_be_bytes([value.0[0], value.0[1]]),
u16::from_be_bytes([value.0[2], value.0[3]]),
u16::from_be_bytes([value.0[4], value.0[5]]),
u16::from_be_bytes([value.0[6], value.0[7]]),
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_flags_set_nothing_reported() {
let state = DeviceStatusRegister(0b0000_0000_0000_0000_0000_0000_0000_0000);
assert!(!state.fan_speed_warning());
assert!(state.has_error().is_ok());
}
#[test]
fn set_fan_speed_warning_reported() {
let state = DeviceStatusRegister(0b0000_0000_0010_0000_0000_0000_0000_0000);
assert!(state.fan_speed_warning());
}
#[test]
fn set_fan_speed_error_reported() {
let state = DeviceStatusRegister(0b0000_0000_0000_0000_0000_0000_0001_0000);
assert!(state.fan_error());
}
#[test]
fn set_rht_error_reported() {
let state = DeviceStatusRegister(0b0000_0000_0000_0000_0000_0000_0100_0000);
assert!(state.rht_sensor_error());
}
#[test]
fn set_gas_error_reported() {
let state = DeviceStatusRegister(0b0000_0000_0000_0000_0000_0000_1000_0000);
assert!(state.gas_sensor_error());
}
#[test]
fn set_co2_error_reported() {
let state = DeviceStatusRegister(0b0000_0000_0000_0000_0000_0010_0000_0000);
assert!(state.co2_sensor_error());
}
#[test]
fn set_pm_error_reported() {
let state = DeviceStatusRegister(0b0000_0000_0000_0000_0000_1000_0000_0000);
assert!(state.pm_sensor_error());
}
#[test]
fn set_warning_flag_does_not_emit_error() {
let state = DeviceStatusRegister(0b0000_0000_0010_0000_0000_0000_0000_0000);
assert!(state.has_error().is_ok());
}
#[test]
fn set_error_flag_does_emit_device_error() {
let state = DeviceStatusRegister(0b0000_0000_0000_0000_0000_1000_0000_0000);
assert_eq!(
state.has_error().unwrap_err(),
DeviceError {
pm: true,
co2: false,
gas: false,
rht: false,
fan: false
}
);
}
#[test]
fn deserialize_device_status_register_with_all_flags_set_yields_u32_with_flag_bits_one() {
let data = [0x00, 0x20, 0x07, 0x0E, 0xD0, 0xE8];
assert_eq!(
DeviceStatusRegister::try_from(&data[..]).unwrap(),
DeviceStatusRegister(0b0000_0000_0010_0000_0000_1110_1101_0000)
);
}
#[test]
fn deserialize_asc_status_enabled_yields_enabled() {
let data = [0x00, 0x01, 0xB0];
assert_eq!(AscState::try_from(&data[..]).unwrap(), AscState::Enabled);
}
#[test]
fn deserialize_asc_status_disabled_yields_enabled() {
let data = [0x00, 0x00, 0x81];
assert_eq!(AscState::try_from(&data[..]).unwrap(), AscState::Disabled);
}
#[test]
fn deserialize_asc_status_unknown_emit_error() {
let data = [0x00, 0x03, 0xd2];
assert!(AscState::try_from(&data[..]).is_err());
}
#[test]
fn serialize_asc_status_enabled_yields_one() {
assert_eq!(u16::from(AscState::Enabled), 0x0001);
}
#[test]
fn serialize_asc_status_disabled_yields_zero() {
assert_eq!(u16::from(AscState::Disabled), 0x0000);
}
#[test]
fn deserialize_voc_algorithm_state_yields_same_state() {
let data = [
0x01, 0x02, 0x17, 0x03, 0x04, 0x68, 0x05, 0x06, 0x50, 0x07, 0x08, 0x96,
];
assert_eq!(
VocAlgorithmState::try_from(&data[..]).unwrap(),
VocAlgorithmState([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])
);
}
#[test]
fn serialize_voc_algorithm_state_yields_same_state() {
assert_eq!(
<[u16; 4]>::from(VocAlgorithmState([
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
])),
[0x0102, 0x0304, 0x0506, 0x0708]
);
}
}