pub struct Sht3x<I2C, D> { /* private fields */ }Expand description
An SHT3x on an I2C bus, measuring on demand in single-shot mode.
Sht3x::new wraps the bus. init soft-resets the part, waits the
datasheet’s reset time, and reads the status register: the part has no id
register, so a status word whose CRC checks is what confirms an SHT3x answers at
the address. measure issues one single-shot command without
clock stretching, waits the datasheet’s maximum measurement duration for the
repeatability in use, reads the six data bytes, and returns the CRC-checked
Measurement. Every command is followed by the wait the datasheet attaches to
it (the reset time, the measurement duration, or the 1 ms between commands), so
consecutive calls keep the spacing the part needs. As a [Sensor] its reading is
the whole measurement; one channel is selected with [Sensor::map].
§Examples
A proofing cabinet’s climate, read from an SHT3x that is not plugged in yet: the
simulated part, which the alloc feature brings, holds 27 °C and 75 % relative
humidity.
use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::sht3x::{sim, Sht3x, I2C_ADDRESS_A};
let part = sim::reporting(I2C_ADDRESS_A, 27.0, 75.0);
let mut sensor = Sht3x::new(part, I2C_ADDRESS_A, DelayLog::new());
let climate = block_on(sensor.read())?;
assert!((climate.temperature_milli_celsius() - 27_000).abs() < 10);
assert!(climate.humidity_milli_percent().abs_diff(75_000) < 10);Implementations§
Source§impl<I2C, D> Sht3x<I2C, D>
impl<I2C, D> Sht3x<I2C, D>
Sourcepub fn new(bus: I2C, address: u8, delay: D) -> Self
pub fn new(bus: I2C, address: u8, delay: D) -> Self
Wraps an I2C bus and the part’s address.
§Arguments
bus- the I2C bus the part is on.address-I2C_ADDRESS_Awith ADDR low,I2C_ADDRESS_Bwith ADDR high.delay- the timer that paces the reset, the measurements, and the gaps between commands.
§Returns
The driver, measuring at high repeatability.
Sourcepub fn with_repeatability(self, repeatability: Repeatability) -> Self
pub fn with_repeatability(self, repeatability: Repeatability) -> Self
Source§impl<I2C: I2c, D: DelayNs> Sht3x<I2C, D>
impl<I2C: I2c, D: DelayNs> Sht3x<I2C, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
Resets the part and reads its status register.
The sequence is the datasheet’s: the soft-reset command of Table 14, the soft-reset time of Table 4, then the status read of Table 17. The part has no id register, so a status word whose CRC checks is the confirmation that an SHT3x answers at the address. The reset also switches the heater off.
§Errors
Returns DriverError::Bus if the bus fails and DriverError::Sensor with
SensorError::Crc if the status word does not
check, so whatever answered is not a working SHT3x.
Sourcepub fn measure(&mut self) -> Result<Measurement, DriverError<I2C::Error>>
pub fn measure(&mut self) -> Result<Measurement, DriverError<I2C::Error>>
Runs one single-shot measurement and returns the CRC-checked result.
Initializes the part first if init has not run. The sequence
is sections 4.2 to 4.4 of the datasheet: the Table 9 command for the
repeatability in use with clock stretching disabled, a wait of Table 4’s
maximum measurement duration, then one read of the temperature word, its CRC,
the humidity word, and its CRC. Table 4 holds from 2.4 V up; below that
(Table 5) the part can take 0.5 ms longer and answers a read that comes early
with a NACK, which the bus reports as its error.
§Returns
The measurement.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Sensor with
SensorError::Crc if either data word does not
check, or any error of init.
Sourcepub fn read_status(&mut self) -> Result<Status, DriverError<I2C::Error>>
pub fn read_status(&mut self) -> Result<Status, DriverError<I2C::Error>>
Reads the status register.
The command of Table 17, the three bytes that answer it (the register most significant byte first, then its CRC), and the 1 ms the part needs before its next command.
§Returns
The decoded status, which status keeps as well.
§Errors
Returns DriverError::Bus if the bus fails and DriverError::Sensor with
SensorError::Crc if the word does not check.
Sourcepub fn heater_on(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn heater_on(&mut self) -> Result<(), DriverError<I2C::Error>>
Switches the plausibility-check heater on.
Initializes the part first if init has not run, since the
reset in it would switch the heater off again. Sends the enable command of
Table 16 and waits the 1 ms the part needs before its next command. The
heater’s state shows in Status::heater_on on the next
read_status.
§Errors
Returns DriverError::Bus if the bus fails, or any error of
init.
Sourcepub fn heater_off(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn heater_off(&mut self) -> Result<(), DriverError<I2C::Error>>
Switches the heater off, which is its state after any reset.
Initializes the part first if init has not run, then sends
the disable command of Table 16 and waits the 1 ms the part needs before its
next command.
§Errors
Returns DriverError::Bus if the bus fails, or any error of
init.