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
#![deny(missing_docs, unsafe_code, warnings)]
#![no_std]
extern crate byteorder;
extern crate i2cdev;
use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
use i2cdev::core::I2CDevice;
#[derive(Debug)]
pub enum Error<E> {
I2C(E),
InvalidInputData,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TResolution {
_11,
_14,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HResolution {
_8,
_11,
_14,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HeaterMode {
Disabled,
Enabled,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Mode {
TOrH,
TAndH,
}
struct Register;
impl Register {
const TEMPERATURE: u8 = 0x00;
const HUMIDITY: u8 = 0x01;
const CONFIGURATION: u8 = 0x02;
const SERIAL_ID1: u8 = 0xFB;
const SERIAL_ID2: u8 = 0xFC;
const SERIAL_ID3: u8 = 0xFD;
const MANUFACTURER: u8 = 0xFE;
const DEVICE_ID: u8 = 0xFF;
}
struct ConfigBitFlags;
impl ConfigBitFlags {
const RST : u16 = 0b1000_0000;
const HEAT: u16 = 0b0010_0000;
const MODE: u16 = 0b0001_0000;
const BTST :u16 = 0b0000_1000_0000_0000;
const T_MODE: u16 = 0b0000_0100;
const H_MODE9: u16 = 0b000_0010;
const H_MODE8: u16 = 0b000_0001;
}
#[derive(Debug, Clone, Copy)]
pub struct Config {
bits: u16,
}
impl Default for Config {
fn default() -> Self {
Config { bits: 0x00 }
}
}
impl Config{
fn with_high(&mut self, mask: u16) {
self.bits |= mask;
}
fn with_low(&mut self, mask: u16) {
self.bits &= !mask;
}
pub fn set_heater(&mut self, mode: HeaterMode )
{
match mode {
HeaterMode::Enabled => self.with_high(ConfigBitFlags::HEAT),
HeaterMode::Disabled => self.with_low(ConfigBitFlags::HEAT),
};
}
pub fn set_reset(&mut self)
{
self.with_high(ConfigBitFlags::RST);
}
pub fn set_mode(&mut self, mode: Mode)
{
match mode {
Mode::TAndH => self.with_high(ConfigBitFlags::MODE),
Mode::TOrH => self.with_low(ConfigBitFlags::MODE),
};
}
pub fn set_temp_resolution(&mut self, res: TResolution)
{
match res {
TResolution::_11 => self.with_high(ConfigBitFlags::T_MODE),
TResolution::_14 => self.with_low(ConfigBitFlags::T_MODE),
};
}
pub fn set_humidity_resolution(&mut self, res: HResolution)
{
match res {
HResolution::_8 => {self.with_low(ConfigBitFlags::H_MODE8);
self.with_high(ConfigBitFlags::H_MODE9);
},
HResolution::_11 => {self.with_high(ConfigBitFlags::H_MODE9);
self.with_low(ConfigBitFlags::H_MODE8);
},
HResolution::_14 => {self.with_low(ConfigBitFlags::H_MODE9);
self.with_low(ConfigBitFlags::H_MODE8);},
};
}
}
#[derive(Debug, Default)]
pub struct Hdc1080<I2C> {
i2c: I2C,
}
impl<I2C> Hdc1080<I2C>
where
I2C: I2CDevice,
{
pub fn new(i2c: I2C) -> Self {
Hdc1080 {
i2c,
}
}
pub fn destroy(self) -> I2C {
self.i2c
}
fn read_u16_be_data(&mut self) -> Result<u16, I2C::Error> {
let mut data = [0u8; 2];
self.i2c.read(&mut data)?;
let mut current = &data[..];
let result = current.read_u16::<BigEndian>().unwrap();
Ok(result)
}
fn compute_temp(&mut self, data: u16) -> f32 {
f32::from(data) * 165.0 / 65536.0 - 40.0
}
fn compute_humidity(&mut self, data: u16) -> f32 {
f32::from(data) * 100.0 / 65536.0
}
pub fn read_temperature_finish(&mut self) -> Result<f32, I2C::Error> {
let data = self.read_u16_be_data().unwrap();
Ok(self.compute_temp(data))
}
pub fn read_humidity_finish(&mut self) -> Result<f32, I2C::Error> {
let data = self.read_u16_be_data().unwrap();
Ok(self.compute_humidity(data))
}
pub fn read_temperature_humidity_finish(&mut self) -> Result<(f32, f32), I2C::Error> {
let mut data = [0u8; 4];
self.i2c.read(&mut data)?;
let mut current = &data[..];
let t = current.read_u16::<BigEndian>().unwrap();
let h = current.read_u16::<BigEndian>().unwrap();
Ok((self.compute_temp(t), self.compute_humidity(h)))
}
pub fn read_temperature_start(&mut self) -> Result<(), I2C::Error> {
self.i2c.write(&[Register::TEMPERATURE])
}
pub fn read_humidity_start(&mut self) -> Result<(), I2C::Error> {
self.i2c.write(&[Register::HUMIDITY])
}
pub fn write_config(&mut self,config: Config) -> Result<(), I2C::Error> {
let write_bits = config.bits & 0xB7;
self.i2c
.smbus_write_word_data(Register::CONFIGURATION, write_bits)
}
pub fn read_config(&mut self) -> Result<u16, I2C::Error> {
let mut data = [0u16; 1];
data[0] = self
.i2c
.smbus_read_word_data(Register::CONFIGURATION)
.unwrap();
BigEndian::from_slice_u16(&mut data);
Ok(data[0])
}
pub fn battery_low(&mut self) -> Result<bool, I2C::Error> {
let config = self.read_config().unwrap();
Ok((config & ConfigBitFlags::BTST) == ConfigBitFlags::BTST)
}
pub fn read_manufacturer(&mut self) -> Result<u16, I2C::Error> {
let mut data = [0u16; 1];
data[0] = self
.i2c
.smbus_read_word_data(Register::MANUFACTURER)
.unwrap();
BigEndian::from_slice_u16(&mut data);
Ok(data[0])
}
pub fn read_device_id(&mut self) -> Result<u16, I2C::Error> {
let mut data = [0u16; 1];
data[0] = self.i2c.smbus_read_word_data(Register::DEVICE_ID).unwrap();
BigEndian::from_slice_u16(&mut data);
Ok(data[0])
}
pub fn read_serial(&mut self) -> Result<u64, I2C::Error> {
let mut serial = [0u16; 3];
serial[2] = self.i2c.smbus_read_word_data(Register::SERIAL_ID1).unwrap();
serial[1] = self.i2c.smbus_read_word_data(Register::SERIAL_ID2).unwrap();
serial[0] = self.i2c.smbus_read_word_data(Register::SERIAL_ID3).unwrap();
BigEndian::from_slice_u16(&mut serial);
let mut result = u64::from(serial[2]) * 65536 * 65536
+ (u64::from(serial[1]) * 65536)
+ u64::from(serial[0]);
result >>= 7;Ok(result)
}
}