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
//! This is a platform agnostic Rust driver for the HDC1080 temperature
//! sensor and thermal watchdog, based on the [`I2CDevice`] traits.
//!
//! [`I2CDevice`]: https://github.com/rust-embedded/i2cdev
//!
//! This driver allows you to:
//! - Read the temperature.
//! - Read the relative humidity.
//! - Read both temperature and humidity.
//! - Test low battery condition.
//! - Set temperature resolution.
//! - Set humidity resolution.
//! - Set heater on/off.
//! - Reset device.
//!
//! ## The device
//!
//! The HDC1080 is a digital humidity sensor with integrated temperature sensor. The host can
//! query the HDC1080 through its I2C interface to start a temperature and/or humidity reading at any
//! time.
//!
//! Datasheet:
//! - [hdc1080](http://www.ti.com/lit/ds/symlink/hdc1080.pdf)
//!
//!
//! ## Usage examples (see also examples folder)
//!
//! To use this driver, import this crate and an `I2CDevice` implementation,
//! then instantiate the device.
//!
//! ### Read temperature
//!
//! ```no_run
//! extern crate i2cdev;
//! extern crate hdc1080;
//!
//! use i2cdev::linux::LinuxI2CDevice;
//! use std::{thread, time};
//! use hdc1080::{Config, Hdc1080};
//!
//! const HDC1080_SLAVE_ADDR: u16 = 0x40;
//!
//! # fn main() {
//! let dev = LinuxI2CDevice::new("/dev/i2c-0", HDC1080_SLAVE_ADDR).unwrap();
//! let mut sensor = Hdc1080::new(dev);
//! sensor.read_temperature_start().unwrap();
//! thread::sleep(time::Duration::from_millis(500u64));
//! let temp_celsius = sensor.read_temperature_finish().unwrap();
//! println!("Temperature: {}ºC", temp_celsius);
//! # }
//! ```
//!
//! ### Read relative humidity
//!
//! ```no_run
//! extern crate i2cdev;
//! extern crate hdc1080;
//!
//! use i2cdev::linux::LinuxI2CDevice;
//! use std::{thread, time};
//! use hdc1080::{Config, Hdc1080};
//!
//! const HDC1080_SLAVE_ADDR: u16 = 0x40;
//!
//! # fn main() {
//! let dev = LinuxI2CDevice::new("/dev/i2c-0", HDC1080_SLAVE_ADDR).unwrap();
//! let mut sensor = Hdc1080::new(dev);
//! sensor.read_humidity_start().unwrap();
//! thread::sleep(time::Duration::from_millis(500u64));
//! let humidity = sensor.read_humidity_finish().unwrap();
//! println!("Temperature: {}ºC", humidity);
//! # }
//! ```
//! 
//! 
//! ### Read temperature and relative humidity in sequence
//! 
//! ```no_run
//! extern crate i2cdev;
//! extern crate hdc1080;
//!
//! use i2cdev::linux::LinuxI2CDevice;
//! use std::{thread, time};
//! use hdc1080::{Config, Hdc1080, Mode};
//!
//! const HDC1080_SLAVE_ADDR: u16 = 0x40;
//!
//! # fn main() {
//! let dev = LinuxI2CDevice::new("/dev/i2c-0", HDC1080_SLAVE_ADDR).unwrap();
//! let mut sensor = Hdc1080::new(dev);
//! let mut config: Config = hdc1080::Config::default();
//! config.set_mode(Mode::TAndH);
//! sensor.write_config(config).unwrap();
//! sensor.read_temperature_start().unwrap();
//! thread::sleep(time::Duration::from_millis(500u64));
//! let (temp,humidity) = sensor.read_temperature_humidity_finish().unwrap();
//! println!("Temperature: {}ºC, Humidity: {}RH", temp,humidity);
//! # }
//! ```


#![deny(missing_docs, unsafe_code, warnings)]
#![no_std]

extern crate byteorder;
extern crate i2cdev;

use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
use i2cdev::core::I2CDevice;

/// All possible errors in this crate
#[derive(Debug)]
pub enum Error<E> {
    /// I²C bus error
    I2C(E),
    /// Invalid input data
    InvalidInputData,
}

/// Resolution of temperature
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TResolution {
    /// 11 bits
    _11,
    /// 14 bits
    _14,
}


/// Resolution of humidity
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HResolution {
    /// 8 bits
    _8,
    /// 11 bits
    _11,
    /// 14 bits
    _14,
}

/// Heater operation mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HeaterMode {
    /// Disabled (default)
    Disabled,
    /// Enabled
    Enabled,
}


/// Heater operation mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Mode {
    /// Temperature or Humidity (default)
    TOrH,
    /// Temperature and Humidity
    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;
}

/// Config register content to be sent
#[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;
    }

    /// Set heater mode
    pub fn set_heater(&mut self, mode: HeaterMode )
    {
        match mode {
            HeaterMode::Enabled => self.with_high(ConfigBitFlags::HEAT),
            HeaterMode::Disabled => self.with_low(ConfigBitFlags::HEAT),
        };
    }

    /// Enable reset
    pub fn set_reset(&mut self)
    {
        self.with_high(ConfigBitFlags::RST);
    }

    /// Set Temperature or/and Humidity
    pub fn set_mode(&mut self, mode: Mode)
    {
        match mode {
            Mode::TAndH => self.with_high(ConfigBitFlags::MODE),        
            Mode::TOrH => self.with_low(ConfigBitFlags::MODE),
        };
    }

    /// Set temperature resolution
    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),
        };
    }

    /// Set humidity resolution
    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);},
        };
    }

}

/// Hdc1080 device driver.
#[derive(Debug, Default)]
pub struct Hdc1080<I2C> {
    /// The concrete I²C device implementation.
    i2c: I2C,
}

impl<I2C> Hdc1080<I2C>
where
    I2C: I2CDevice,
{
    /// Create new instance of the hdc1080 device.
    pub fn new(i2c: I2C) -> Self {
        Hdc1080 {
            i2c,
            }
    }

    /// Destroy driver instance, return I²C bus instance.
    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
    }

    /// Read the temperature from the sensor (celsius).
    pub fn read_temperature_finish(&mut self) -> Result<f32, I2C::Error> {
        let data = self.read_u16_be_data().unwrap();
        Ok(self.compute_temp(data))
    }

    /// Read humidity from the sensor (RH%).
    pub fn read_humidity_finish(&mut self) -> Result<f32, I2C::Error> {
        let data = self.read_u16_be_data().unwrap();
        Ok(self.compute_humidity(data))
    }

    /// Read the temperature from the sensor (celsius).
    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)))
    }

    /// Read the temperature from the sensor (celsius).
    pub fn read_temperature_start(&mut self) -> Result<(), I2C::Error> {
        self.i2c.write(&[Register::TEMPERATURE])
    }

    /// Read the humidity from the sensor (RH).
    pub fn read_humidity_start(&mut self) -> Result<(), I2C::Error> {
        self.i2c.write(&[Register::HUMIDITY])
    }

    /// Write config register, 0x02
    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)
    }


    /// Read config, register 0x02
    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])
    }

    /// Returns true if battery voltage is under 2.8v
    pub fn battery_low(&mut self) -> Result<bool, I2C::Error> {
        let config = self.read_config().unwrap();
        Ok((config & ConfigBitFlags::BTST) == ConfigBitFlags::BTST)
    }

    /// Read manufacturer, register 0xfe
    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])
    }

    /// Read device id, register 0xff
    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])
    }

    /// Read serial registers 0xfb, 0xfc, 0xfd.
    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;//this might be an error in TI docs, section 8.6.4. It is 40 bits or 41?
        Ok(result)
    }
}