Skip to content

Commit

Permalink
Readd DynamicOneShot.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 13, 2024
1 parent af9f826 commit b24fe1c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]

- Updated `embedded-hal` to version `1`.
- Removed `DynamicOneShot` for simplicity.

## [0.2.2] - 2021-07-29

Expand Down
24 changes: 24 additions & 0 deletions examples/trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This example demonstrates the use of the `DynamicOneSot` trait to ease the usage
// of the `Ads1x1x` struct in functions.

use linux_embedded_hal::I2cdev;
use nb::block;

use ads1x1x::{Ads1x1x, ChannelSelection, DynamicOneShot, SlaveAddr};

/// Read a single value from channel A.
/// Returns 0 on Error.
pub fn read<E, A: DynamicOneShot<Error = E>>(adc: &mut A) -> i16 {
block!(adc.read(ChannelSelection::SingleA0)).unwrap_or(0)
}

fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut adc = Ads1x1x::new_ads1115(dev, address);

let value = read(&mut adc);
println!("Measurement: {}", value);
// get I2C device back
let _dev = adc.destroy_ads1115();
}
17 changes: 15 additions & 2 deletions src/devices/mode/oneshot.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Common functions
use core::marker::PhantomData;

use crate::{
conversion, devices::OperatingMode, interface, mode, Ads1x1x, BitFlags, ChannelSelection,
Config, Error, ModeChangeError, Register,
Config, DynamicOneShot, Error, ModeChangeError, Register,
};
use core::marker::PhantomData;

impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot>
where
Expand Down Expand Up @@ -77,3 +78,15 @@ where
Err(nb::Error::WouldBlock)
}
}

impl<DI, IC, CONV, E> DynamicOneShot for Ads1x1x<DI, IC, CONV, mode::OneShot>
where
DI: interface::ReadWriteRegister<Error = E>,
CONV: conversion::ConvertMeasurement,
{
type Error = Error<E>;

fn read(&mut self, channel: ChannelSelection) -> nb::Result<i16, Self::Error> {
(*self).read(channel)
}
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ mod types;
use crate::types::Config;
pub use crate::types::{
mode, Ads1x1x, ComparatorLatching, ComparatorMode, ComparatorPolarity, ComparatorQueue,
DataRate12Bit, DataRate16Bit, Error, FullScaleRange, ModeChangeError, SlaveAddr,
DataRate12Bit, DataRate16Bit, DynamicOneShot, Error, FullScaleRange, ModeChangeError,
SlaveAddr,
};

mod private {
Expand Down
11 changes: 11 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use core::marker::PhantomData;

use crate::{private, ChannelSelection};

/// Errors in this crate
#[derive(Debug)]
pub enum Error<E> {
Expand Down Expand Up @@ -263,6 +265,15 @@ pub struct Ads1x1x<DI, IC, CONV, MODE> {
pub(crate) _mode: PhantomData<MODE>,
}

/// Multi channel One-shot ADC
pub trait DynamicOneShot: private::Sealed {
/// Error type
type Error;

/// Read a measurement
fn read(&mut self, channel: ChannelSelection) -> nb::Result<i16, Self::Error>;
}

#[cfg(test)]
mod tests {
use crate::{FullScaleRange, SlaveAddr};
Expand Down

0 comments on commit b24fe1c

Please sign in to comment.