diff options
author | Markus Reiter <me@reitermark.us> | 2024-06-10 21:56:37 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-10 23:56:37 +0200 |
commit | 00830f4e5569a381afbdfce270e478c5fd25401f (patch) | |
tree | 8940dd071a18d8b1ab93ca25cea89b5ea52f269d | |
parent | 0ff4c7b448fd388015d4903b774c63991356b954 (diff) | |
download | ads1x1x-async-00830f4e5569a381afbdfce270e478c5fd25401f.tar.gz ads1x1x-async-00830f4e5569a381afbdfce270e478c5fd25401f.tar.xz ads1x1x-async-00830f4e5569a381afbdfce270e478c5fd25401f.zip |
Remove `DynamicOneShot` trait. (#19)
* Remove `DynamicOneShot` trait.
* Update changelog.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | examples/trait.rs | 23 | ||||
-rw-r--r-- | src/channel.rs | 24 | ||||
-rw-r--r-- | src/devices/mode/oneshot.rs | 52 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/types.rs | 11 |
6 files changed, 32 insertions, 84 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index cb23a44..785ec7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Removed - Removed `I2cInterface`. - Removed `reset_internal_driver_state` method. +- Removed `DynamicOneShot` trait. ## [0.2.2] - 2021-07-29 diff --git a/examples/trait.rs b/examples/trait.rs deleted file mode 100644 index 3eff5b5..0000000 --- a/examples/trait.rs +++ /dev/null @@ -1,23 +0,0 @@ -// 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 mut adc = Ads1x1x::new_ads1115(dev, SlaveAddr::default()); - - let value = read(&mut adc); - println!("Measurement: {}", value); - // get I2C device back - let _dev = adc.destroy_ads1115(); -} diff --git a/src/channel.rs b/src/channel.rs index 27262d4..4b7555e 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -1,33 +1,31 @@ //! ADC input channels use crate::{ic, Ads1x1x, BitFlags as BF, Config}; -mod private { - pub trait Sealed {} -} +use private::ChannelSelection; /// Marker type for an ADC input channel. -pub trait ChannelId<T>: private::Sealed { +pub trait ChannelId<T> { /// Get the channel. fn channel_id() -> ChannelSelection; } macro_rules! impl_channels { ($(#[doc = $doc:expr] $CH:ident => [$($IC:ident),+]),+ $(,)?) => { - #[derive(Debug, Clone, Copy)] - /// ADC input channel selection. - pub enum ChannelSelection { - $( - #[doc = $doc] - $CH, - )+ + mod private { + #[derive(Debug, Clone, Copy)] + /// ADC input channel selection. + pub enum ChannelSelection { + $( + #[doc = $doc] + $CH, + )+ + } } $( #[doc = $doc] pub struct $CH; - impl private::Sealed for $CH {} - $( impl<I2C, CONV, MODE> ChannelId<Ads1x1x<I2C, ic::$IC, CONV, MODE>> for $CH { fn channel_id() -> ChannelSelection { diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index 957eba5..10cdadb 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -1,7 +1,7 @@ //! Common functions use crate::{ - conversion, devices::OperatingMode, mode, Ads1x1x, BitFlags, ChannelId, ChannelSelection, - Config, DynamicOneShot, Error, ModeChangeError, Register, + conversion, devices::OperatingMode, mode, Ads1x1x, BitFlags, ChannelId, Config, Error, + ModeChangeError, Register, }; use core::marker::PhantomData; @@ -40,14 +40,28 @@ where I2C: embedded_hal::i2c::I2c<Error = E>, CONV: conversion::ConvertMeasurement, { - fn read_inner(&mut self, channel: ChannelSelection) -> nb::Result<i16, Error<E>> { + /// Request that the ADC begin a conversion on the specified channel. + /// + /// The output value will be within `[2047..-2048]` for 12-bit devices + /// (`ADS101x`) and within `[32767..-32768]` for 16-bit devices (`ADS111x`). + /// The voltage that these values correspond to must be calculated using + /// the full-scale range selected. + /// See [`FullScaleRange`](enum.FullScaleRange.html). + /// + /// Returns `nb::Error::WouldBlock` while a measurement is in progress. + /// + /// In case a measurement was requested and after is it is finished a + /// measurement on a different channel is requested, a new measurement on + /// using the new channel selection is triggered. + #[allow(unused_variables)] + pub fn read<CH: ChannelId<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> { if self .is_measurement_in_progress() .map_err(nb::Error::Other)? { return Err(nb::Error::WouldBlock); } - let config = self.config.with_mux_bits(channel); + let config = self.config.with_mux_bits(CH::channel_id()); let same_channel = self.config == config; if self.a_conversion_was_started && same_channel { // result is ready @@ -63,34 +77,4 @@ where self.a_conversion_was_started = true; Err(nb::Error::WouldBlock) } - - /// Request that the ADC begin a conversion on the specified channel. - /// - /// The output value will be within `[2047..-2048]` for 12-bit devices - /// (`ADS101x`) and within `[32767..-32768]` for 16-bit devices (`ADS111x`). - /// The voltage that these values correspond to must be calculated using - /// the full-scale range selected. - /// See [`FullScaleRange`](enum.FullScaleRange.html). - /// - /// Returns `nb::Error::WouldBlock` while a measurement is in progress. - /// - /// In case a measurement was requested and after is it is finished a - /// measurement on a different channel is requested, a new measurement on - /// using the new channel selection is triggered. - #[allow(unused_variables)] - pub fn read<CH: ChannelId<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> { - self.read_inner(CH::channel_id()) - } -} - -impl<I2C, IC, CONV, E> DynamicOneShot for Ads1x1x<I2C, IC, CONV, mode::OneShot> -where - I2C: embedded_hal::i2c::I2c<Error = E>, - CONV: conversion::ConvertMeasurement, -{ - type Error = Error<E>; - - fn read(&mut self, channel: ChannelSelection) -> nb::Result<i16, Self::Error> { - self.read_inner(channel) - } } @@ -213,7 +213,7 @@ impl BitFlags { } pub mod channel; -pub use channel::{ChannelId, ChannelSelection}; +pub use channel::ChannelId; mod construction; mod conversion; pub use crate::conversion::{ConvertMeasurement, ConvertThreshold}; @@ -224,8 +224,7 @@ mod types; use crate::types::Config; pub use crate::types::{ mode, Ads1x1x, ComparatorLatching, ComparatorMode, ComparatorPolarity, ComparatorQueue, - DataRate12Bit, DataRate16Bit, DynamicOneShot, Error, FullScaleRange, ModeChangeError, - SlaveAddr, + DataRate12Bit, DataRate16Bit, Error, FullScaleRange, ModeChangeError, SlaveAddr, }; mod private { diff --git a/src/types.rs b/src/types.rs index 31b4cdb..bcda430 100644 --- a/src/types.rs +++ b/src/types.rs @@ -2,8 +2,6 @@ use core::marker::PhantomData; -use crate::{private, ChannelSelection}; - /// Errors in this crate #[derive(Debug)] pub enum Error<E> { @@ -235,15 +233,6 @@ pub struct Ads1x1x<I2C, 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}; |