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
#![deny(warnings)]
#![no_std]

extern crate bit_field;
extern crate cast;
extern crate embedded_hal;

use crate::error::Error;
use crate::reg_conf::Configuration;
use crate::reg_device_id::DeviceId;
use crate::reg_manuf_id::ManufacturerId;
use crate::reg_res::Resolution;
use crate::reg_temp::Temperature;
use crate::reg_temp_alert_crit::CriticalTemperatureAlert;
use crate::reg_temp_alert_lower::LowerTemperatureAlert;
use crate::reg_temp_alert_upper::UpperTemperatureAlert;
use crate::address::SlaveAddress;
use embedded_hal::i2c::{I2c, SevenBitAddress};

pub mod error;
mod prelude;
pub mod address;
pub mod reg;
pub mod reg_conf;
pub mod reg_device_id;
pub mod reg_manuf_id;
pub mod reg_res;
pub mod reg_temp;
pub mod reg_temp_alert_crit;
pub mod reg_temp_alert_lower;
pub mod reg_temp_alert_upper;
pub mod reg_temp_generic;

/// MCP9808 Driver
pub struct MCP9808<I2C> {
    addr: u8,
    i2c: I2C,
}

impl<I2C> MCP9808<I2C>
where
    I2C: I2c<SevenBitAddress>,
    I2C::Error: Into<Error<I2C::Error>>,
{
    /// Creates a new driver from an I2C peripheral.
    pub fn new(i2c: I2C) -> Self {
        MCP9808 {
            addr: SlaveAddress::Default.into(),
            i2c,
        }
    }

    /// Change i2c address
    pub fn set_address(&mut self, addr: SlaveAddress) -> u8 {
        self.addr = addr.into();
        self.addr
    }

    /// release resources
    pub fn free(self) -> I2C {
        self.i2c
    }

    fn read_register<T>(&mut self, mut reg: T) -> Result<T, Error<I2C::Error>>
    where
        T: prelude::Read,
        I2C: I2c<SevenBitAddress>,
        I2C::Error: Into<Error<I2C::Error>>,
    {
        reg.read_from_device(&mut self.i2c, self.addr)?;
        Ok(reg)
    }

    pub fn write_register<R: prelude::Write>(&mut self, reg: R) -> Result<(), Error<I2C::Error>> {
        reg.write_to_device(&mut self.i2c, self.addr)?;
        Ok(())
    }

    pub fn read_configuration(&mut self) -> Result<impl Configuration, Error<I2C::Error>> {
        self.read_register(reg_conf::new())
    }

    pub fn read_device_id(&mut self) -> Result<impl DeviceId, Error<I2C::Error>> {
        self.read_register(reg_device_id::new())
    }

    pub fn read_manufacturer_id(&mut self) -> Result<impl ManufacturerId, Error<I2C::Error>> {
        self.read_register(reg_manuf_id::new())
    }

    pub fn read_resolution(&mut self) -> Result<impl Resolution, Error<I2C::Error>> {
        self.read_register(reg_res::new())
    }

    /// Read temperature register. Its double-buffered so no wait required.
    pub fn read_temperature(&mut self) -> Result<impl Temperature, Error<I2C::Error>> {
        self.read_register(reg_temp::new())
    }

    pub fn read_alert_critical(
        &mut self,
    ) -> Result<impl CriticalTemperatureAlert, Error<I2C::Error>> {
        self.read_register(reg_temp_alert_crit::new())
    }

    pub fn read_alert_lower(&mut self) -> Result<impl LowerTemperatureAlert, Error<I2C::Error>> {
        self.read_register(reg_temp_alert_lower::new())
    }

    pub fn read_alert_upper(&mut self) -> Result<impl UpperTemperatureAlert, Error<I2C::Error>> {
        self.read_register(reg_temp_alert_upper::new())
    }
}