From d24bcb9bc2d57843e68171efe3b2f7e35195bb2b Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 29 Apr 2024 01:11:23 -0700 Subject: [PATCH] some interrupt configs can be read back, interrupt config fixes --- src/sensor.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++----- src/types.rs | 16 ++++++++++++++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/sensor.rs b/src/sensor.rs index db60b06..271a48a 100644 --- a/src/sensor.rs +++ b/src/sensor.rs @@ -383,7 +383,7 @@ where Ok((reg & 0b1000_0000) != 0) } - /// Configure INT1 pin. + /// Write INT1 pin configuration /// /// # Arguments /// @@ -394,7 +394,7 @@ where /// - `Ok(())`: Write success /// - `Err(Error)`: Write failure /// - pub fn int1_io_conf(&mut self, conf: IntConfiguration) -> Result<(), Error> { + pub fn int1_io_conf_write(&mut self, conf: IntConfiguration) -> Result<(), Error> { let reg = AccRegisters::INT1_IO_CONF as u8; let set = conf.into(); let mut data = [reg, set]; @@ -402,6 +402,19 @@ where Ok(()) } + /// Read configuration of INT1 pin. + /// + /// # Returns + /// + /// - `Ok(IntConfiguration)`: Interrupt configuration + /// - `Err(Error)`: Write failure + /// + pub fn int1_io_conf_read(&mut self) -> Result> { + let reg = AccRegisters::INT1_IO_CONF as u8; + let data = self.iface.read_register_acc(reg)?; + Ok(IntConfiguration::from(data)) + } + /// Configure INT2 pin. /// /// # Arguments @@ -413,7 +426,7 @@ where /// - `Ok(())`: Write success /// - `Err(Error)`: Write failure /// - pub fn int2_io_conf(&mut self, conf: IntConfiguration) -> Result<(), Error> { + pub fn int2_io_conf_write(&mut self, conf: IntConfiguration) -> Result<(), Error> { let reg = AccRegisters::INT2_IO_CONF as u8; let set = conf.into(); let mut data = [reg, set]; @@ -421,7 +434,20 @@ where Ok(()) } - /// Map data ready interrupt to output pin INT1 and/or INT2. (0x58) + /// Read configuration of INT2 pin. + /// + /// # Returns + /// + /// - `Ok(IntConfiguration)`: Interrupt configuration + /// - `Err(Error)`: Write failure + /// + pub fn int2_io_conf_read(&mut self) -> Result> { + let reg = AccRegisters::INT2_IO_CONF as u8; + let data = self.iface.read_register_acc(reg)?; + Ok(IntConfiguration::from(data)) + } + + /// Write Map data ready interrupt to output pin INT1 and/or INT2. (0x58) /// /// # Arguments /// @@ -432,7 +458,7 @@ where /// - `Ok(())`: Write success /// - `Err(Error)`: Write failure /// - pub fn acc_map_drdy(&mut self, map: AccDrdyMap) -> Result<(), Error> { + pub fn acc_map_drdy_write(&mut self, map: AccDrdyMap) -> Result<(), Error> { let reg = AccRegisters::INT1_INT2_MAP_DATA as u8; let set = map as u8; let mut data = [reg, set]; @@ -440,6 +466,19 @@ where Ok(()) } + /// Read Map data ready interrupt to output pin INT1 and/or INT2. (0x58) + /// + /// # Returns + /// + /// - `Ok(AccDrdyMap)`: The interrupt configuration + /// - `Err(Error)`: Read failure + /// + pub fn acc_map_drdy_read(&mut self) -> Result> { + let reg = AccRegisters::INT1_INT2_MAP_DATA as u8; + let data = self.iface.read_register_acc(reg)?; + Ok(AccDrdyMap::from(data)) + } + /* GYRO REGISTERS */ /// Read the gyro chip ID (0x00) diff --git a/src/types.rs b/src/types.rs index 440ecba..95b77ec 100644 --- a/src/types.rs +++ b/src/types.rs @@ -271,7 +271,7 @@ impl From<&IntConfiguration> for u8 { let mut value = 0; match item.int_pin { IntPin::Input => value |= 0b0001_0000, - IntPin::Output => value |= 0b0000_0000, + IntPin::Output => value |= 0b0000_1000, } match item.int_od { PinBehavior::PushPull => value |= 0b0000_0000, @@ -290,7 +290,7 @@ impl From for u8 { let mut value = 0; match val.int_pin { IntPin::Input => value |= 0b0001_0000, - IntPin::Output => value |= 0b0000_0000, + IntPin::Output => value |= 0b0000_1000, } match val.int_od { PinBehavior::PushPull => value |= 0b0000_0000, @@ -317,6 +317,18 @@ pub enum AccDrdyMap { Int1Int2 = 0b0100_0100, } +impl From for AccDrdyMap { + fn from(item: u8) -> Self { + match item { + 0b0000_0000 => AccDrdyMap::None, + 0b0000_0100 => AccDrdyMap::Int1, + 0b0100_0000 => AccDrdyMap::Int2, + 0b0100_0100 => AccDrdyMap::Int1Int2, + _ => AccDrdyMap::None, + } + } +} + /// Gyroscope range configuration #[derive(Debug, Clone, Copy, PartialEq)] pub enum GyroRange {