embedded_hal_bus/i2c/
rc.rs1extern crate alloc;
2use alloc::rc::Rc;
3
4use core::cell::RefCell;
5use embedded_hal::i2c::{ErrorType, I2c};
6
7#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
17pub struct RcDevice<Bus> {
18 bus: Rc<RefCell<Bus>>,
19}
20
21impl<Bus> RcDevice<Bus> {
22 #[inline]
27 pub fn new(bus: Rc<RefCell<Bus>>) -> Self {
28 Self { bus }
29 }
30}
31
32impl<Bus> ErrorType for RcDevice<Bus>
33where
34 Bus: ErrorType,
35{
36 type Error = Bus::Error;
37}
38
39impl<Bus> I2c for RcDevice<Bus>
40where
41 Bus: I2c,
42{
43 #[inline]
44 fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
45 let bus = &mut *self.bus.borrow_mut();
46 bus.read(address, read)
47 }
48
49 #[inline]
50 fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
51 let bus = &mut *self.bus.borrow_mut();
52 bus.write(address, write)
53 }
54
55 #[inline]
56 fn write_read(
57 &mut self,
58 address: u8,
59 write: &[u8],
60 read: &mut [u8],
61 ) -> Result<(), Self::Error> {
62 let bus = &mut *self.bus.borrow_mut();
63 bus.write_read(address, write, read)
64 }
65
66 #[inline]
67 fn transaction(
68 &mut self,
69 address: u8,
70 operations: &mut [embedded_hal::i2c::Operation<'_>],
71 ) -> Result<(), Self::Error> {
72 let bus = &mut *self.bus.borrow_mut();
73 bus.transaction(address, operations)
74 }
75}