From bad978d2645e5bb8c1ef71f0a071a3d8f547a91b Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 12 Jan 2024 20:24:51 +0100 Subject: [PATCH] Remove `DynamicOneShot` trait. --- examples/trait.rs | 24 ----------------- src/channel.rs | 24 +++++++++-------- src/devices/mode/oneshot.rs | 52 +++++++++++++------------------------ src/lib.rs | 5 ++-- src/types.rs | 11 -------- 5 files changed, 33 insertions(+), 83 deletions(-) delete mode 100644 examples/trait.rs diff --git a/examples/trait.rs b/examples/trait.rs deleted file mode 100644 index 070d89c..0000000 --- a/examples/trait.rs +++ /dev/null @@ -1,24 +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>(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(); -} diff --git a/src/channel.rs b/src/channel.rs index 27262d4..6b7664e 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -1,25 +1,27 @@ //! 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: private::Sealed { +pub trait ChannelId { /// 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 { + pub trait Sealed {} + + #[derive(Debug, Clone, Copy)] + /// ADC input channel selection. + pub enum ChannelSelection { + $( + #[doc = $doc] + $CH, + )+ + } } $( 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, CONV: conversion::ConvertMeasurement, { - fn read_inner(&mut self, channel: ChannelSelection) -> nb::Result> { + /// 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>(&mut self, channel: CH) -> nb::Result> { 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>(&mut self, channel: CH) -> nb::Result> { - self.read_inner(CH::channel_id()) - } -} - -impl DynamicOneShot for Ads1x1x -where - I2C: embedded_hal::i2c::I2c, - CONV: conversion::ConvertMeasurement, -{ - type Error = Error; - - fn read(&mut self, channel: ChannelSelection) -> nb::Result { - self.read_inner(channel) - } } diff --git a/src/lib.rs b/src/lib.rs index c020afc..c7acd30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -235,7 +235,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}; @@ -246,8 +246,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 c3de8b0..ed69310 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 { @@ -256,15 +254,6 @@ pub struct Ads1x1x { pub(crate) _mode: PhantomData, } -/// 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; -} - #[cfg(test)] mod tests { use crate::DEVICE_BASE_ADDRESS as ADDR;