Skip to main content

Ads1115

Struct Ads1115 

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

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 - the address the ADDR pin selects, from address.
  • 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.

Source

pub fn with_input(self, mux: Mux) -> Self

Selects the input the part converts.

§Arguments
  • mux - the differential pair or the single-ended input.
§Returns

The driver.

Source

pub fn with_gain(self, pga: Pga) -> Self

Selects the full-scale range, and with it the size of one count.

§Arguments
  • pga - the range; the input must stay within the supply regardless.
§Returns

The driver.

Source

pub fn with_data_rate(self, rate: DataRate) -> Self

Selects the data rate, and with it the conversion time and the noise.

§Arguments
  • rate - samples per second.
§Returns

The driver.

Source

pub fn config(&self) -> Config

Returns the configuration the driver writes to the part.

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

Source

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.

Source

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.

Source

pub fn sample_input( &mut self, mux: Mux, ) -> Result<Sample, DriverError<I2C::Error>>

Converts one input once without changing the configured input.

§Arguments
  • mux - the input to convert this once.
§Returns

The sample.

§Errors

The errors of sample.

Trait Implementations§

Source§

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

Source§

type Reading = Sample

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

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

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

§

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

§

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

§

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

§

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

§

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

§

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