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
use core::time::Duration;
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
pub enum OversamplingSetting {
OSNone = 0,
OS1x = 1,
OS2x = 2,
OS4x = 3,
OS8x = 4,
OS16x = 5,
}
impl OversamplingSetting {
pub fn from_u8(os: u8) -> OversamplingSetting {
match os {
0 => OversamplingSetting::OSNone,
1 => OversamplingSetting::OS1x,
2 => OversamplingSetting::OS2x,
3 => OversamplingSetting::OS4x,
4 => OversamplingSetting::OS8x,
5 => OversamplingSetting::OS16x,
_ => panic!("Unknown oversampling setting: {}", os),
}
}
}
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
pub enum IIRFilterSize {
Size0 = 0,
Size1 = 1,
Size3 = 2,
Size7 = 3,
Size15 = 4,
Size31 = 5,
Size63 = 6,
Size127 = 7,
}
impl IIRFilterSize {
pub fn from_u8(os: u8) -> IIRFilterSize {
match os {
0 => IIRFilterSize::Size0,
1 => IIRFilterSize::Size1,
2 => IIRFilterSize::Size3,
3 => IIRFilterSize::Size7,
4 => IIRFilterSize::Size15,
5 => IIRFilterSize::Size31,
6 => IIRFilterSize::Size63,
7 => IIRFilterSize::Size127,
_ => panic!("Unknown IIRFilterSize: {}", os),
}
}
}
#[derive(Debug, Default, Copy)]
#[repr(C)]
pub struct TphSett {
pub os_hum: Option<OversamplingSetting>,
pub os_temp: Option<OversamplingSetting>,
pub os_pres: Option<OversamplingSetting>,
pub filter: Option<IIRFilterSize>,
pub temperature_offset: Option<i32>,
}
impl Clone for TphSett {
fn clone(&self) -> Self {
*self
}
}
#[derive(Debug, Default, Copy)]
#[repr(C)]
pub struct GasSett {
pub nb_conv: u8,
pub heatr_ctrl: Option<u8>,
pub run_gas_measurement: bool,
pub heatr_temp: Option<u16>,
pub heatr_dur: Option<Duration>,
pub ambient_temperature: i8,
}
impl Clone for GasSett {
fn clone(&self) -> Self {
*self
}
}
#[derive(Debug, Default, Copy)]
pub struct SensorSettings {
pub gas_sett: GasSett,
pub tph_sett: TphSett,
}
impl Clone for SensorSettings {
fn clone(&self) -> Self {
*self
}
}
bitflags! {
#[derive(Default)]
pub struct DesiredSensorSettings: u16 {
const OST_SEL = 1;
const OSP_SEL = 2;
const OSH_SEL = 4;
const GAS_MEAS_SEL = 8;
const FILTER_SEL = 16;
const HCNTRL_SEL = 32;
const RUN_GAS_SEL = 64;
const NBCONV_SEL = 128;
const GAS_SENSOR_SEL = Self::GAS_MEAS_SEL.bits | Self::RUN_GAS_SEL.bits | Self::NBCONV_SEL.bits;
}
}
pub struct SettingsBuilder {
desired_settings: DesiredSensorSettings,
sensor_settings: SensorSettings,
}
pub type Settings = (SensorSettings, DesiredSensorSettings);
impl SettingsBuilder {
pub fn new() -> SettingsBuilder {
SettingsBuilder {
desired_settings: Default::default(),
sensor_settings: Default::default(),
}
}
pub fn with_temperature_filter(mut self, filter: IIRFilterSize) -> SettingsBuilder {
self.sensor_settings.tph_sett.filter = Some(filter);
self.desired_settings |= DesiredSensorSettings::FILTER_SEL;
self
}
pub fn with_humidity_control(mut self, heatr_control: u8) -> SettingsBuilder {
self.sensor_settings.gas_sett.heatr_ctrl = Some(heatr_control);
self.desired_settings |= DesiredSensorSettings::HCNTRL_SEL;
self
}
pub fn with_temperature_oversampling(
mut self,
os_temp: OversamplingSetting,
) -> SettingsBuilder {
self.sensor_settings.tph_sett.os_temp = Some(os_temp);
self.desired_settings |= DesiredSensorSettings::OST_SEL;
self
}
pub fn with_pressure_oversampling(mut self, os_pres: OversamplingSetting) -> SettingsBuilder {
self.sensor_settings.tph_sett.os_pres = Some(os_pres);
self.desired_settings |= DesiredSensorSettings::OSP_SEL;
self
}
pub fn with_humidity_oversampling(mut self, os_hum: OversamplingSetting) -> SettingsBuilder {
self.sensor_settings.tph_sett.os_hum = Some(os_hum);
self.desired_settings |= DesiredSensorSettings::OSH_SEL;
self
}
pub fn with_gas_measurement(
mut self,
heatr_dur: Duration,
heatr_temp: u16,
ambient_temperature: i8,
) -> SettingsBuilder {
self.sensor_settings.gas_sett.heatr_dur = Some(heatr_dur);
self.sensor_settings.gas_sett.heatr_temp = Some(heatr_temp);
self.sensor_settings.gas_sett.ambient_temperature = ambient_temperature;
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
self
}
pub fn with_nb_conv(mut self, nb_conv: u8) -> SettingsBuilder {
self.sensor_settings.gas_sett.nb_conv = nb_conv;
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
self
}
pub fn with_run_gas(mut self, run_gas: bool) -> SettingsBuilder {
self.sensor_settings.gas_sett.run_gas_measurement = run_gas;
self.desired_settings |= DesiredSensorSettings::GAS_SENSOR_SEL;
self
}
pub fn with_temperature_offset(mut self, offset: i32) -> SettingsBuilder {
self.sensor_settings.tph_sett.temperature_offset = Some(offset);
self
}
pub fn build(self) -> Settings {
(self.sensor_settings, self.desired_settings)
}
}