From 00830f4e5569a381afbdfce270e478c5fd25401f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 10 Jun 2024 21:56:37 +0000 Subject: Remove `DynamicOneShot` trait. (#19) * Remove `DynamicOneShot` trait. * Update changelog. --- CHANGELOG.md | 1 + examples/trait.rs | 23 -------------------- src/channel.rs | 24 ++++++++++----------- src/devices/mode/oneshot.rs | 52 ++++++++++++++++----------------------------- src/lib.rs | 5 ++--- src/types.rs | 11 ---------- 6 files changed, 32 insertions(+), 84 deletions(-) delete mode 100644 examples/trait.rs 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>(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: 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 { + #[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 ChannelId> 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, 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 277b2ad..cd63c4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { @@ -235,15 +233,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::{FullScaleRange, SlaveAddr}; -- cgit v1.2.3-54-g00ecf