Skip to content

Commit

Permalink
Read temperature sensor data overrun
Browse files Browse the repository at this point in the history
  • Loading branch information
dam4rus committed Nov 22, 2021
1 parent be70fee commit 5ae8eff
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This driver allows you to:
- Set accelerometer mode. See: `set_accel_mode()`.
- Set accelerometer scale. See: `set_accel_scale()`.
- Get accelerometer ID. See: `accelerometer_id()`.
- Get temperature sensor status. See: `temp_has_new_data()`.
- Get temperature sensor status. See: `temp_status()`.
- Get temperature sensor data. See: `temp_data()`.
- Get temperature sensor data in celsius. See: `temp_celsius()`.
- Magnetometer:
Expand Down
16 changes: 12 additions & 4 deletions src/device_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
mode,
register_address::{WHO_AM_I_A_VAL, WHO_AM_I_M_VAL},
AccelMode, AccelScale, BitFlags as BF, Config, Error, Lsm303agr, Measurement, PhantomData,
Register, Status, UnscaledMeasurement,
Register, Status, TempStatus, UnscaledMeasurement,
};

impl<I2C> Lsm303agr<I2cInterface<I2C>, mode::MagOneShot> {
Expand Down Expand Up @@ -193,9 +193,10 @@ where
}

/// Temperature sensor status
pub fn temp_has_new_data(&mut self) -> Result<bool, Error<CommE, PinE>> {
let status = self.iface.read_accel_register(Register::STATUS_REG_AUX_A)?;
Ok((status & BF::TDA) != 0)
pub fn temp_status(&mut self) -> Result<TempStatus, Error<CommE, PinE>> {
self.iface
.read_accel_register(Register::STATUS_REG_AUX_A)
.map(convert_temp_status)
}
}

Expand All @@ -211,3 +212,10 @@ fn convert_status(st: u8) -> Status {
x_new_data: (st & BF::XDR) != 0,
}
}

fn convert_temp_status(st: u8) -> TempStatus {
TempStatus {
overrun: (st & BF::TOR) != 0,
new_data: (st & BF::TDA) != 0,
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! - Set accelerometer mode. See: [`set_accel_mode()`](Lsm303agr::set_accel_mode).
//! - Set accelerometer scale. See: [`set_accel_scale()`](Lsm303agr::set_accel_scale).
//! - Get accelerometer ID. See: [`accelerometer_id()`](Lsm303agr::accelerometer_id).
//! - Get temperature sensor status. See: [`temp_has_new_data()`](Lsm303agr::temp_has_new_data).
//! - Get temperature sensor status. See: [`temp_status()`](Lsm303agr::temp_status).
//! - Get temperature sensor data. See: [`temp_data()`](Lsm303agr::temp_data).
//! - Get temperature sensor data in celsius. See: [`temp_celsius()`](Lsm303agr::temp_celsius).
//! - Magnetometer:
Expand Down Expand Up @@ -118,7 +118,7 @@ mod magnetometer;
mod types;
pub use crate::types::{
mode, AccelMode, AccelOutputDataRate, AccelScale, Error, MagOutputDataRate, Measurement,
ModeChangeError, Status, UnscaledMeasurement,
ModeChangeError, Status, TempStatus, UnscaledMeasurement,
};
mod register_address;
use crate::register_address::{BitFlags, Register};
Expand Down
1 change: 1 addition & 0 deletions src/register_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl BitFlags {
pub const XYZOR: u8 = 1 << 7;

pub const TDA: u8 = 1 << 2;
pub const TOR: u8 = 1 << 6;

pub const TEMP_EN0: u8 = 1 << 6;
pub const TEMP_EN1: u8 = 1 << 7;
Expand Down
9 changes: 9 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,12 @@ pub struct Status {
/// Z-axis data new data ready
pub z_new_data: bool,
}

/// Temperature sensor status
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct TempStatus {
/// Data overrun
pub overrun: bool,
/// New data ready
pub new_data: bool,
}
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl BitFlags {
pub const XYZOR: u8 = 1 << 7;

pub const TDA: u8 = 1 << 2;
pub const TOR: u8 = 1 << 6;

pub const TEMP_EN0: u8 = 1 << 6;
pub const TEMP_EN1: u8 = 1 << 7;
Expand Down
16 changes: 14 additions & 2 deletions tests/temp_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ fn can_read_temp_has_new_data() {
vec![BF::TDA],
)]);

assert!(sensor.temp_has_new_data().unwrap());
assert!(sensor.temp_status().unwrap().new_data);
destroy_i2c(sensor);
}

#[test]
fn can_read_temp_has_data_overrun() {
let mut sensor = new_i2c(&[I2cTrans::write_read(
ACCEL_ADDR,
vec![Register::STATUS_REG_AUX_A],
vec![BF::TOR],
)]);

assert!(sensor.temp_status().unwrap().overrun);
destroy_i2c(sensor);
}

Expand All @@ -26,7 +38,7 @@ fn can_read_temp_has_no_new_data() {
vec![0x00],
)]);

assert!(!sensor.temp_has_new_data().unwrap());
assert!(!sensor.temp_status().unwrap().new_data);
destroy_i2c(sensor);
}

Expand Down

0 comments on commit 5ae8eff

Please sign in to comment.