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>
impl<I2C, D> Ina219<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-BASE_ADDRESSplus 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.
Sourcepub fn with_shunt(self, milliohms: u32, max_microamps: u32) -> Self
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.
Sourcepub fn with_current_lsb(self, microamps: u32) -> Self
pub fn with_current_lsb(self, microamps: u32) -> Self
Sourcepub fn with_configuration(self, configuration: Configuration) -> Self
pub fn with_configuration(self, configuration: Configuration) -> Self
Sourcepub fn current_lsb_microamps(&self) -> u32
pub fn current_lsb_microamps(&self) -> u32
Returns the current resolution in microamps per count.
Sourcepub fn calibration(&self) -> u16
pub fn calibration(&self) -> u16
Returns the calibration word the driver programs.
Source§impl<I2C: I2c, D: DelayNs> Ina219<I2C, D>
impl<I2C: I2c, D: DelayNs> Ina219<I2C, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
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.
Sourcepub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>
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.