diff options
author | Markus Reiter <me@reitermark.us> | 2024-01-19 15:09:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-19 15:09:19 +0100 |
commit | 5cd361340635aafe8806c82805622bc6afc4c4d7 (patch) | |
tree | e71f39a729a95020ca299925b93d0990416666b7 | |
parent | 6f417d833e2a4041f32a6578332b050d79028910 (diff) | |
download | ads1x1x-async-5cd361340635aafe8806c82805622bc6afc4c4d7.tar.gz ads1x1x-async-5cd361340635aafe8806c82805622bc6afc4c4d7.tar.xz ads1x1x-async-5cd361340635aafe8806c82805622bc6afc4c4d7.zip |
Remove `I2cInterface`. (#20)
* Merge `ReadData` and `WriteData` traits.
* Remove `I2cInterface`.
* Update changelog.
* Fix links.
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | examples/typed.rs | 3 | ||||
-rw-r--r-- | src/channel.rs | 2 | ||||
-rw-r--r-- | src/construction.rs | 17 | ||||
-rw-r--r-- | src/devices/common.rs | 24 | ||||
-rw-r--r-- | src/devices/features/tier1.rs | 16 | ||||
-rw-r--r-- | src/devices/features/tier2.rs | 26 | ||||
-rw-r--r-- | src/devices/mode/continuous.rs | 16 | ||||
-rw-r--r-- | src/devices/mode/oneshot.rs | 24 | ||||
-rw-r--r-- | src/interface.rs | 53 | ||||
-rw-r--r-- | src/lib.rs | 11 | ||||
-rw-r--r-- | src/types.rs | 5 | ||||
-rw-r--r-- | tests/common/mod.rs | 11 |
13 files changed, 79 insertions, 132 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 49cd011..362a5f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Updated `embedded-hal` to version `1`, `read` in one-shot mode is therefore only an inherent method. - Raised MSRV to 1.62.0. +### Removed +- Removed `I2cInterface`. + ## [0.2.2] - 2021-07-29 ### Added diff --git a/examples/typed.rs b/examples/typed.rs index 6d6961c..572754f 100644 --- a/examples/typed.rs +++ b/examples/typed.rs @@ -7,12 +7,11 @@ use nb::block; use ads1x1x::{ channel, ic::{Ads1115, Resolution16Bit}, - interface::I2cInterface, Ads1x1x, SlaveAddr, }; /// Type alias -type Adc = Ads1x1x<I2cInterface<I2cdev>, Ads1115, Resolution16Bit, ads1x1x::mode::OneShot>; +type Adc = Ads1x1x<I2cdev, Ads1115, Resolution16Bit, ads1x1x::mode::OneShot>; /// Read a single value from channel A. /// Returns 0 on Error. diff --git a/src/channel.rs b/src/channel.rs index df23c39..27262d4 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -29,7 +29,7 @@ macro_rules! impl_channels { impl private::Sealed for $CH {} $( - impl<DI, CONV, MODE> ChannelId<Ads1x1x<DI, ic::$IC, CONV, MODE>> for $CH { + impl<I2C, CONV, MODE> ChannelId<Ads1x1x<I2C, ic::$IC, CONV, MODE>> for $CH { fn channel_id() -> ChannelSelection { ChannelSelection::$CH } diff --git a/src/construction.rs b/src/construction.rs index d6463df..7ac128f 100644 --- a/src/construction.rs +++ b/src/construction.rs @@ -1,24 +1,19 @@ //! Constructor/destructor functions. -use crate::{ - ic, interface::I2cInterface, mode, Ads1x1x, Config, FullScaleRange, SlaveAddr, - DEVICE_BASE_ADDRESS, -}; +use crate::{ic, mode, Ads1x1x, Config, FullScaleRange, SlaveAddr, DEVICE_BASE_ADDRESS}; use core::marker::PhantomData; macro_rules! impl_new_destroy { ( $IC:ident, $create:ident, $destroy:ident, $conv:ty ) => { - impl<I2C, E> Ads1x1x<I2cInterface<I2C>, ic::$IC, $conv, mode::OneShot> + impl<I2C, E> Ads1x1x<I2C, ic::$IC, $conv, mode::OneShot> where I2C: embedded_hal::i2c::I2c<Error = E>, { /// Create a new instance of the device in OneShot mode. pub fn $create(i2c: I2C, address: SlaveAddr) -> Self { Ads1x1x { - iface: I2cInterface { - i2c, - address: address.addr(DEVICE_BASE_ADDRESS), - }, + i2c, + address: address.addr(DEVICE_BASE_ADDRESS), config: Config::default(), fsr: FullScaleRange::default(), a_conversion_was_started: false, @@ -28,10 +23,10 @@ macro_rules! impl_new_destroy { } } } - impl<I2C, CONV, MODE> Ads1x1x<I2cInterface<I2C>, ic::$IC, CONV, MODE> { + impl<I2C, CONV, MODE> Ads1x1x<I2C, ic::$IC, CONV, MODE> { /// Destroy driver instance, return I²C bus instance. pub fn $destroy(self) -> I2C { - self.iface.i2c + self.i2c } } }; diff --git a/src/devices/common.rs b/src/devices/common.rs index f1509d3..5fed33f 100644 --- a/src/devices/common.rs +++ b/src/devices/common.rs @@ -1,17 +1,31 @@ //! Common functions -use crate::{devices::OperatingMode, interface, Ads1x1x, BitFlags, Config, Error, Register}; +use crate::{devices::OperatingMode, Ads1x1x, BitFlags, Config, Error, Register}; -impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE> +impl<I2C, IC, CONV, MODE, E> Ads1x1x<I2C, IC, CONV, MODE> where - DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, { + pub(super) fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> { + let data = data.to_be_bytes(); + let payload: [u8; 3] = [register, data[0], data[1]]; + self.i2c.write(self.address, &payload).map_err(Error::I2C) + } + + pub(super) fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> { + let mut data = [0, 0]; + self.i2c + .write_read(self.address, &[register], &mut data) + .map_err(Error::I2C) + .and(Ok(u16::from_be_bytes(data))) + } + pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> { let config = match mode { OperatingMode::OneShot => self.config.with_high(BitFlags::OP_MODE), OperatingMode::Continuous => self.config.with_low(BitFlags::OP_MODE), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -19,7 +33,7 @@ where /// Read whether a measurement is currently in progress. pub fn is_measurement_in_progress(&mut self) -> Result<bool, Error<E>> { let config = Config { - bits: self.iface.read_register(Register::CONFIG)?, + bits: self.read_register(Register::CONFIG)?, }; Ok(!config.is_high(BitFlags::OS)) } diff --git a/src/devices/features/tier1.rs b/src/devices/features/tier1.rs index 0c940ee..c6aa8cd 100644 --- a/src/devices/features/tier1.rs +++ b/src/devices/features/tier1.rs @@ -1,12 +1,10 @@ //! Common functions -use crate::{ - ic, interface, Ads1x1x, BitFlags as BF, DataRate12Bit, DataRate16Bit, Error, Register, -}; +use crate::{ic, Ads1x1x, BitFlags as BF, DataRate12Bit, DataRate16Bit, Error, Register}; -impl<DI, IC, MODE, E> Ads1x1x<DI, IC, ic::Resolution12Bit, MODE> +impl<I2C, IC, MODE, E> Ads1x1x<I2C, IC, ic::Resolution12Bit, MODE> where - DI: interface::WriteData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, { /// Set data rate pub fn set_data_rate(&mut self, rate: DataRate12Bit) -> Result<(), Error<E>> { @@ -21,15 +19,15 @@ where DR::Sps2400 => cfg.with_high(BF::DR2).with_low(BF::DR1).with_high(BF::DR0), DR::Sps3300 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_low(BF::DR0), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } } -impl<DI, IC, MODE, E> Ads1x1x<DI, IC, ic::Resolution16Bit, MODE> +impl<I2C, IC, MODE, E> Ads1x1x<I2C, IC, ic::Resolution16Bit, MODE> where - DI: interface::WriteData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, { /// Set data rate pub fn set_data_rate(&mut self, rate: DataRate16Bit) -> Result<(), Error<E>> { @@ -45,7 +43,7 @@ where DR::Sps475 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_low(BF::DR0), DR::Sps860 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_high(BF::DR0), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs index 96330a0..4eb989e 100644 --- a/src/devices/features/tier2.rs +++ b/src/devices/features/tier2.rs @@ -3,13 +3,13 @@ //! These are the features included only in ADS1x14, ADS1x15 use crate::{ - conversion, ic, interface, Ads1x1x, BitFlags as BF, ComparatorLatching, ComparatorMode, + conversion, ic, Ads1x1x, BitFlags as BF, ComparatorLatching, ComparatorMode, ComparatorPolarity, ComparatorQueue, Error, FullScaleRange, Register, }; -impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE> +impl<I2C, IC, CONV, MODE, E> Ads1x1x<I2C, IC, CONV, MODE> where - DI: interface::WriteData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, IC: ic::Tier2Features, CONV: conversion::ConvertThreshold<E>, { @@ -42,7 +42,7 @@ where .with_low(BF::PGA1) .with_high(BF::PGA0), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -55,7 +55,7 @@ where /// selected. See [`FullScaleRange`](enum.FullScaleRange.html). pub fn set_low_threshold_raw(&mut self, value: i16) -> Result<(), Error<E>> { let register_value = CONV::convert_threshold(value)?; - self.iface.write_register(Register::LOW_TH, register_value) + self.write_register(Register::LOW_TH, register_value) } /// Set raw comparator upper threshold @@ -66,7 +66,7 @@ where /// selected. See [`FullScaleRange`](enum.FullScaleRange.html). pub fn set_high_threshold_raw(&mut self, value: i16) -> Result<(), Error<E>> { let register_value = CONV::convert_threshold(value)?; - self.iface.write_register(Register::HIGH_TH, register_value) + self.write_register(Register::HIGH_TH, register_value) } /// Set comparator mode @@ -75,7 +75,7 @@ where ComparatorMode::Traditional => self.config.with_low(BF::COMP_MODE), ComparatorMode::Window => self.config.with_high(BF::COMP_MODE), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -89,7 +89,7 @@ where ComparatorPolarity::ActiveLow => self.config.with_low(BF::COMP_POL), ComparatorPolarity::ActiveHigh => self.config.with_high(BF::COMP_POL), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -103,7 +103,7 @@ where ComparatorLatching::Nonlatching => self.config.with_low(BF::COMP_LAT), ComparatorLatching::Latching => self.config.with_high(BF::COMP_LAT), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -117,7 +117,7 @@ where ComparatorQueue::Two => self.config.with_low(BF::COMP_QUE1).with_high(BF::COMP_QUE0), ComparatorQueue::Four => self.config.with_high(BF::COMP_QUE1).with_low(BF::COMP_QUE0), }; - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -132,7 +132,7 @@ where .config .with_high(BF::COMP_QUE1) .with_high(BF::COMP_QUE0); - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -153,7 +153,7 @@ where { self.disable_comparator()?; } - self.iface.write_register(Register::HIGH_TH, 0x8000)?; - self.iface.write_register(Register::LOW_TH, 0) + self.write_register(Register::HIGH_TH, 0x8000)?; + self.write_register(Register::LOW_TH, 0) } } diff --git a/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs index b3b4807..edc87c2 100644 --- a/src/devices/mode/continuous.rs +++ b/src/devices/mode/continuous.rs @@ -1,25 +1,25 @@ //! Continuous measurement mode use crate::{ - conversion, devices::OperatingMode, interface, mode, Ads1x1x, ChannelId, Error, - ModeChangeError, Register, + conversion, devices::OperatingMode, mode, Ads1x1x, ChannelId, Error, ModeChangeError, Register, }; use core::marker::PhantomData; -impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous> +impl<I2C, IC, CONV, E> Ads1x1x<I2C, IC, CONV, mode::Continuous> where - DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, CONV: conversion::ConvertMeasurement, { /// Change operating mode to OneShot pub fn into_one_shot( mut self, - ) -> Result<Ads1x1x<DI, IC, CONV, mode::OneShot>, ModeChangeError<E, Self>> { + ) -> Result<Ads1x1x<I2C, IC, CONV, mode::OneShot>, ModeChangeError<E, Self>> { if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::OneShot) { return Err(ModeChangeError::I2C(e, self)); } Ok(Ads1x1x { - iface: self.iface, + i2c: self.i2c, + address: self.address, config: self.config, fsr: self.fsr, a_conversion_was_started: false, @@ -31,7 +31,7 @@ where /// Read the most recent measurement pub fn read(&mut self) -> Result<i16, Error<E>> { - let value = self.iface.read_register(Register::CONVERSION)?; + let value = self.read_register(Register::CONVERSION)?; Ok(CONV::convert_measurement(value)) } @@ -43,7 +43,7 @@ where #[allow(unused_variables)] pub fn select_channel<CH: ChannelId<Self>>(&mut self, channel: CH) -> Result<(), Error<E>> { let config = self.config.with_mux_bits(CH::channel_id()); - self.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index 0663e4b..957eba5 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -1,24 +1,25 @@ //! Common functions use crate::{ - conversion, devices::OperatingMode, interface, mode, Ads1x1x, BitFlags, ChannelId, - ChannelSelection, Config, DynamicOneShot, Error, ModeChangeError, Register, + conversion, devices::OperatingMode, mode, Ads1x1x, BitFlags, ChannelId, ChannelSelection, + Config, DynamicOneShot, Error, ModeChangeError, Register, }; use core::marker::PhantomData; -impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot> +impl<I2C, IC, CONV, E> Ads1x1x<I2C, IC, CONV, mode::OneShot> where - DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, CONV: conversion::ConvertMeasurement, { /// Change operating mode to Continuous pub fn into_continuous( mut self, - ) -> Result<Ads1x1x<DI, IC, CONV, mode::Continuous>, ModeChangeError<E, Self>> { + ) -> Result<Ads1x1x<I2C, IC, CONV, mode::Continuous>, ModeChangeError<E, Self>> { if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::Continuous) { return Err(ModeChangeError::I2C(e, self)); } Ok(Ads1x1x { - iface: self.iface, + i2c: self.i2c, + address: self.address, config: self.config, fsr: self.fsr, a_conversion_was_started: true, @@ -30,13 +31,13 @@ where fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error<E>> { let config = config.with_high(BitFlags::OS); - self.iface.write_register(Register::CONFIG, config.bits) + self.write_register(Register::CONFIG, config.bits) } } -impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot> +impl<I2C, IC, CONV, E> Ads1x1x<I2C, IC, CONV, mode::OneShot> where - DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, CONV: conversion::ConvertMeasurement, { fn read_inner(&mut self, channel: ChannelSelection) -> nb::Result<i16, Error<E>> { @@ -51,7 +52,6 @@ where if self.a_conversion_was_started && same_channel { // result is ready let value = self - .iface .read_register(Register::CONVERSION) .map_err(nb::Error::Other)?; self.a_conversion_was_started = false; @@ -83,9 +83,9 @@ where } } -impl<DI, IC, CONV, E> DynamicOneShot for Ads1x1x<DI, IC, CONV, mode::OneShot> +impl<I2C, IC, CONV, E> DynamicOneShot for Ads1x1x<I2C, IC, CONV, mode::OneShot> where - DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, CONV: conversion::ConvertMeasurement, { type Error = Error<E>; diff --git a/src/interface.rs b/src/interface.rs deleted file mode 100644 index 0e665a3..0000000 --- a/src/interface.rs +++ /dev/null @@ -1,53 +0,0 @@ -//! I2C interface - -use crate::{private, Error}; - -/// I2C interface -#[derive(Debug, Default)] -pub struct I2cInterface<I2C> { - pub(crate) i2c: I2C, - pub(crate) address: u8, -} - -/// Write data -pub trait WriteData: private::Sealed { - /// Error type - type Error; - - /// Write to an u16 register - fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<Self::Error>>; -} - -impl<I2C, E> WriteData for I2cInterface<I2C> -where - I2C: embedded_hal::i2c::I2c<Error = E>, -{ - type Error = E; - fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> { - let payload: [u8; 3] = [register, (data >> 8) as u8, data as u8]; - self.i2c.write(self.address, &payload).map_err(Error::I2C) - } -} - -/// Read data -pub trait ReadData: private::Sealed { - /// Error type - type Error; - - /// Read an u16 register - fn read_register(&mut self, register: u8) -> Result<u16, Error<Self::Error>>; -} - -impl<I2C, E> ReadData for I2cInterface<I2C> -where - I2C: embedded_hal::i2c::I2c<Error = E>, -{ - type Error = E; - fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> { - let mut data = [0, 0]; - self.i2c - .write_read(self.address, &[register], &mut data) - .map_err(Error::I2C) - .and(Ok((u16::from(data[0]) << 8) | u16::from(data[1]))) - } -} @@ -22,9 +22,9 @@ //! - Disable the comparator. See: [`disable_comparator()`]. //! //! [`into_continuous()`]: struct.Ads1x1x.html#method.into_continuous -//! [read_os]: struct.Ads1x1x.html#method.read +//! [read_os]: struct.Ads1x1x.html#method.read-1 //! [`start()`]: struct.Ads1x1x.html#method.start -//! [read_cont]: struct.Ads1x1x.html#impl-OneShot%3CAds1x1x%3CDI%2C%20IC%2C%20CONV%2C%20OneShot%3E%2C%20i16%2C%20CH%3E +//! [read_cont]: struct.Ads1x1x.html#method.read //! [`set_data_rate()`]: struct.Ads1x1x.html#method.set_data_rate //! [`set_full_scale_range()`]: struct.Ads1x1x.html#method.set_full_scale_range //! [`is_measurement_in_progress()`]: struct.Ads1x1x.html#method.is_measurement_in_progress @@ -242,8 +242,6 @@ pub use crate::conversion::{ConvertMeasurement, ConvertThreshold}; mod devices; #[doc(hidden)] pub mod ic; -#[doc(hidden)] -pub mod interface; mod types; use crate::types::Config; pub use crate::types::{ @@ -253,11 +251,10 @@ pub use crate::types::{ }; mod private { - use super::{ic, interface, Ads1x1x}; + use super::{ic, Ads1x1x}; pub trait Sealed {} - impl<I2C> Sealed for interface::I2cInterface<I2C> {} - impl<DI, IC, CONV, MODE> Sealed for Ads1x1x<DI, IC, CONV, MODE> {} + impl<I2C, IC, CONV, MODE> Sealed for Ads1x1x<I2C, IC, CONV, MODE> {} impl Sealed for ic::Resolution12Bit {} impl Sealed for ic::Resolution16Bit {} diff --git a/src/types.rs b/src/types.rs index 6937dfe..c3de8b0 100644 --- a/src/types.rs +++ b/src/types.rs @@ -245,8 +245,9 @@ impl Default for Config { /// ADS1x1x ADC driver #[derive(Debug, Default)] -pub struct Ads1x1x<DI, IC, CONV, MODE> { - pub(crate) iface: DI, +pub struct Ads1x1x<I2C, IC, CONV, MODE> { + pub(crate) i2c: I2C, + pub(crate) address: u8, pub(crate) config: Config, pub(crate) fsr: FullScaleRange, pub(crate) a_conversion_was_started: bool, diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 429479c..ffeb916 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,4 +1,4 @@ -use ads1x1x::{ic, interface, mode, Ads1x1x, SlaveAddr}; +use ads1x1x::{ic, mode, Ads1x1x, SlaveAddr}; use embedded_hal_mock::eh1::i2c::{Mock as I2cMock, Transaction as I2cTrans}; #[allow(unused)] @@ -82,14 +82,7 @@ macro_rules! impl_new_destroy { macro_rules! impl_new_destroy_i2c { ($ic:ident, $create:ident, $destroy:ident, $conv:ty) => { - impl_new_destroy!( - $ic, - $create, - $destroy, - $conv, - I2cTrans, - interface::I2cInterface<I2cMock> - ); + impl_new_destroy!($ic, $create, $destroy, $conv, I2cTrans, I2cMock); }; } |