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>
impl<I2C, D> Scd4x<I2C, D>
Source§impl<I2C: I2c, D: DelayNs> Scd4x<I2C, D>
impl<I2C: I2c, D: DelayNs> Scd4x<I2C, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
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.
Sourcepub fn measure(&mut self) -> Result<Measurement, DriverError<I2C::Error>>
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.
Sourcepub fn measure_single_shot(
&mut self,
) -> Result<Measurement, DriverError<I2C::Error>>
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.
Sourcepub fn data_ready(&mut self) -> Result<bool, DriverError<I2C::Error>>
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.
Sourcepub fn stop(&mut self) -> Result<(), DriverError<I2C::Error>>
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.
Sourcepub fn start(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn start(&mut self) -> Result<(), DriverError<I2C::Error>>
Sourcepub fn set_temperature_offset(
&mut self,
milli_celsius: u32,
) -> Result<(), DriverError<I2C::Error>>
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.
Sourcepub fn set_sensor_altitude(
&mut self,
meters: u16,
) -> Result<(), DriverError<I2C::Error>>
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.