Skip to main content

Ina219

Struct Ina219 

Source
pub struct Ina219<I2C, D> { /* private fields */ }
Expand description

An INA219 on an I2C bus, measuring shunt and bus voltage, current, and power on demand.

init resets the part, writes the configuration, programs the calibration register for the shunt and the current resolution, and reads the calibration back, which is the identity check a part without an id register offers. measure writes the triggered shunt-and-bus mode, which starts one conversion of each, waits the conversion time for the ADC settings, polls the conversion-ready flag in the Bus Voltage register, then reads the shunt, current, and power registers, power last, because reading it clears the flag. As a [Sensor] its reading is the Reading; one quantity is selected with [Sensor::map].

§Examples

A solar charger’s output, read through an INA219 that is not plugged in yet: 12 V on the bus and 1 A through a 100 mΩ shunt, with the part calibrated for up to 3.2 A.

use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::ina219::{sim, Ina219, BASE_ADDRESS};

let part = sim::reporting(BASE_ADDRESS, 100, 3_200_000, 12_000, 1_000_000);
let mut charger = Ina219::new(part, BASE_ADDRESS, DelayLog::new()).with_shunt(100, 3_200_000);
let reading = block_on(charger.read())?;
assert_eq!(reading.bus_millivolts(), 12_000);
assert!((reading.current_microamps() - 1_000_000).abs() < 100);

Implementations§

Source§

impl<I2C, D> Ina219<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 - BASE_ADDRESS plus what the A1 and A0 pins add.
  • delay - the timer that paces the conversions.
§Returns

The driver, with the reset configuration and a 100 mΩ shunt sized for 3.2 A, the common breakout.

Source

pub fn with_shunt(self, milliohms: u32, max_microamps: u32) -> Self

Sets the shunt and the largest current expected through it.

The current resolution is the smallest the datasheet allows for that maximum, which gives the Current register its finest scale; the calibration register follows from the two by the datasheet’s equation.

§Arguments
  • milliohms - the shunt resistance.
  • max_microamps - the largest current the shunt will carry.
§Returns

The driver.

Source

pub fn with_current_lsb(self, microamps: u32) -> Self

Sets the current resolution outright, for a round figure such as 100 µA.

§Arguments
  • microamps - microamps per count of the Current register.
§Returns

The driver.

Source

pub fn with_configuration(self, configuration: Configuration) -> Self

Sets the range, gain, and ADC settings; the mode is chosen per conversion.

§Arguments
  • configuration - the settings to write.
§Returns

The driver.

Source

pub fn current_lsb_microamps(&self) -> u32

Returns the current resolution in microamps per count.

Source

pub fn calibration(&self) -> u16

Returns the calibration word the driver programs.

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

Source

pub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>

Resets the part, writes the configuration and calibration, and reads the calibration back.

§Errors

Returns DriverError::Bus if the bus fails, and DriverError::Sensor with SensorError::Identity if the calibration register does not hold what was written.

Source

pub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>

Triggers one shunt and bus conversion and returns every result.

Initializes the part first if init has not run.

§Returns

The four registers with the current resolution they scale by.

§Errors

Returns DriverError::Bus if the bus fails, DriverError::Timeout if the conversion-ready flag never sets, or any error of init.

Trait Implementations§

Source§

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

§

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

§

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

§

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

§

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

§

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

§

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