tof_control/device/
tmp112.rs

1#![allow(unused)]
2use i2cdev::core::*;
3use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
4
5// Register
6const TEMP: u16 = 0x00; // Temperature Register
7const CONFIG: u16 = 0x01; // Configuration Register
8const T_LOW: u16 = 0x02; // T Low Register
9const T_HIGI: u16 = 0x03; // T High Register
10                          // Configuration Register
11const CONFIG_CR_025: u16 = 0x0000; // Continuos-Conversion Rate 0.25Hz
12const CONFIG_CR_1: u16 = 0x0040; // Continuos-Conversion Rate 1Hz
13const CONFIG_CR_4: u16 = 0x0080; // Continuos-Conversion Rate 4Hz, Default
14const CONFIG_CR_8: u16 = 0x00C0; // Continuos-Conversion Rate 8Hz
15const CONFIG_NM: u16 = 0x0000; // Normal Mode (12 bit), Default
16const CONFIG_EM: u16 = 0x0010; // Extended Mode (13 bit)
17const CONFIG_OS: u16 = 0x8000; // One-Shot/Conversion Ready Mode
18const CONFIG_TM_CM: u16 = 0x0000; // Thermostat Mode, Comparator Mode
19const CONFIG_TM_IM: u16 = 0x0200; // Thermostat Mode, Interrupt Mode
20const CONFIG_SD_CC: u16 = 0x0000; // Continuous Conversion Mode
21const CONFIG_SD_SM: u16 = 0x0100; // Shutdown Mode
22const CONFIG_POL: u16 = 0x0400; // Polarity
23const CONFIG_F_1: u16 = 0x0000; // Fault Queue, 1 Consecutive Fault
24const CONFIG_F_2: u16 = 0x0800; // Fault Queue, 2 Consecutive Faults
25const CONFIG_F_4: u16 = 0x1000; // Fault Queue, 4 Consecutive Faults
26const CONFIG_F_6: u16 = 0x1800; // Fault Queue, 6 Consecutive Faults
27                                // High Limit Register
28const T_HIGH_TEMP: u16 = 0x4B0; // 65°C for High Limit
29                                // Low Limit Register
30const T_LOW_TEMP: u16 = 0x3C0; // 60°C for Low Limit
31
32pub struct TMP112 {
33    bus: u8,
34    address: u16,
35}
36
37impl TMP112 {
38    pub fn new(bus: u8, address: u16) -> Self {
39        Self { bus, address }
40    }
41    pub fn config(&self) -> Result<(), LinuxI2CError> {
42        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
43        let config_reg = CONFIG_F_1 | CONFIG_TM_CM | CONFIG_SD_CC | CONFIG_CR_4;
44
45        dev.smbus_write_i2c_block_data(CONFIG as u8, &config_reg.to_be_bytes())
46    }
47    pub fn read(&self) -> Result<f32, LinuxI2CError> {
48        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
49        let temp_raw = dev.smbus_read_i2c_block_data(TEMP as u8, 2)?;
50        let temp_adc = (((temp_raw[0] as u16) << 4) | ((temp_raw[1] as u16) >> 4)) & 0xFFF;
51
52        Ok(self.adc_to_celsius(temp_adc))
53    }
54    pub fn read_raw(&self) -> Result<u16, LinuxI2CError> {
55        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
56        let temp_raw = dev.smbus_read_i2c_block_data(TEMP as u8, 2)?;
57        let temp_adc = (((temp_raw[0] as u16) << 4) | ((temp_raw[1] as u16) >> 4)) & 0xFFF;
58
59        Ok(temp_adc)
60    }
61    fn adc_to_celsius(&self, mut adc: u16) -> f32 {
62        let mut sign: f32 = 1.0;
63        if adc >= 0x800 {
64            sign = -1.0;
65            adc = 0xFFF - adc;
66        }
67
68        sign * adc as f32 * 0.0625
69    }
70}