extern crate alloc;
use alloc::rc::Rc;
use core::cell::RefCell;
use embedded_hal::i2c::{ErrorType, I2c};
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
pub struct RcDevice<Bus> {
bus: Rc<RefCell<Bus>>,
}
impl<Bus> RcDevice<Bus> {
#[inline]
pub fn new(bus: Rc<RefCell<Bus>>) -> Self {
Self { bus }
}
}
impl<Bus> ErrorType for RcDevice<Bus>
where
Bus: ErrorType,
{
type Error = Bus::Error;
}
impl<Bus> I2c for RcDevice<Bus>
where
Bus: I2c,
{
#[inline]
fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
let bus = &mut *self.bus.borrow_mut();
bus.read(address, read)
}
#[inline]
fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
let bus = &mut *self.bus.borrow_mut();
bus.write(address, write)
}
#[inline]
fn write_read(
&mut self,
address: u8,
write: &[u8],
read: &mut [u8],
) -> Result<(), Self::Error> {
let bus = &mut *self.bus.borrow_mut();
bus.write_read(address, write, read)
}
#[inline]
fn transaction(
&mut self,
address: u8,
operations: &mut [embedded_hal::i2c::Operation<'_>],
) -> Result<(), Self::Error> {
let bus = &mut *self.bus.borrow_mut();
bus.transaction(address, operations)
}
}