From 5cd361340635aafe8806c82805622bc6afc4c4d7 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 Jan 2024 15:09:19 +0100 Subject: Remove `I2cInterface`. (#20) * Merge `ReadData` and `WriteData` traits. * Remove `I2cInterface`. * Update changelog. * Fix links. --- src/devices/common.rs | 24 +++++++++++++++++++----- src/devices/features/tier1.rs | 16 +++++++--------- src/devices/features/tier2.rs | 26 +++++++++++++------------- src/devices/mode/continuous.rs | 16 ++++++++-------- src/devices/mode/oneshot.rs | 24 ++++++++++++------------ 5 files changed, 59 insertions(+), 47 deletions(-) (limited to 'src/devices') 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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData + interface::ReadData, + I2C: embedded_hal::i2c::I2c, { + pub(super) fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error> { + 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> { + 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> { 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> { 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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData, + I2C: embedded_hal::i2c::I2c, { /// Set data rate pub fn set_data_rate(&mut self, rate: DataRate12Bit) -> Result<(), Error> { @@ -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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData, + I2C: embedded_hal::i2c::I2c, { /// Set data rate pub fn set_data_rate(&mut self, rate: DataRate16Bit) -> Result<(), Error> { @@ -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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData, + I2C: embedded_hal::i2c::I2c, IC: ic::Tier2Features, CONV: conversion::ConvertThreshold, { @@ -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> { 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> { 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 Ads1x1x +impl Ads1x1x where - DI: interface::ReadData + interface::WriteData, + I2C: embedded_hal::i2c::I2c, CONV: conversion::ConvertMeasurement, { /// Change operating mode to OneShot pub fn into_one_shot( mut self, - ) -> Result, ModeChangeError> { + ) -> Result, ModeChangeError> { 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> { - 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>(&mut self, channel: CH) -> Result<(), Error> { 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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData + interface::ReadData, + I2C: embedded_hal::i2c::I2c, CONV: conversion::ConvertMeasurement, { /// Change operating mode to Continuous pub fn into_continuous( mut self, - ) -> Result, ModeChangeError> { + ) -> Result, ModeChangeError> { 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> { let config = config.with_high(BitFlags::OS); - self.iface.write_register(Register::CONFIG, config.bits) + self.write_register(Register::CONFIG, config.bits) } } -impl Ads1x1x +impl Ads1x1x where - DI: interface::ReadData + interface::WriteData, + I2C: embedded_hal::i2c::I2c, CONV: conversion::ConvertMeasurement, { fn read_inner(&mut self, channel: ChannelSelection) -> nb::Result> { @@ -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 DynamicOneShot for Ads1x1x +impl DynamicOneShot for Ads1x1x where - DI: interface::ReadData + interface::WriteData, + I2C: embedded_hal::i2c::I2c, CONV: conversion::ConvertMeasurement, { type Error = Error; -- cgit v1.2.3-54-g00ecf