Skip to content

Commit

Permalink
fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
FloppyDisck committed Dec 1, 2024
1 parent c3b7b73 commit cb81053
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
43 changes: 32 additions & 11 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::address_from_pins;
use embedded_hal::i2c::{ErrorType, I2c, Operation, SevenBitAddress};
use crate::prelude::MultiplexerError;
use embedded_hal::i2c::{ErrorType, I2c, Operation, SevenBitAddress};

pub struct MultiplexerBus {
address: u8,
Expand Down Expand Up @@ -57,42 +57,63 @@ where
}
}

impl<I2C> ErrorType for BusPort<I2C> where I2C: I2c {
impl<I2C> ErrorType for BusPort<I2C>
where
I2C: I2c,
{
type Error = MultiplexerError<I2C::Error>;
}

impl<I2C> I2c for BusPort<I2C>
where I2C: I2c
where
I2C: I2c,
{
fn read(&mut self, address: SevenBitAddress, read: &mut [u8]) -> Result<(), Self::Error> {
self.open_port()?;
self.bus.read(address, read).map_err(|err| MultiplexerError::I2CError(err))
self.bus
.read(address, read)
.map_err(|err| MultiplexerError::I2CError(err))
}

fn write(&mut self, address: SevenBitAddress, write: &[u8]) -> Result<(), Self::Error> {
self.open_port()?;
self.bus.write(address, write).map_err(|err| MultiplexerError::I2CError(err))
self.bus
.write(address, write)
.map_err(|err| MultiplexerError::I2CError(err))
}

fn write_read(&mut self, address: SevenBitAddress, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
fn write_read(
&mut self,
address: SevenBitAddress,
write: &[u8],
read: &mut [u8],
) -> Result<(), Self::Error> {
self.open_port()?;
self.bus.write_read(address, write, read).map_err(|err| MultiplexerError::I2CError(err))
self.bus
.write_read(address, write, read)
.map_err(|err| MultiplexerError::I2CError(err))
}

fn transaction(&mut self, address: SevenBitAddress, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> {
fn transaction(
&mut self,
address: SevenBitAddress,
operations: &mut [Operation<'_>],
) -> Result<(), Self::Error> {
self.open_port()?;
self.bus.transaction(address, operations).map_err(|err| MultiplexerError::I2CError(err))
self.bus
.transaction(address, operations)
.map_err(|err| MultiplexerError::I2CError(err))
}
}

#[cfg(test)]
mod test {
extern crate alloc;
use crate::prelude::*;
use alloc::vec;
use core::cell::RefCell;
use embedded_hal::i2c::I2c;
use embedded_hal_bus::i2c::RefCellDevice;
use crate::prelude::*;
use embedded_hal_mock::eh1::i2c::{Mock, Transaction};

#[test]
Expand Down Expand Up @@ -192,7 +213,7 @@ mod test {
assert!(multiplexed_i2c_d.read(component_addr, &mut md).is_ok());
assert_eq!(md, [0x45, 0x48]);
}

i2c.into_inner().done();
}

Expand Down
18 changes: 12 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use thiserror::Error;
pub type Result<T, I2cError> = core::result::Result<T, MultiplexerError<I2cError>>;

#[derive(Error, Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum MultiplexerError<I2cError> where I2cError: Error {
pub enum MultiplexerError<I2cError>
where
I2cError: Error,
{
#[error("Write Read I2C Error")]
WriteReadI2CError,
#[error("Write I2C Error")]
Expand All @@ -14,14 +17,17 @@ pub enum MultiplexerError<I2cError> where I2cError: Error {
#[error("Incorrect port supplied")]
PortError,
#[error("I2C Error")]
I2CError(I2cError)
I2CError(I2cError),
}

impl<I2cError> Error for MultiplexerError<I2cError> where I2cError: Error {
impl<I2cError> Error for MultiplexerError<I2cError>
where
I2cError: Error,
{
fn kind(&self) -> ErrorKind {
match self {
match self {
Self::I2CError(e) => e.kind(),
_ => ErrorKind::Other
_ => ErrorKind::Other,
}
}
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ where
}

fn i2c_write(&mut self, bytes: &[u8]) -> Result<(), I2C::Error> {
self.i2c.write(self.address, bytes).map_err(|err| MultiplexerError::I2CError(err))
self.i2c
.write(self.address, bytes)
.map_err(MultiplexerError::I2CError)
}
}

Expand Down

0 comments on commit cb81053

Please sign in to comment.