pub struct Ads1115<I2C, D> { /* private fields */ }Expand description
An ADS1115 on an I2C bus, converting one input on demand.
The part is left in its power-down single-shot state between conversions, which
is its reset state. init writes the input, gain, and data
rate to the Config register and reads it back, which is the only identity check
a part without an id register offers. sample sets the
operational-status bit to start a conversion, waits one period of the data rate
plus the datasheet’s ten percent rate variation, polls the status bit until the
part reports idle, and reads the Conversion register. The reading carries the
gain it was taken at, so Sample::volts needs nothing else. As a [Sensor]
its reading is the Sample; [Sensor::map] with a
Calibration
turns it into the physical quantity an analog probe measures.
§Examples
A soil moisture probe’s analog output, read through an ADS1115 that is not plugged in yet: the simulated converter sees 1.2 V on AIN0 in its default ±2.048 V range, and the driver hands the sample back in volts.
use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::ads1115::{address, sim, Ads1115, Mux, Pga};
let part = sim::reporting(address::GND, Pga::Fsr2_048, 1.2);
let adc = Ads1115::new(part, address::GND, DelayLog::new()).with_input(Mux::Ain0Gnd);
let mut volts = adc.map(|sample| sample.volts());
let reading = block_on(volts.read())?;
assert!((reading - 1.2).abs() < 0.001);Implementations§
Source§impl<I2C, D> Ads1115<I2C, D>
impl<I2C, D> Ads1115<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- the address the ADDR pin selects, fromaddress.delay- the timer that paces the conversions.
§Returns
The driver, converting AIN0 against AIN1 at the ±2.048 V range and 128 samples per second, the part’s reset settings.
Sourcepub fn with_input(self, mux: Mux) -> Self
pub fn with_input(self, mux: Mux) -> Self
Sourcepub fn with_data_rate(self, rate: DataRate) -> Self
pub fn with_data_rate(self, rate: DataRate) -> Self
Source§impl<I2C: I2c, D: DelayNs> Ads1115<I2C, D>
impl<I2C: I2c, D: DelayNs> Ads1115<I2C, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
Writes the input, gain, and data rate and reads the Config register back.
The operational-status bit is left clear so no conversion starts. Reading the register back is the identity check: a part that does not hold what was written is not an ADS1115 in its power-down state.
§Errors
Returns DriverError::Bus if the bus fails, and DriverError::Sensor
with SensorError::Identity if the register reads back differently.
Sourcepub fn sample(&mut self) -> Result<Sample, DriverError<I2C::Error>>
pub fn sample(&mut self) -> Result<Sample, DriverError<I2C::Error>>
Runs one conversion of the configured input and returns the sample.
Initializes the part first if init has not run. The
sequence is the datasheet’s single-shot mode: the Config register written
with the operational-status bit set, the conversion time for the data rate,
the status bit polled until the part is idle, then the Conversion register.
§Returns
The sample, carrying the gain it was taken at.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if
the part never reports idle, or any error of init.