use crate::private;
pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}
impl Error for core::convert::Infallible {
fn kind(&self) -> ErrorKind {
match *self {}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
Bus,
ArbitrationLoss,
NoAcknowledge(NoAcknowledgeSource),
Overrun,
Other,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum NoAcknowledgeSource {
Address,
Data,
Unknown,
}
impl Error for ErrorKind {
fn kind(&self) -> ErrorKind {
*self
}
}
impl core::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Bus => write!(f, "Bus error occurred"),
Self::ArbitrationLoss => write!(f, "The arbitration was lost"),
Self::NoAcknowledge(s) => s.fmt(f),
Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
Self::Other => write!(
f,
"A different error occurred. The original error may contain more information"
),
}
}
}
impl core::fmt::Display for NoAcknowledgeSource {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Address => write!(f, "The device did not acknowledge its address"),
Self::Data => write!(f, "The device did not acknowledge the data"),
Self::Unknown => write!(f, "The device did not acknowledge its address or the data"),
}
}
}
pub trait ErrorType {
type Error: Error;
}
impl<T: ErrorType> ErrorType for &mut T {
type Error = T::Error;
}
pub trait AddressMode: private::Sealed + 'static {}
pub type SevenBitAddress = u8;
pub type TenBitAddress = u16;
impl AddressMode for SevenBitAddress {}
impl AddressMode for TenBitAddress {}
pub mod blocking {
use super::{AddressMode, ErrorType, SevenBitAddress};
#[derive(Debug, PartialEq)]
pub enum Operation<'a> {
Read(&'a mut [u8]),
Write(&'a [u8]),
}
pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error>;
fn write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error>;
fn write_iter<B>(&mut self, address: A, bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>;
fn write_read(
&mut self,
address: A,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error>;
fn write_iter_read<B>(
&mut self,
address: A,
bytes: B,
buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>;
fn transaction<'a>(
&mut self,
address: A,
operations: &mut [Operation<'a>],
) -> Result<(), Self::Error>;
fn transaction_iter<'a, O>(&mut self, address: A, operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = Operation<'a>>;
}
impl<A: AddressMode, T: I2c<A>> I2c<A> for &mut T {
fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error> {
T::read(self, address, buffer)
}
fn write(&mut self, address: A, bytes: &[u8]) -> Result<(), Self::Error> {
T::write(self, address, bytes)
}
fn write_iter<B>(&mut self, address: A, bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
T::write_iter(self, address, bytes)
}
fn write_read(
&mut self,
address: A,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error> {
T::write_read(self, address, bytes, buffer)
}
fn write_iter_read<B>(
&mut self,
address: A,
bytes: B,
buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
T::write_iter_read(self, address, bytes, buffer)
}
fn transaction<'a>(
&mut self,
address: A,
operations: &mut [Operation<'a>],
) -> Result<(), Self::Error> {
T::transaction(self, address, operations)
}
fn transaction_iter<'a, O>(&mut self, address: A, operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = Operation<'a>>,
{
T::transaction_iter(self, address, operations)
}
}
}