Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gate low power commands behind scd41 feature #7

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Scd4x::new(dev, Delay);

#[cfg(feature = "scd41")]
sensor.wake_up();
sensor.stop_periodic_measurement().unwrap();
sensor.reinit().unwrap();
Expand Down
6 changes: 6 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ pub enum Command {
#[cfg(feature = "scd41")]
MeasureSingleShotNonBlocking,
/// On-demand measurement of CO2 concentration, relative humidity and temperature.
#[cfg(feature = "scd41")]
MeasureSingleShotRhtOnly,
/// Put the sensor from idle to sleep mode to reduce current consumption.
#[cfg(feature = "scd41")]
PowerDown,
/// Wake up sensor from sleep mode to idle mode.
#[cfg(feature = "scd41")]
WakeUp,
}

Expand Down Expand Up @@ -84,8 +87,11 @@ impl Command {
Self::MeasureSingleShot => (0x219D, 5000, false),
#[cfg(feature = "scd41")]
Self::MeasureSingleShotNonBlocking => (0x219D, 1, false),
#[cfg(feature = "scd41")]
Self::MeasureSingleShotRhtOnly => (0x2196, 50, false),
#[cfg(feature = "scd41")]
Self::PowerDown => (0x36E0, 1, false),
#[cfg(feature = "scd41")]
Self::WakeUp => (0x36F6, 20, false),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn main() -> Result<(), Error<LinuxI2CError>> {

debug!("Initalising sensor");

#[cfg(feature = "scd41")]
sensor.wake_up();
sensor.stop_periodic_measurement()?;
sensor.reinit()?;
Expand Down
53 changes: 28 additions & 25 deletions src/scd4x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ where
Ok(())
}

/// On-demand measurement of CO₂ concentration, relative humidity and temperature.
/// On-demand measurement of CO₂ concentration, relative humidity and temperature.
/// The sensor output is read with the measurement method.
/// Takes around 5 seconds to complete
#[cfg(feature = "scd41")]
pub fn measure_single_shot(&mut self) -> Result<(), Error<E>>{
pub fn measure_single_shot(&mut self) -> Result<(), Error<E>> {
self.write_command(Command::MeasureSingleShot)?;
Ok(())
}
Expand All @@ -222,18 +222,21 @@ where
}

/// On-demand measurement of relative humidity and temperature only.
pub fn measure_single_shot_rht(&mut self) ->Result<(), Error<E>> {
#[cfg(feature = "scd41")]
pub fn measure_single_shot_rht(&mut self) -> Result<(), Error<E>> {
self.write_command(Command::MeasureSingleShotRhtOnly)?;
Ok(())
}

/// Put the sensor from idle to sleep mode to reduce current consumption.
pub fn power_down(&mut self) -> Result<(), Error<E>>{
#[cfg(feature = "scd41")]
pub fn power_down(&mut self) -> Result<(), Error<E>> {
self.write_command(Command::PowerDown)?;
Ok(())
}

/// Wake up sensor from sleep mode to idle mode.
#[cfg(feature = "scd41")]
pub fn wake_up(&mut self) {
// Sensor does not acknowledge the wake-up call, error is ignored
self.write_command(Command::WakeUp).ok();
Expand Down Expand Up @@ -316,25 +319,25 @@ mod tests {
assert_eq!(serial, 0xbeefbeefbeef);
}

/// Test the measurement function
#[test]
fn test_measurement() {
// Arrange
let (cmd, _, _) = Command::ReadMeasurement.as_tuple();
let expectations = [
Transaction::write(SCD4X_I2C_ADDRESS, cmd.to_be_bytes().to_vec()),
Transaction::read(
SCD4X_I2C_ADDRESS,
vec![0x03, 0xE8, 0xD4, 0x62, 0x03, 0x5E, 0x80, 0x00, 0xA2],
),
];
let mock = I2cMock::new(&expectations);
let mut sensor = Scd4x::new(mock, DelayMock);
// Act
let data = sensor.measurement().unwrap();
// Assert
assert_eq!(data.co2, 1000_u16);
assert_eq!(data.temperature, 22.000198_f32);
assert_eq!(data.humidity, 50_f32);
}
/// Test the measurement function
#[test]
fn test_measurement() {
// Arrange
let (cmd, _, _) = Command::ReadMeasurement.as_tuple();
let expectations = [
Transaction::write(SCD4X_I2C_ADDRESS, cmd.to_be_bytes().to_vec()),
Transaction::read(
SCD4X_I2C_ADDRESS,
vec![0x03, 0xE8, 0xD4, 0x62, 0x03, 0x5E, 0x80, 0x00, 0xA2],
),
];
let mock = I2cMock::new(&expectations);
let mut sensor = Scd4x::new(mock, DelayMock);
// Act
let data = sensor.measurement().unwrap();
// Assert
assert_eq!(data.co2, 1000_u16);
assert_eq!(data.temperature, 22.000198_f32);
assert_eq!(data.humidity, 50_f32);
}
}