summaryrefslogtreecommitdiffstats
path: root/src/devices
diff options
context:
space:
mode:
authorMarkus Reiter <me@reitermark.us>2024-01-19 15:09:19 +0100
committerGitHub <noreply@github.com>2024-01-19 15:09:19 +0100
commit5cd361340635aafe8806c82805622bc6afc4c4d7 (patch)
treee71f39a729a95020ca299925b93d0990416666b7 /src/devices
parent6f417d833e2a4041f32a6578332b050d79028910 (diff)
downloadads1x1x-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.
Diffstat (limited to 'src/devices')
-rw-r--r--src/devices/common.rs24
-rw-r--r--src/devices/features/tier1.rs16
-rw-r--r--src/devices/features/tier2.rs26
-rw-r--r--src/devices/mode/continuous.rs16
-rw-r--r--src/devices/mode/oneshot.rs24
5 files changed, 59 insertions, 47 deletions
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>;