max6642/
lib.rs

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
//! Implements a driver for the MAX6642 temperature sensor.
#![no_std]
#![deny(warnings)]

use bit_field::BitField;
use embedded_hal::i2c::{ErrorType, I2c};

#[allow(dead_code)]
#[doc(hidden)]
enum Command {
    ReadLocalTemperature = 0x00,
    ReadRemoteTemperature = 0x01,
    ReadStatusByte = 0x02,
    ReadConfigurationByte = 0x03,
    ReadLocalHighLimit = 0x05,
    ReadRemoteHighLimit = 0x07,
    WriteConfigurationByte = 0x09,
    WriteLocalHighLimit = 0x0b,
    WriteRemoteHighLimit = 0x0d,
    SingleShot = 0x0f,
    ReadRemoteExtendedTemperature = 0x10,
    ReadInternalExtendedTemperature = 0x11,
    ReadManufacturerId = 0xfe,
}

impl Command {
    /// Check if a command should be followed by writable data.
    fn is_writable(&self) -> bool {
        match self {
            Command::WriteConfigurationByte
            | Command::WriteLocalHighLimit
            | Command::WriteRemoteHighLimit
            | Command::SingleShot => true,
            _ => false,
        }
    }
}

/// Represents possible errors from the temperature sensor.
#[derive(Debug)]
pub enum Error<E> {
    Interface(E),
    DiodeFault,
    InvalidCommand,
}

impl<E> From<E> for Error<E> {
    fn from(err: E) -> Error<E> {
        Error::Interface(err)
    }
}

/// The temperature sensor driver.
pub struct Max6642<I2C> {
    i2c: I2C,
    address: u8,
}

impl<I2C> Max6642<I2C>
where
    I2C: I2c,
    <I2C as ErrorType>::Error: Into<<I2C as ErrorType>::Error>,
{
    /// Construct a new driver for the MAX6642-ATT94 variant.
    ///
    /// # Args
    /// * `i2c` - The I2C driver to use to communicate with the device.
    pub fn att94(i2c: I2C) -> Self {
        Max6642::new(i2c, 0x4A)
    }

    /// Construct a new driver for the MAX6642 temperature sensor.
    ///
    /// # Args
    /// * `i2c` - The I2C driver to use to communicate with the device.
    /// * `address` - The I2C address of the device.
    pub fn new(i2c: I2C, address: u8) -> Self {
        Max6642 { i2c, address }
    }

    fn read(&mut self, command: Command) -> Result<u8, Error<<I2C as ErrorType>::Error>> {
        let mut result: [u8; 1] = [0; 1];
        self.i2c
            .write_read(self.address, &[command as u8], &mut result)?;

        Ok(result[0])
    }

    #[allow(dead_code)]
    fn write(
        &mut self,
        command: Command,
        value: u8,
    ) -> Result<(), Error<<I2C as ErrorType>::Error>> {
        if !command.is_writable() {
            return Err(Error::InvalidCommand);
        }

        self.i2c.write(self.address, &[command as u8, value])?;

        Ok(())
    }

    /// Get the temperature of the remote diode.
    ///
    /// # Returns
    /// The temperature of the remote diode in degrees celsius.
    pub fn get_remote_temperature(&mut self) -> Result<f32, Error<<I2C as ErrorType>::Error>> {
        let temp_c = self.read(Command::ReadRemoteTemperature)?;
        if temp_c > 130 {
            return Err(Error::DiodeFault);
        }

        // 0.25C temperature is stored in the top 2 bits of the extended data register.
        let temp_c_4ths = self
            .read(Command::ReadRemoteExtendedTemperature)?
            .get_bits(6..8);

        let temp_c = (temp_c as f32) + (temp_c_4ths as f32) * 0.25;

        Ok(temp_c)
    }
}