pub struct Tmp117<I2C, D> { /* private fields */ }Expand description
A TMP117 on an I2C bus, converting on demand in one-shot mode.
Tmp117::new wraps the bus. init checks the device id, waits
for the EEPROM to finish loading, and writes the configuration with the part in
shutdown and the chosen averaging. measure starts one
conversion, waits the conversion time for that averaging, polls the data-ready
flag, and returns the temperature register as a Reading; the part powers
itself down again once the conversion is done. The alert limits are written with
set_alert_limits and their flags read with
alerts. As a [Sensor] its reading is the Reading; one
unit is selected with [Sensor::map].
§Examples
An aquarium’s water temperature, read through a TMP117 that is not plugged in yet: the simulated part holds 25.5 °C, and the driver waits out one conversion at the default averaging before it reads it.
use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::tmp117::{address, sim, Tmp117};
let part = sim::reporting(address::ADD0_GND, 25.5);
let mut sensor = Tmp117::new(part, address::ADD0_GND, DelayLog::new());
let reading = block_on(sensor.read())?;
assert_eq!(reading.celsius(), 25.5);
let (_, delay) = sensor.release();
assert_eq!(delay.total_micros(), 125_000, "eight averaged conversions");Implementations§
Source§impl<I2C, D> Tmp117<I2C, D>
impl<I2C, D> Tmp117<I2C, D>
Sourcepub fn with_averaging(self, averaging: Averaging) -> Self
pub fn with_averaging(self, averaging: Averaging) -> Self
Sets how many conversions are averaged into each result.
The setting is written to the part by init and with every
conversion; more averaging lengthens the conversion, see
Averaging::conversion_micros.
§Arguments
averaging- the number of conversions per result.
§Returns
The driver.
Source§impl<I2C: I2c, D: DelayNs> Tmp117<I2C, D>
impl<I2C: I2c, D: DelayNs> Tmp117<I2C, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
Checks the part’s identity, waits for its EEPROM to finish loading, and writes the settings with the part in shutdown.
The identity is the device id field of the Device_ID register (section
7.6.11). The EEPROM_Busy flag of the EEPROM unlock register is then polled
until it clears, because the part ignores writes to the configuration and limit
registers while its power-on load runs (section 7.5.1.1). The configuration
register (Table 7-6) is written last, with the shutdown mode and the averaging:
that ends any continuous conversion and leaves the part powered down between
the one-shot conversions measure starts (sections 7.4.2
and 7.4.3).
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Sensor with
SensorError::Identity if the device id is not a TMP117’s, and
DriverError::Timeout if the EEPROM never reports ready.
Sourcepub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>
pub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>
Runs one conversion and returns the temperature.
Initializes the part first if init has not run. The
configuration register is written with the one-shot mode and the averaging,
which starts the conversion (section 7.4.3), and read back once so that a
Data_Ready flag left over from an earlier conversion is cleared rather than
trusted, since only a read clears it (Table 7-6). The wait is the conversion
time for the averaging in use, the no-standby cycle of Table 7-7, after which
the configuration register is polled until Data_Ready is set and the
temperature register is read (section 7.6.2).
§Returns
The temperature register as a Reading.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if the
part never reports data ready, or any error of init.
Sourcepub fn set_alert_limits(
&mut self,
high_celsius: f32,
low_celsius: f32,
) -> Result<(), DriverError<I2C::Error>>
pub fn set_alert_limits( &mut self, high_celsius: f32, low_celsius: f32, ) -> Result<(), DriverError<I2C::Error>>
Writes the high and low limit registers.
The limits share the temperature register’s format, 7.8125 m°C per count over
±256 °C (sections 7.6.4 and 7.6.5); each is rounded to the nearest count and
saturated at the register range. At the end of every conversion the part sets
HIGH_Alert when the result is above the high limit and LOW_Alert when it is
below the low limit (section 7.4.4.1); alerts reads them.
§Arguments
high_celsius- the high limit in degrees Celsius; the factory value is 192 °C.low_celsius- the low limit in degrees Celsius; the factory value is -256 °C.
§Errors
Returns DriverError::Bus if the bus fails.
Sourcepub fn alerts(&mut self) -> Result<Alerts, DriverError<I2C::Error>>
pub fn alerts(&mut self) -> Result<Alerts, DriverError<I2C::Error>>
Reads the HIGH_Alert and LOW_Alert flags: whether a result since the previous call was above the high limit or below the low limit.
The part sets the flags at the end of every conversion and, in alert mode,
clears them whenever the configuration register is read (Table 7-6), the reads
measure makes to see a conversion finish included. The
driver keeps the flags those reads consumed, folds them into this answer, and
forgets them, so the register’s read-to-clear behavior holds across the
driver’s own reads.
§Returns
The two flags.
§Errors
Returns DriverError::Bus if the bus fails.