use super::Arbiter;
use embedded_hal::i2c::{AddressMode, ErrorType, I2c as BlockingI2c, Operation};
use embedded_hal_async::i2c::I2c as AsyncI2c;
pub struct ArbiterDevice<'a, BUS> {
bus: &'a Arbiter<BUS>,
}
impl<'a, BUS> ArbiterDevice<'a, BUS> {
pub fn new(bus: &'a Arbiter<BUS>) -> Self {
Self { bus }
}
}
impl<BUS> ErrorType for ArbiterDevice<'_, BUS>
where
BUS: ErrorType,
{
type Error = BUS::Error;
}
impl<BUS, A> AsyncI2c<A> for ArbiterDevice<'_, BUS>
where
BUS: AsyncI2c<A>,
A: AddressMode,
{
async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.read(address, read).await
}
async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.write(address, write).await
}
async fn write_read(
&mut self,
address: A,
write: &[u8],
read: &mut [u8],
) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.write_read(address, write, read).await
}
async fn transaction(
&mut self,
address: A,
operations: &mut [Operation<'_>],
) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.transaction(address, operations).await
}
}
pub struct BlockingArbiterDevice<'a, BUS> {
bus: &'a Arbiter<BUS>,
}
impl<'a, BUS> BlockingArbiterDevice<'a, BUS> {
pub fn new(bus: &'a Arbiter<BUS>) -> Self {
Self { bus }
}
pub fn into_non_blocking(self) -> ArbiterDevice<'a, BUS>
where
BUS: AsyncI2c,
{
ArbiterDevice { bus: self.bus }
}
}
impl<'a, BUS> ErrorType for BlockingArbiterDevice<'a, BUS>
where
BUS: ErrorType,
{
type Error = BUS::Error;
}
impl<'a, BUS, A> AsyncI2c<A> for BlockingArbiterDevice<'a, BUS>
where
BUS: BlockingI2c<A>,
A: AddressMode,
{
async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.read(address, read)
}
async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.write(address, write)
}
async fn write_read(
&mut self,
address: A,
write: &[u8],
read: &mut [u8],
) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.write_read(address, write, read)
}
async fn transaction(
&mut self,
address: A,
operations: &mut [Operation<'_>],
) -> Result<(), Self::Error> {
let mut bus = self.bus.access().await;
bus.transaction(address, operations)
}
}