summaryrefslogtreecommitdiff
path: root/tmp117/src/client.rs
blob: 6618d722bca158e5c085b9baa64c80ca552426fe (plain)
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
use crate::conf::Configuration;
use crate::eeprom::EEPROM;
use crate::id::Identity;
use crate::reg::Registers;
use embedded_hal::{delay::DelayNs, i2c::I2c};

pub struct TMP117<I2C, Delay> {
    addr: u8,
    i2c: I2C,
    delay: Delay,
}

impl<I2C: I2c, Delay: DelayNs> TMP117<I2C, Delay> {
    const CELSIUS_PER_LSB: f32 = 7.8125e-3;

    pub fn new(addr: u8, i2c: I2C, delay: Delay) -> Self {
        Self { addr, i2c, delay }
    }

    pub fn configure(&mut self, configuration: u16) -> Result<(), I2C::Error> {
        let bytes = configuration.to_be_bytes();
        self.i2c.write(
            self.addr,
            &[Registers::Configuration as u8, bytes[0], bytes[1]],
        )
    }

    pub fn configure_typed(&mut self, configuration: Configuration) -> Result<(), I2C::Error> {
        let configuration = u16::from(&configuration);
        self.configure(configuration)
    }

    pub fn get_configuration(&mut self) -> Result<u16, I2C::Error> {
        let mut bytes = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::Configuration as u8], &mut bytes)?;
        let configuration = u16::from_be_bytes(bytes);
        Ok(configuration)
    }

    pub fn get_configuration_typed(&mut self) -> Result<Configuration, I2C::Error> {
        let configuration = self.get_configuration()?;
        let configuration = Configuration::from(configuration);
        Ok(configuration)
    }

    pub fn configure_low_limit(&mut self, limit: i16) -> Result<(), I2C::Error> {
        let bytes = limit.to_be_bytes();
        self.i2c
            .write(self.addr, &[Registers::LowLimit as u8, bytes[0], bytes[1]])
    }

    pub fn configure_low_limit_celsius(&mut self, limit: f32) -> Result<(), I2C::Error> {
        let limit = limit / Self::CELSIUS_PER_LSB;
        let limit = limit.round() as i16;
        self.configure_low_limit(limit)
    }

    pub fn get_low_limit(&mut self) -> Result<i16, I2C::Error> {
        let mut limit = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::LowLimit as u8], &mut limit)?;
        let limit = i16::from_be_bytes(limit);
        Ok(limit)
    }

    pub fn get_low_limit_celsius(&mut self) -> Result<f32, I2C::Error> {
        let limit = self.get_low_limit()?;
        let limit = limit as f32 * Self::CELSIUS_PER_LSB;
        Ok(limit)
    }

    pub fn configure_high_limit(&mut self, limit: i16) -> Result<(), I2C::Error> {
        let limit = limit.to_be_bytes();
        self.i2c
            .write(self.addr, &[Registers::HighLimit as u8, limit[0], limit[1]])
    }

    pub fn configure_high_limit_celsius(&mut self, limit: f32) -> Result<(), I2C::Error> {
        let limit = limit / Self::CELSIUS_PER_LSB;
        let limit = limit.round() as i16;
        self.configure_high_limit(limit)
    }

    pub fn get_high_limit(&mut self) -> Result<i16, I2C::Error> {
        let mut limit = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::HighLimit as u8], &mut limit)?;
        let limit = i16::from_be_bytes(limit);
        Ok(limit)
    }

    pub fn get_high_limit_celsius(&mut self) -> Result<f32, I2C::Error> {
        let high_limit = self.get_high_limit()?;
        let high_limit = high_limit as f32 * Self::CELSIUS_PER_LSB;
        Ok(high_limit)
    }

    pub fn configure_offset_temperature(&mut self, offset: i16) -> Result<(), I2C::Error> {
        let offset = offset.to_be_bytes();
        self.i2c.write(
            self.addr,
            &[Registers::TemperatureOffset as u8, offset[0], offset[1]],
        )
    }

    pub fn configure_offset_temperature_celsius(&mut self, offset: f32) -> Result<(), I2C::Error> {
        let offset = offset / Self::CELSIUS_PER_LSB;
        let offset = offset.round() as i16;
        self.configure_offset_temperature(offset)
    }

    pub fn get_offset_temperature(&mut self) -> Result<i16, I2C::Error> {
        let mut offset = [0u8; 2];
        self.i2c.write_read(
            self.addr,
            &[Registers::TemperatureOffset as u8],
            &mut offset,
        )?;
        let offset = i16::from_be_bytes(offset);
        Ok(offset)
    }

    pub fn get_offset_temperature_celsius(&mut self) -> Result<f32, I2C::Error> {
        let offset = self.get_offset_temperature()?;
        let offset = offset as f32 * Self::CELSIUS_PER_LSB;
        Ok(offset)
    }

    pub fn get_eeprom_1(&mut self) -> Result<u16, I2C::Error> {
        let mut eeprom = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::Eeprom1 as u8], &mut eeprom)?;
        let eeprom = u16::from_be_bytes(eeprom);
        Ok(eeprom)
    }

    pub fn get_eeprom_2(&mut self) -> Result<u16, I2C::Error> {
        let mut eeprom = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::Eeprom2 as u8], &mut eeprom)?;
        let eeprom = u16::from_be_bytes(eeprom);
        Ok(eeprom)
    }

    pub fn get_eeprom_3(&mut self) -> Result<u16, I2C::Error> {
        let mut eeprom = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::Eeprom3 as u8], &mut eeprom)?;
        let eeprom = u16::from_be_bytes(eeprom);
        Ok(eeprom)
    }

    pub fn get_eeprom_all(&mut self) -> Result<EEPROM, I2C::Error> {
        let eeprom1 = self.get_eeprom_1()?;
        let eeprom2 = self.get_eeprom_2()?;
        let eeprom3 = self.get_eeprom_3()?;
        let eeprom = EEPROM {
            eeprom1,
            eeprom2,
            eeprom3,
        };
        Ok(eeprom)
    }

    pub fn get_identity(&mut self) -> Result<Identity, I2C::Error> {
        let mut identity = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::DeviceId as u8], &mut identity)?;
        let identity = u16::from_be_bytes(identity);
        let identity = Identity::from(identity);
        Ok(identity)
    }

    pub fn get_temperature(&mut self) -> Result<i16, I2C::Error> {
        let mut temperature = [0u8; 2];
        self.i2c
            .write_read(self.addr, &[Registers::Temperature as u8], &mut temperature)?;
        let temperature = i16::from_be_bytes(temperature);
        Ok(temperature)
    }

    pub fn get_temperature_celsius(&mut self) -> Result<f32, I2C::Error> {
        let temperature = self.get_temperature()?;
        let temperature = temperature as f32 * Self::CELSIUS_PER_LSB;
        Ok(temperature)
    }

    pub fn software_reset(&mut self) -> Result<(), I2C::Error> {
        let mask: u16 = 0x0002;
        let mask = mask.to_be_bytes();
        self.i2c.write(
            self.addr,
            &[Registers::Configuration as u8, mask[0], mask[1]],
        )?;
        self.delay.delay_ms(2);
        Ok(())
    }
}