Skip to content

Commit

Permalink
Merge traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 17, 2024
1 parent 737f049 commit 5f797bc
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/devices/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{devices::OperatingMode, interface, Ads1x1x, BitFlags, Config, Error,

impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE>
where
DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>,
DI: interface::ReadWriteRegister<Error = E>,
{
pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> {
let config = match mode {
Expand Down
4 changes: 2 additions & 2 deletions src/devices/features/tier1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

impl<DI, IC, MODE, E> Ads1x1x<DI, IC, ic::Resolution12Bit, MODE>
where
DI: interface::WriteData<Error = E>,
DI: interface::ReadWriteRegister<Error = E>,
{
/// Set data rate
pub fn set_data_rate(&mut self, rate: DataRate12Bit) -> Result<(), Error<E>> {
Expand All @@ -29,7 +29,7 @@ where

impl<DI, IC, MODE, E> Ads1x1x<DI, IC, ic::Resolution16Bit, MODE>
where
DI: interface::WriteData<Error = E>,
DI: interface::ReadWriteRegister<Error = E>,
{
/// Set data rate
pub fn set_data_rate(&mut self, rate: DataRate16Bit) -> Result<(), Error<E>> {
Expand Down
2 changes: 1 addition & 1 deletion src/devices/features/tier2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{

impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE>
where
DI: interface::WriteData<Error = E>,
DI: interface::ReadWriteRegister<Error = E>,
IC: ic::Tier2Features,
CONV: conversion::ConvertThreshold<E>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/devices/mode/continuous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::marker::PhantomData;

impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous>
where
DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>,
DI: interface::ReadWriteRegister<Error = E>,
CONV: conversion::ConvertMeasurement,
{
/// Change operating mode to OneShot
Expand Down
4 changes: 2 additions & 2 deletions src/devices/mode/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::marker::PhantomData;

impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot>
where
DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>,
DI: interface::ReadWriteRegister<Error = E>,
CONV: conversion::ConvertMeasurement,
{
/// Change operating mode to Continuous
Expand Down Expand Up @@ -36,7 +36,7 @@ where

impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot>
where
DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>,
DI: interface::ReadWriteRegister<Error = E>,
CONV: conversion::ConvertMeasurement,
{
/// Request that the ADC begin a conversion on the specified channel.
Expand Down
34 changes: 12 additions & 22 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,35 @@ pub struct I2cInterface<I2C> {
pub(crate) address: u8,
}

/// Write data
pub trait WriteData: private::Sealed {
/// Error type
/// Read/write data from/to register.
pub trait ReadWriteRegister: private::Sealed {
/// Error type.
type Error;

/// Write to an u16 register
/// Reads a `u16` register.
fn read_register(&mut self, register: u8) -> Result<u16, Error<Self::Error>>;

/// Writes to a `u16` register.
fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<Self::Error>>;
}

impl<I2C, E> WriteData for I2cInterface<I2C>
impl<I2C, E> ReadWriteRegister 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];
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)
}
}

/// 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])))
.and(Ok(u16::from_be_bytes(data)))
}
}

0 comments on commit 5f797bc

Please sign in to comment.