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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use core::time::Duration;
use log::warn;
use crate::{
constants::{GAS_ARRAY_1, GAS_ARRAY_2, MAX_HEATER_TEMPERATURE, MAX_HEATER_WAIT_DURATION_MS},
data::CalibrationData,
};
#[repr(u8)]
pub enum DeviceAddress {
Primary = 0x76,
Secondary = 0x77,
}
impl From<DeviceAddress> for u8 {
fn from(value: DeviceAddress) -> Self {
match value {
DeviceAddress::Primary => 0x76,
DeviceAddress::Secondary => 0x77,
}
}
}
impl Default for DeviceAddress {
fn default() -> Self {
Self::Primary
}
}
pub enum Variant {
GasLow = 0,
GasHigh = 1,
}
impl From<u8> for Variant {
fn from(value: u8) -> Self {
match value {
0 => Variant::GasLow,
1 => Variant::GasHigh,
x => panic!(
"Got unimplemented device variant from sensor: {x}. Possible values are: [0, 1]."
),
}
}
}
impl Variant {
pub fn calc_gas_resistance(
&self,
adc_gas: u16,
range_switching_error: i8,
gas_range: usize,
) -> f32 {
match self {
Self::GasLow => {
let adc_gas = adc_gas as f32;
let gas_range_f = (1 << gas_range) as f32;
let var1 = 1340. + (5. * range_switching_error as f32);
let var2 = var1 * (1. + GAS_ARRAY_1[gas_range] / 100.);
let var3 = 1. + (GAS_ARRAY_2[gas_range] / 100.);
1. / (var3 * (0.000000125) * gas_range_f * (((adc_gas - 512.) / var2) + 1.))
}
Self::GasHigh => {
let var1 = 262144_u32 >> gas_range;
let mut var2 = adc_gas as i32 - 512_i32;
var2 *= 3;
var2 += 4096;
1000000. * var1 as f32 / var2 as f32
}
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum SensorMode {
Sleep,
Forced,
}
impl From<SensorMode> for u8 {
fn from(value: SensorMode) -> Self {
match value {
SensorMode::Sleep => 0,
SensorMode::Forced => 1,
}
}
}
impl From<u8> for SensorMode {
fn from(val: u8) -> Self {
match val {
0 => SensorMode::Sleep,
1 => SensorMode::Forced,
invalid => panic!("Failed to read sensor mode. Received {invalid:b} possible values are 0b00(sleep) or 0b01(forced)"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GasConfig {
heater_duration: Duration,
heater_target_temperature: u16,
}
impl Default for GasConfig {
fn default() -> Self {
Self {
heater_duration: Duration::from_millis(150),
heater_target_temperature: 300,
}
}
}
impl GasConfig {
pub fn calc_gas_wait(&self) -> u8 {
let mut duration = self.heater_duration.as_millis() as u16;
let mut factor: u8 = 0;
if duration >= MAX_HEATER_WAIT_DURATION_MS {
warn!("Specified heater duration longer than {MAX_HEATER_WAIT_DURATION_MS}ms. Setting to {MAX_HEATER_WAIT_DURATION_MS}ms instead.");
0xff } else {
while duration > 0x3F {
duration /= 4;
factor += 1;
}
duration as u8 + factor * 64
}
}
pub fn calc_res_heat(
&self,
calibration_data: &CalibrationData,
ambient_temperature: i32,
) -> u8 {
let target_temperature = if self.heater_target_temperature > MAX_HEATER_TEMPERATURE {
warn!(
"Specified heater target temperature higher than {MAX_HEATER_TEMPERATURE}°C. Setting to 400°C instead."
);
400u16
} else {
self.heater_target_temperature
};
let var1 = ((ambient_temperature * calibration_data.par_gh3 as i32) / 1000) * 256;
let var2 = (calibration_data.par_gh1 as i32 + 784)
* (((((calibration_data.par_gh2 as i32 + 154009) * target_temperature as i32 * 5)
/ 100)
+ 3276800)
/ 10);
let var3 = var1 + (var2 / 2);
let var4 = var3 / (calibration_data.res_heat_range as i32 + 4);
let var5 = (131 * calibration_data.res_heat_val as i32) + 65536;
let heatr_res_x100 = ((var4 / var5) - 250) * 34;
((heatr_res_x100 + 50) / 100) as u8
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Configuration {
pub temperature_oversampling: Option<Oversampling>,
pub pressure_oversampling: Option<Oversampling>,
pub humidity_oversampling: Option<Oversampling>,
pub filter: Option<IIRFilter>,
pub gas_config: Option<GasConfig>,
}
impl Default for Configuration {
fn default() -> Self {
Self {
temperature_oversampling: Some(Oversampling::By2),
pressure_oversampling: Some(Oversampling::By16),
humidity_oversampling: Some(Oversampling::By1),
filter: Some(IIRFilter::Coeff1),
gas_config: Some(GasConfig::default()),
}
}
}
impl Configuration {
pub fn builder() -> ConfigBuilder {
ConfigBuilder {
config: Configuration::default(),
}
}
}
pub struct ConfigBuilder {
config: Configuration,
}
impl ConfigBuilder {
pub fn temperature_oversampling(mut self, oversampling: Oversampling) -> Self {
self.config.temperature_oversampling = Some(oversampling);
self
}
pub fn humidity_oversampling(mut self, oversampling: Oversampling) -> Self {
self.config.humidity_oversampling = Some(oversampling);
self
}
pub fn pressure_oversampling(mut self, oversampling: Oversampling) -> Self {
self.config.pressure_oversampling = Some(oversampling);
self
}
pub fn filter(mut self, filter: IIRFilter) -> Self {
self.config.filter = Some(filter);
self
}
pub fn gas_config(mut self, gas_config: Option<GasConfig>) -> Self {
self.config.gas_config = gas_config;
self
}
pub fn build(self) -> Configuration {
self.config
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Oversampling {
Skipped,
By1,
By2,
By4,
By8,
By16,
}
impl Oversampling {
pub fn cycles(&self) -> u32 {
match self {
Self::Skipped => 0,
Self::By1 => 1,
Self::By2 => 2,
Self::By4 => 4,
Self::By8 => 8,
Self::By16 => 16,
}
}
}
impl From<u8> for Oversampling {
fn from(val: u8) -> Self {
match val {
0 => Oversampling::Skipped,
1 => Oversampling::By1,
2 => Oversampling::By2,
3 => Oversampling::By4,
4 => Oversampling::By8,
_ => Oversampling::By16,
}
}
}
impl From<Oversampling> for u8 {
fn from(value: Oversampling) -> Self {
match value {
Oversampling::Skipped => 0,
Oversampling::By1 => 1,
Oversampling::By2 => 2,
Oversampling::By4 => 3,
Oversampling::By8 => 4,
Oversampling::By16 => 5,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum IIRFilter {
Coeff0,
Coeff1,
Coeff3,
Coeff7,
Coeff15,
Coeff31,
Coeff63,
Coeff127,
}
impl From<u8> for IIRFilter {
fn from(value: u8) -> Self {
match value {
0 => Self::Coeff0,
1 => Self::Coeff1,
2 => Self::Coeff3,
3 => Self::Coeff7,
4 => Self::Coeff15,
5 => Self::Coeff31,
6 => Self::Coeff63,
_ => Self::Coeff127,
}
}
}
impl From<IIRFilter> for u8 {
fn from(value: IIRFilter) -> Self {
match value {
IIRFilter::Coeff0 => 0,
IIRFilter::Coeff1 => 1,
IIRFilter::Coeff3 => 2,
IIRFilter::Coeff7 => 3,
IIRFilter::Coeff15 => 4,
IIRFilter::Coeff31 => 5,
IIRFilter::Coeff63 => 6,
IIRFilter::Coeff127 => 7,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum HeaterProfile {
Profile0,
Profile1,
Profile2,
Profile3,
Profile4,
Profile5,
Profile6,
Profile7,
Profile8,
Profile9,
}
impl From<u8> for HeaterProfile {
fn from(value: u8) -> Self {
match value {
0 => Self::Profile0,
1 => Self::Profile1,
2 => Self::Profile2,
3 => Self::Profile3,
4 => Self::Profile4,
5 => Self::Profile5,
6 => Self::Profile6,
7 => Self::Profile7,
8 => Self::Profile8,
_ => Self::Profile9,
}
}
}
impl From<HeaterProfile> for u8 {
fn from(value: HeaterProfile) -> Self {
match value {
HeaterProfile::Profile0 => 0,
HeaterProfile::Profile1 => 1,
HeaterProfile::Profile2 => 2,
HeaterProfile::Profile3 => 3,
HeaterProfile::Profile4 => 4,
HeaterProfile::Profile5 => 5,
HeaterProfile::Profile6 => 6,
HeaterProfile::Profile7 => 7,
HeaterProfile::Profile8 => 8,
HeaterProfile::Profile9 => 9,
}
}
}
#[cfg(test)]
mod config_tests {
extern crate std;
use std::time::Duration;
use crate::config::SensorMode;
use super::GasConfig;
#[test]
fn test_sensor_mode() {
let sleeping = 0u8;
let forced = 1u8;
assert!(SensorMode::Sleep == sleeping.into());
assert!(SensorMode::Forced == forced.into());
}
#[test]
fn test_gas_config() {
let config = GasConfig {
heater_duration: Duration::from_millis(100),
heater_target_temperature: 200,
};
assert!(config.calc_gas_wait() <= config.heater_duration.as_millis() as u8);
assert!(config.calc_gas_wait() == 0x59);
}
}