Skip to main content

Tmp117

Struct Tmp117 

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

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 - one of the address constants, chosen by where the ADD0 pin is tied.
  • delay - the timer that paces the conversions.
§Returns

The driver, averaging 8 conversions into each result, the factory setting.

Source

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

pub fn revision(&self) -> Option<u8>

Returns the silicon revision 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> Tmp117<I2C, D>

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<I2C: Debug, D: Debug> Debug for Tmp117<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 Tmp117<I2C, D>
where I2C: I2c + Send, I2C::Error: Debug, D: DelayNs + Send,

Source§

type Reading = Reading

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

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

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 Tmp117<I2C, D>
where I2C: Freeze, D: Freeze,

§

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

§

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

§

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

§

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

§

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

§

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