Skip to main content

Scd4x

Struct Scd4x 

Source
pub struct Scd4x<I2C, D> { /* private fields */ }
Expand description

An SCD40 or SCD41 on an I2C bus, running periodic measurements.

init stops any periodic measurement a previous run left going, reads the serial number, which is the identity a part without an id register offers, and starts periodic measurement, after which the part produces a new result every five seconds. measure polls the data-ready status until a result waits, then reads it. Every command follows the datasheet’s sequence rules: the two command bytes go out most significant first, the command’s execution time passes before the read header is sent, and the CRC on every word read is checked. As a [Sensor] its reading is the Measurement; one channel is selected with [Sensor::map].

§Examples

A meeting room’s air, read from an SCD4x that is not plugged in yet: the simulated part, which the alloc feature brings, holds 1,200 ppm of carbon dioxide at 22 °C and 40 % relative humidity.

use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::scd4x::{sim, Scd4x};

let mut sensor = Scd4x::new(sim::reporting(1_200, 22.0, 40.0), DelayLog::new());
let air = block_on(sensor.read())?;
assert_eq!(air.co2_ppm, 1_200);

Implementations§

Source§

impl<I2C, D> Scd4x<I2C, D>

Source

pub fn new(bus: I2C, delay: D) -> Self

Wraps an I2C bus; the part has one address.

§Arguments
  • bus - the I2C bus the part is on.
  • delay - the timer that paces the commands and the polls.
§Returns

The driver.

Source

pub fn serial(&self) -> Option<u64>

Returns the serial number read at initialization, if it has happened.

Source

pub fn release(self) -> (I2C, D)

Gives back the bus and the delay.

§Returns

The bus and delay the driver was built from.

Source§

impl<I2C: I2c, D: DelayNs> Scd4x<I2C, D>

Source

pub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>

Stops any running measurement, reads the serial number, and starts periodic measurement.

§Errors

Returns DriverError::Bus if the bus fails, and DriverError::Sensor with SensorError::Crc if the serial number arrives corrupted.

Source

pub fn measure(&mut self) -> Result<Measurement, DriverError<I2C::Error>>

Waits for the next periodic result and reads it.

Initializes the part first if init has not run. The data-ready status is polled every DATA_READY_POLL_MILLIS up to DATA_READY_POLLS times, a little over one signal update interval.

§Returns

The CO2, temperature, and humidity words as a Measurement.

§Errors

Returns DriverError::Bus if the bus fails, DriverError::Timeout if no result becomes ready, DriverError::Sensor with SensorError::Crc if a word arrives corrupted, or any error of init.

Source

pub fn measure_single_shot( &mut self, ) -> Result<Measurement, DriverError<I2C::Error>>

Runs one on-demand measurement on an SCD41, which takes five seconds.

The part must not be in periodic measurement: call stop first, or use this instead of init on a bus where the part is idle.

§Returns

The measurement.

§Errors

Returns DriverError::Bus if the bus fails, and DriverError::Sensor with SensorError::Crc if a word arrives corrupted.

Source

pub fn data_ready(&mut self) -> Result<bool, DriverError<I2C::Error>>

Asks whether a periodic result is waiting.

§Returns

true when measure would read without waiting.

§Errors

Returns DriverError::Bus if the bus fails, and DriverError::Sensor with SensorError::Crc if the status word arrives corrupted.

Source

pub fn stop(&mut self) -> Result<(), DriverError<I2C::Error>>

Stops periodic measurement, after which the settings commands are allowed.

§Errors

Returns DriverError::Bus if the bus fails.

Source

pub fn start(&mut self) -> Result<(), DriverError<I2C::Error>>

Starts periodic measurement.

§Errors

Returns DriverError::Bus if the bus fails.

Source

pub fn set_temperature_offset( &mut self, milli_celsius: u32, ) -> Result<(), DriverError<I2C::Error>>

Sets the temperature offset that compensates the part’s self-heating.

Periodic measurement is stopped, the offset written, and measurement started again, since the part only takes settings while idle. The setting lasts until power is lost unless persisted by the part’s own command.

§Arguments
  • milli_celsius - the offset to subtract, in millidegrees Celsius.
§Errors

Returns DriverError::Bus if the bus fails.

Source

pub fn set_sensor_altitude( &mut self, meters: u16, ) -> Result<(), DriverError<I2C::Error>>

Sets the altitude the part corrects its CO2 reading for.

Periodic measurement is stopped, the altitude written, and measurement started again.

§Arguments
  • meters - the altitude above sea level.
§Errors

Returns DriverError::Bus if the bus fails.

Trait Implementations§

Source§

impl<I2C: Debug, D: Debug> Debug for Scd4x<I2C, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I2C, D> Sensor for Scd4x<I2C, D>
where I2C: I2c + Send, I2C::Error: Debug, D: DelayNs + Send,

Source§

type Reading = Measurement

The value produced by a single read, for example a temperature or a fix.
Source§

async fn read(&mut self) -> Result<Measurement>

Takes a single reading from the sensor. Read more
§

fn map<F, T>(self, select: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Reading) -> T,

Wraps this sensor so every reading passes through select first. Read more

Auto Trait Implementations§

§

impl<I2C, D> Freeze for Scd4x<I2C, D>
where I2C: Freeze, D: Freeze,

§

impl<I2C, D> RefUnwindSafe for Scd4x<I2C, D>

§

impl<I2C, D> Send for Scd4x<I2C, D>
where I2C: Send, D: Send,

§

impl<I2C, D> Sync for Scd4x<I2C, D>
where I2C: Sync, D: Sync,

§

impl<I2C, D> Unpin for Scd4x<I2C, D>
where I2C: Unpin, D: Unpin,

§

impl<I2C, D> UnsafeUnpin for Scd4x<I2C, D>
where I2C: UnsafeUnpin, D: UnsafeUnpin,

§

impl<I2C, D> UnwindSafe for Scd4x<I2C, D>
where I2C: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.