Skip to content

Commit

Permalink
some interrupt configs can be read back, interrupt config fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rland93 committed Apr 29, 2024
1 parent 3747a8f commit d24bcb9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
49 changes: 44 additions & 5 deletions src/sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ where
Ok((reg & 0b1000_0000) != 0)
}

/// Configure INT1 pin.
/// Write INT1 pin configuration
///
/// # Arguments
///
Expand All @@ -394,14 +394,27 @@ where
/// - `Ok(())`: Write success
/// - `Err(Error<CommE>)`: Write failure
///
pub fn int1_io_conf(&mut self, conf: IntConfiguration) -> Result<(), Error<CommE>> {
pub fn int1_io_conf_write(&mut self, conf: IntConfiguration) -> Result<(), Error<CommE>> {
let reg = AccRegisters::INT1_IO_CONF as u8;
let set = conf.into();
let mut data = [reg, set];
self.iface.write_data_acc(&mut data)?;
Ok(())
}

/// Read configuration of INT1 pin.
///
/// # Returns
///
/// - `Ok(IntConfiguration)`: Interrupt configuration
/// - `Err(Error<CommE>)`: Write failure
///
pub fn int1_io_conf_read(&mut self) -> Result<IntConfiguration, Error<CommE>> {
let reg = AccRegisters::INT1_IO_CONF as u8;
let data = self.iface.read_register_acc(reg)?;
Ok(IntConfiguration::from(data))
}

/// Configure INT2 pin.
///
/// # Arguments
Expand All @@ -413,15 +426,28 @@ where
/// - `Ok(())`: Write success
/// - `Err(Error<CommE>)`: Write failure
///
pub fn int2_io_conf(&mut self, conf: IntConfiguration) -> Result<(), Error<CommE>> {
pub fn int2_io_conf_write(&mut self, conf: IntConfiguration) -> Result<(), Error<CommE>> {
let reg = AccRegisters::INT2_IO_CONF as u8;
let set = conf.into();
let mut data = [reg, set];
self.iface.write_data_acc(&mut data)?;
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<CommE>)`: Write failure
///
pub fn int2_io_conf_read(&mut self) -> Result<IntConfiguration, Error<CommE>> {
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
///
Expand All @@ -432,14 +458,27 @@ where
/// - `Ok(())`: Write success
/// - `Err(Error<CommE>)`: Write failure
///
pub fn acc_map_drdy(&mut self, map: AccDrdyMap) -> Result<(), Error<CommE>> {
pub fn acc_map_drdy_write(&mut self, map: AccDrdyMap) -> Result<(), Error<CommE>> {
let reg = AccRegisters::INT1_INT2_MAP_DATA as u8;
let set = map as u8;
let mut data = [reg, set];
self.iface.write_data_acc(&mut data)?;
Ok(())
}

/// Read Map data ready interrupt to output pin INT1 and/or INT2. (0x58)
///
/// # Returns
///
/// - `Ok(AccDrdyMap)`: The interrupt configuration
/// - `Err(Error<CommE>)`: Read failure
///
pub fn acc_map_drdy_read(&mut self) -> Result<AccDrdyMap, Error<CommE>> {
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)
Expand Down
16 changes: 14 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -290,7 +290,7 @@ impl From<IntConfiguration> 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,
Expand All @@ -317,6 +317,18 @@ pub enum AccDrdyMap {
Int1Int2 = 0b0100_0100,
}

impl From<u8> 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 {
Expand Down

0 comments on commit d24bcb9

Please sign in to comment.