-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.rs
48 lines (45 loc) · 1.63 KB
/
common.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
use crate::{
ic,
register_access::{mlx90614, mlx90615},
Error, Mlx9061x, SlaveAddr,
};
use embedded_hal::blocking::{delay::DelayMs, i2c};
impl<I2C, IC> Mlx9061x<I2C, IC> {
/// Destroy driver instance, return I²C bus.
pub fn destroy(self) -> I2C {
self.i2c
}
}
macro_rules! common {
($ic_marker:ident, $ic_reg:ident) => {
impl<E, I2C> Mlx9061x<I2C, ic::$ic_marker>
where
I2C: i2c::WriteRead<Error = E> + i2c::Write<Error = E>,
{
/// Change the device address
///
/// The address will be stored in the EEPROM.
/// The address will be first cleared, before the new one is written.
/// After each write the configured delay will be waited except the last time.
pub fn set_address<D: DelayMs<u8>>(
&mut self,
address: SlaveAddr,
delay_ms: &mut D,
) -> Result<(), Error<E>> {
let address = Self::get_address(address, $ic_reg::DEV_ADDR)?;
self.write_u16_eeprom($ic_reg::Register::ADDRESS, u16::from(address), delay_ms)?;
self.address = address;
Ok(())
}
/// Enter sleep mode
///
/// After entering sleep, either destroy this driver to get the SDA/SCL pins back
/// and call the `wake()` method or perform a hardware POR to wake the device.
pub fn sleep(&mut self) -> Result<(), Error<E>> {
self.write_u8($ic_reg::SLEEP_COMMAND)
}
}
};
}
common!(Mlx90614, mlx90614);
common!(Mlx90615, mlx90615);