Skip to main content

Sht3x

Struct Sht3x 

Source
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>

Source

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_A with ADDR low, I2C_ADDRESS_B with ADDR high.
  • delay - the timer that paces the reset, the measurements, and the gaps between commands.
§Returns

The driver, measuring at high repeatability.

Source

pub fn with_repeatability(self, repeatability: Repeatability) -> Self

Sets the repeatability of each measurement, which trades noise against measurement time and energy.

§Arguments
  • repeatability - the repeatability to measure at.
§Returns

The driver.

Source

pub fn address(&self) -> u8

Returns the part’s 7-bit address.

Source

pub fn status(&self) -> Option<Status>

Returns the status register as it was last read, if it has been.

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> Sht3x<I2C, D>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<I2C: Debug, D: Debug> Debug for Sht3x<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 Sht3x<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 Sht3x<I2C, D>
where I2C: Freeze, D: Freeze,

§

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

§

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

§

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

§

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

§

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

§

impl<I2C, D> UnwindSafe for Sht3x<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.