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
#![no_std]
extern crate embedded_hal;
pub mod device;
use core::marker::PhantomData;
use embedded_hal::blocking::i2c::{Write, WriteRead};
#[doc(inline)]
pub use device::av_conf::AvgH;
#[doc(inline)]
pub use device::av_conf::AvgT;
#[doc(inline)]
pub use device::cr1::DataRate;
fn clamp<T: PartialOrd>(n: T, min: T, max: T) -> T {
if n < min {
min
} else if n > max {
max
} else {
n
}
}
pub struct HTS221<Comm, E> {
comm: Comm,
calibration: device::Calibration,
_e: PhantomData<E>,
}
impl<Comm, E> HTS221<Comm, E>
where
Comm: Write<Error = E> + WriteRead<Error = E>,
{
pub fn humidity_x2(&mut self) -> Result<u16, E> {
let raw = device::HumidityOut::new(&mut self.comm)?.value();
Ok(self.convert_humidity_x2(raw))
}
pub fn temperature_x8(&mut self) -> Result<i16, E> {
let raw = device::TemperatureOut::new(&mut self.comm)?.value();
Ok(self.convert_temperature_x8(raw))
}
pub fn convert_humidity_x2(&self, raw: i16) -> u16 {
const MIN_HUMIDITY: u16 = 0;
const MAX_HUMIDITY: u16 = 100;
let h_range_x2 = (self.calibration.h1_rh_x2 - self.calibration.h0_rh_x2) as i16;
let adc_range = self.calibration.h1_t0_out - self.calibration.h0_t0_out;
let meas = raw - self.calibration.h0_t0_out;
let humidity_x2 = self.calibration.h0_rh_x2 as u16 +
(meas as i32 * h_range_x2 as i32 / adc_range as i32) as u16;
clamp(humidity_x2, MIN_HUMIDITY * 2, MAX_HUMIDITY * 2) as u16
}
pub fn convert_temperature_x8(&self, raw: i16) -> i16 {
const MIN_TEMPERATURE: i16 = -40;
const MAX_TEMPERATURE: i16 = 120;
let t_range_x8 = (self.calibration.t1_deg_c_x8 - self.calibration.t0_deg_c_x8) as i16;
let adc_range = self.calibration.t1_out - self.calibration.t0_out;
let meas = raw - self.calibration.t0_out;
let temperature_x8 = self.calibration.t0_deg_c_x8 as i16 +
(meas as i32 * t_range_x8 as i32 / adc_range as i32) as i16;
clamp(temperature_x8, MIN_TEMPERATURE * 8, MAX_TEMPERATURE * 8)
}
pub fn who_am_i(&mut self) -> Result<device::WhoAmI, E> {
device::WhoAmI::new(&mut self.comm)
}
pub fn av_conf(&mut self) -> Result<device::AvConf, E> {
device::AvConf::new(&mut self.comm)
}
pub fn cr1(&mut self) -> Result<device::CtrlReg1, E> {
device::CtrlReg1::new(&mut self.comm)
}
pub fn cr2(&mut self) -> Result<device::CtrlReg2, E> {
device::CtrlReg2::new(&mut self.comm)
}
pub fn cr3(&mut self) -> Result<device::CtrlReg3, E> {
device::CtrlReg3::new(&mut self.comm)
}
pub fn status(&mut self) -> Result<device::StatusReg, E> {
device::StatusReg::new(&mut self.comm)
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum UpdateMode {
Block,
Continuous,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Polarity {
High,
Low,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum PinMode {
PushPull,
OpenDrain,
}
pub struct Builder<Comm, E> {
comm: Comm,
avg_t: Option<AvgT>,
avg_h: Option<AvgH>,
powered_up: bool,
update_mode: UpdateMode,
data_rate: DataRate,
boot: bool,
data_ready_polarity: Option<Polarity>,
data_ready_mode: Option<PinMode>,
data_ready_enable: bool,
_e: PhantomData<E>,
}
impl<Comm, E> Builder<Comm, E>
where
Comm: Write<Error = E> + WriteRead<Error = E>,
{
pub fn new(comm: Comm) -> Self {
Self {
comm: comm,
avg_t: None,
avg_h: None,
powered_up: true,
update_mode: UpdateMode::Block,
data_rate: DataRate::OneShot,
boot: false,
data_ready_polarity: None,
data_ready_mode: None,
data_ready_enable: false,
_e: PhantomData,
}
}
pub fn with_avg_t(mut self, avg_t: AvgT) -> Self {
self.avg_t = Some(avg_t);
self
}
pub fn with_avg_h(mut self, avg_h: AvgH) -> Self {
self.avg_h = Some(avg_h);
self
}
pub fn powered_up(mut self) -> Self {
self.powered_up = true;
self
}
pub fn powered_down(mut self) -> Self {
self.powered_up = false;
self
}
pub fn with_update_mode(mut self, mode: UpdateMode) -> Self {
self.update_mode = mode;
self
}
pub fn with_data_rate(mut self, rate: DataRate) -> Self {
self.data_rate = rate;
self
}
pub fn with_boot(mut self) -> Self {
self.boot = true;
self
}
pub fn without_boot(mut self) -> Self {
self.boot = false;
self
}
pub fn with_data_ready_polarity(mut self, polarity: Polarity) -> Self {
self.data_ready_polarity = Some(polarity);
self
}
pub fn with_data_ready_mode(mut self, mode: PinMode) -> Self {
self.data_ready_mode = Some(mode);
self
}
pub fn with_data_ready_enabled(mut self) -> Self {
self.data_ready_enable = true;
self
}
pub fn with_data_ready_disabled(mut self) -> Self {
self.data_ready_enable = false;
self
}
pub fn build(mut self) -> Result<HTS221<Comm, E>, E> {
match (self.avg_t, self.avg_h) {
(Some(avg_t), Some(avg_h)) => {
let mut av_conf = device::AvConf::new(&mut self.comm)?;
av_conf.modify(&mut self.comm, |w| {
w.set_temperature_samples_averaged(avg_t);
w.set_humidity_samples_averaged(avg_h);
})?;
}
(Some(avg_t), None) => {
let mut av_conf = device::AvConf::new(&mut self.comm)?;
av_conf.modify(&mut self.comm, |w| {
w.set_temperature_samples_averaged(avg_t);
})?;
}
(None, Some(avg_h)) => {
let mut av_conf = device::AvConf::new(&mut self.comm)?;
av_conf.modify(&mut self.comm, |w| {
w.set_humidity_samples_averaged(avg_h);
})?;
}
(None, None) => (),
}
{
let powered_up = self.powered_up;
let update_mode = self.update_mode;
let data_rate = self.data_rate;
let mut cr1 = device::CtrlReg1::new(&mut self.comm)?;
cr1.modify(&mut self.comm, |w| {
if powered_up {
w.power_up();
} else {
w.power_down();
}
match update_mode {
UpdateMode::Block => w.set_block_update(),
UpdateMode::Continuous => w.set_continuous_update(),
}
w.set_data_rate(data_rate);
})?;
}
if self.boot {
device::CtrlReg2::new(&mut self.comm)?.modify(
&mut self.comm,
|w| { w.boot(); },
)?;
}
{
let data_ready_polarity = self.data_ready_polarity;
let data_ready_mode = self.data_ready_mode;
let data_ready_enable = self.data_ready_enable;
let mut cr3 = device::CtrlReg3::new(&mut self.comm)?;
cr3.modify(&mut self.comm, |w| {
match data_ready_polarity {
Some(Polarity::High) => w.data_ready_high(),
Some(Polarity::Low) => w.data_ready_low(),
None => (),
}
match data_ready_mode {
Some(PinMode::PushPull) => w.data_ready_push_pull(),
Some(PinMode::OpenDrain) => w.data_ready_open_drain(),
None => (),
}
if data_ready_enable {
w.data_ready_enable();
} else {
w.data_ready_disable();
}
})?;
}
let cal = device::Calibration::new(&mut self.comm)?;
Ok(HTS221::<Comm, E> {
comm: self.comm,
calibration: cal,
_e: PhantomData,
})
}
}