Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove I2cInterface. #20

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions examples/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 6 additions & 11 deletions src/construction.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
}
}
};
Expand Down
24 changes: 19 additions & 5 deletions src/devices/common.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
//! 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(())
}

/// 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))
}
Expand Down
16 changes: 7 additions & 9 deletions src/devices/features/tier1.rs
Original file line number Diff line number Diff line change
@@ -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>> {
Expand All @@ -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>> {
Expand All @@ -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(())
}
Expand Down
26 changes: 13 additions & 13 deletions src/devices/features/tier2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
{
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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)
}
}
16 changes: 8 additions & 8 deletions src/devices/mode/continuous.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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))
}

Expand All @@ -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(())
}
Expand Down
24 changes: 12 additions & 12 deletions src/devices/mode/oneshot.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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>> {
Expand All @@ -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;
Expand Down Expand Up @@ -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>;
Expand Down
Loading