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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! This is a platform agnostic Rust driver for the lm73 temperature
//! sensor and thermal watchdog, based on the [`embedded-hal`] traits.
//!
//! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal
//!
//! This driver allows you to:
//! - Enable/disable the device.
//! - Read the temperature.
//! - Set the OS temperature.
//! - Set the OS operation mode.
//!
//! ## The device
//!
//! The lm73 temperature sensor includes a delta-sigma analog-to-digital
//! converter, and a digital overtemperature detector. The host can
//! query the lm73 through its I2C interface to read temperature at any
//! time. 
//! 
//! Datasheet:
//! - [lm73](https://datasheets.maximintegrated.com/en/ds/lm73.pdf)
//!
//!
//! ## Usage examples (see also examples folder)
//!
//! To use this driver, import this crate and an `embedded_hal` implementation,
//! then instantiate the device.
//!
//! ### Read temperature
//!
//! ```no_run
//! extern crate linux_embedded_hal as hal;
//! extern crate lm73;
//!
//! use hal::I2cdev;
//! use lm73::{ Lm73, SlaveAddr };
//!
//! # fn main() {
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let address = SlaveAddr::default();
//! let mut sensor = Lm73::new(dev, address);
//! let temp_celsius = sensor.read_temperature().unwrap();
//! println!("Temperature: {}ºC", temp_celsius);
//! # }
//! ```
//!
//! ### Provide an alternative address
//!
//! ```no_run
//! extern crate linux_embedded_hal as hal;
//! extern crate lm73;
//!
//! use hal::I2cdev;
//! use lm73::{ Lm73, SlaveAddr };
//!
//! # fn main() {
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let (a2, a1, a0) = (false, false, true);
//! let address = SlaveAddr::Alternative(a2, a1, a0);
//! let mut sensor = Lm73::new(dev, address);
//! # }
//! ```
//!
//!
//! ### Set the OneShot operation mode
//!
//! ```no_run
//! extern crate linux_embedded_hal as hal;
//! extern crate lm73;
//!
//! use hal::I2cdev;
//! use lm73::{ Lm73, SlaveAddr, OsMode };
//!
//! # fn main() {
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let mut sensor = Lm73::new(dev, SlaveAddr::default());
//! sensor.set_os_mode(OsMode::Enabled).unwrap();
//! # }
//! ```
//!
//! ### Set the HIGH_TEMP temperature
//!
//! ```no_run
//! extern crate linux_embedded_hal as hal;
//! extern crate lm73;
//!
//! use hal::I2cdev;
//! use lm73::{ Lm73, SlaveAddr };
//!
//! # fn main() {
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let mut sensor = Lm73::new(dev, SlaveAddr::default());
//! let temp_celsius = 50.0;
//! sensor.set_temperature_high(temp_celsius).unwrap();
//! # }
//! ```
//!
//! ### Enable / disable the sensor
//!
//! ```no_run
//! extern crate linux_embedded_hal as hal;
//! extern crate lm73;
//!
//! use hal::I2cdev;
//! use lm73::{ Lm73, SlaveAddr };
//!
//! # fn main() {
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let mut sensor = Lm73::new(dev, SlaveAddr::default());
//! sensor.disable().unwrap(); // shutdown
//! sensor.enable().unwrap();
//! # }
//! ```

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

extern crate embedded_hal as hal;
extern crate i2cdev;

use hal::blocking::i2c;

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

/// Possible slave addresses
#[derive(Debug, Clone)]
pub enum SlaveAddr {
    /// Default slave address
    Default,
    /// Alternative slave address providing bit values for A2, A1 and A0
    Alternative(bool, bool, bool),
}

impl Default for SlaveAddr {
    /// Default slave address
    fn default() -> Self {
        SlaveAddr::Default
    }
}

impl SlaveAddr {
    fn addr(self, default: u8) -> u8 {
        match self {
            SlaveAddr::Default => default,
            SlaveAddr::Alternative(a2, a1, a0) => {
                default | ((a2 as u8) << 2) | ((a1 as u8) << 1) | a0 as u8
            }
        }
    }
}

/// Resolution of temperature
///
/// Number of consecutive faults necessary to trigger OS condition.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Resolution {
    /// 11 bits, 0.25(default)
    _11,
    /// 12 bits, 0.125
    _12,
    /// 13 bits, 0.0625
    _13,
    /// 14 bits, 0.03125
    _14,
}

/// OS polarity
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OsPolarity {
    /// Active low (default)
    ActiveLow,
    /// Active high
    ActiveHigh,
}

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

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

const DEVICE_BASE_ADDRESS: u8 = 0b100_1000;

struct Register;

impl Register {
    const TEMPERATURE: u8 = 0x00;
    const CONFIGURATION: u8 = 0x01;
    const T_HIGH: u8 = 0x02;
    const T_LOW: u8 = 0x03;
    const CONTROL_STATUS: u8 = 0x04;
    const IDENTIFICATION: u8 = 0x07;
}

struct ConfigBitFlags;

impl ConfigBitFlags {
    //   const RESERVED0_0 : u8 = 0b0000_0001;
    //   const RESERVED1_0 : u8 = 0b0000_0010;
    const ONE_SHOT: u8 = 0b0000_0100;
    const ALRT_RST: u8 = 0b0000_1000;
    const ALRT_POL: u8 = 0b0001_0000;
    const ALRT_EN: u8 = 0b0010_0000;
    //   const RESERVED6_1 : u8 = 0b0100_0000;
    const PD: u8 = 0b1000_0000;
}

#[derive(Debug, Clone, Copy)]
struct Config {
    bits: u8,
}

impl Config {
    fn with_high(self, mask: u8) -> Self {
        Config {
            bits: self.bits | mask,
        }
    }
    fn with_low(self, mask: u8) -> Self {
        Config {
            bits: self.bits & !mask,
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Config { bits: 0x40 }
    }
}

struct CsrBitFlags;

impl CsrBitFlags {
    //   const DAV         : u8 = 0b0000_0001;
    //   const TLOW        : u8 = 0b0000_0010;
    //   const THI         : u8 = 0b0000_0100;
    //   const ALRT_STAT   : u8 = 0b0000_1000;
    //   const RESERVED4_0 : u8 = 0b0001_0000;
    const RES0: u8 = 0b0010_0000;
    const RES1: u8 = 0b0100_0000;
    //   const TO_DIS      : u8 = 0b1000_0000;
}

#[derive(Debug, Clone, Copy)]
struct Csr {
    bits: u8,
}

impl Csr {
    fn with_high(self, mask: u8) -> Self {
        Csr {
            bits: self.bits | mask,
        }
    }
    fn with_low(self, mask: u8) -> Self {
        Csr {
            bits: self.bits & !mask,
        }
    }
}

impl Default for Csr {
    fn default() -> Self {
        Csr { bits: 0b0000_1000 }
    }
}

/// Lm73 device driver.
#[derive(Debug, Default)]
pub struct Lm73<I2C> {
    /// The concrete I²C device implementation.
    i2c: I2C,
    /// The I²C device address.
    address: u8,
    /// Configuration register status.
    config: Config,
    /// Control and Status register.
    csr: Csr,
}

mod conversion;

impl<I2C, E> Lm73<I2C>
where
    I2C: i2c::Write<Error = E>,
{
    /// Create new instance of the lm73 device.
    pub fn new(i2c: I2C, address: SlaveAddr) -> Self {
        Lm73 {
            i2c,
            address: address.addr(DEVICE_BASE_ADDRESS),
            config: Config::default(),
            csr: Csr::default(),
        }
    }

    /// Destroy driver instance, return I²C bus instance.
    pub fn destroy(self) -> I2C {
        self.i2c
    }

    /// Enable the sensor (default state).
    pub fn enable(&mut self) -> Result<(), Error<E>> {
        let csr = self.csr;
        let _result = self.write_csr(csr)?;

        let config = self.config;
        self.write_config(config.with_low(ConfigBitFlags::PD))
    }

    /// Disable the sensor (shutdown).
    pub fn disable(&mut self) -> Result<(), Error<E>> {
        let config = self.config;
        self.write_config(config.with_high(ConfigBitFlags::PD))
    }

    /// Set temperature resolution.
    ///
    /// Set temperature conversion resolution.
    pub fn set_resolution(&mut self, res: Resolution) -> Result<(), Error<E>> {
        let csr = self.csr;
        match res {
            Resolution::_11 => {
                self.write_csr(csr.with_low(CsrBitFlags::RES1).with_low(CsrBitFlags::RES0))
            }
            Resolution::_12 => {
                self.write_csr(csr.with_low(CsrBitFlags::RES1).with_high(CsrBitFlags::RES0))
            }
            Resolution::_13 => {
                self.write_csr(csr.with_high(CsrBitFlags::RES1).with_low(CsrBitFlags::RES0))
            }
            Resolution::_14 => self.write_csr(
                csr.with_high(CsrBitFlags::RES1)
                    .with_high(CsrBitFlags::RES0),
            ),
        }
    }

    /// Set the OneShot operation mode.
    pub fn set_os_mode(&mut self, mode: OsMode) -> Result<(), Error<E>> {
        let config = self.config;
        match mode {
            OsMode::Enabled => self.write_config(config.with_high(ConfigBitFlags::ONE_SHOT)),
            OsMode::Disabled => self.write_config(config.with_low(ConfigBitFlags::ONE_SHOT)),
        }
    }

    /// Reset Alert pin.
    pub fn set_alert_reset(&mut self) -> Result<(), Error<E>> {
        let config = self.config;
        self.write_config(config.with_high(ConfigBitFlags::ALRT_RST))
    }

    /// Set the ALRT_POL polarity.
    pub fn set_alert_polarity(&mut self, polarity: OsPolarity) -> Result<(), Error<E>> {
        let config = self.config;
        match polarity {
            OsPolarity::ActiveLow => self.write_config(config.with_low(ConfigBitFlags::ALRT_POL)),
            OsPolarity::ActiveHigh => self.write_config(config.with_high(ConfigBitFlags::ALRT_POL)),
        }
    }

    /// Set the ALRT_EN
    pub fn set_alert_enable(&mut self, enabled: AlertEnable) -> Result<(), Error<E>> {
        let config = self.config;
        match enabled {
            AlertEnable::Disabled => self.write_config(config.with_high(ConfigBitFlags::ALRT_EN)),
            AlertEnable::Enabled => self.write_config(config.with_low(ConfigBitFlags::ALRT_EN)),
        }
    }

    /// Set temperature_high (celsius).
    pub fn set_temperature_high(&mut self, temperature: f32) -> Result<(), Error<E>> {
        if temperature < -256.0 || temperature > 255.75 {
            return Err(Error::InvalidInputData);
        }
        let (msb, lsb) = conversion::convert_temp_to_register(temperature);
        let _data: u16 = (msb as u16 * 256 as u16 + lsb as u16).into();
        self.i2c
            .write(Register::T_HIGH, &[msb, lsb])
            .map_err(Error::I2C)
    }

    /// Set temperature_low (celsius).
    pub fn set_temperature_low(&mut self, temperature: f32) -> Result<(), Error<E>> {
        if temperature < -256.0 || temperature > 255.75 {
            return Err(Error::InvalidInputData);
        }
        let (msb, lsb) = conversion::convert_temp_to_register(temperature);
        let _data: u16 = (msb as u16 * 256 as u16 + lsb as u16).into();
        self.i2c
            .write(Register::T_LOW, &[msb, lsb])
            .map_err(Error::I2C)
    }

    fn write_config(&mut self, config: Config) -> Result<(), Error<E>> {
        self.i2c
            .write(Register::CONFIGURATION, &[config.bits])
            .map_err(Error::I2C)?;
        self.config = config;
        Ok(())
    }

    fn write_csr(&mut self, csr: Csr) -> Result<(), Error<E>> {
        self.i2c
            .write(Register::CONTROL_STATUS, &[csr.bits])
            .map_err(Error::I2C)?;
        self.csr = csr;
        Ok(())
    }
}

impl<I2C, E> Lm73<I2C>
where
    I2C: i2c::WriteRead<Error = E>,
{
    /// Read the temperature from the sensor (celsius).
    pub fn read_temperature(&mut self) -> Result<f32, Error<E>> {
        let mut data = [0; 2];
        self.i2c
            .write_read(self.address, &[Register::TEMPERATURE], &mut data)
            .map_err(Error::I2C)?;
        Ok(conversion::convert_temp_from_register(data[0], data[1]))
    }

    /// Read the identification sensor register.
    pub fn read_identification(&mut self) -> Result<u16, Error<E>> {
        let mut data = [0; 2];
        self.i2c
            .write_read(self.address, &[Register::IDENTIFICATION], &mut data)
            .map_err(Error::I2C)?;

        Ok( (data[0] as u16) << 8 + data[1])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_get_default_address() {
        let addr = SlaveAddr::default();
        assert_eq!(DEVICE_BASE_ADDRESS, addr.addr(DEVICE_BASE_ADDRESS));
    }

    #[test]
    fn can_generate_alternative_addresses() {
        assert_eq!(
            0b100_1001,
            SlaveAddr::Alternative(false, false, true).addr(DEVICE_BASE_ADDRESS)
        );
        assert_eq!(
            0b100_1010,
            SlaveAddr::Alternative(false, true, false).addr(DEVICE_BASE_ADDRESS)
        );
        assert_eq!(
            0b100_1100,
            SlaveAddr::Alternative(true, false, false).addr(DEVICE_BASE_ADDRESS)
        );
        assert_eq!(
            0b100_1101,
            SlaveAddr::Alternative(true, false, true).addr(DEVICE_BASE_ADDRESS)
        );
        assert_eq!(
            0b100_1110,
            SlaveAddr::Alternative(true, true, false).addr(DEVICE_BASE_ADDRESS)
        );
    }
}