@@ -6,69 +6,69 @@ use embedded_hal::i2c::{ErrorType, I2c};
66/// Sharing is implemented with a `RefCell`. This means it has low overhead, but `RefCellDevice` instances are not `Send`,
77/// so it only allows sharing within a single thread (interrupt priority level). If you need to share a bus across several
88/// threads, use [`CriticalSectionDevice`](super::CriticalSectionDevice) instead.
9+ ///
10+ /// # Examples
11+ ///
12+ /// Assuming there is a pressure sensor with address `0x42` on the same bus as a temperature sensor
13+ /// with address `0x20`; [`RefCellDevice`] can be used to give access to both of these sensors
14+ /// from a single `i2c` instance.
15+ ///
16+ /// ```
17+ /// use embedded_hal_bus::i2c;
18+ /// use core::cell::RefCell;
19+ /// # use embedded_hal::i2c::{self as hali2c, SevenBitAddress, TenBitAddress, I2c, Operation, ErrorKind};
20+ /// # pub struct Sensor<I2C> {
21+ /// # i2c: I2C,
22+ /// # address: u8,
23+ /// # }
24+ /// # impl<I2C: I2c> Sensor<I2C> {
25+ /// # pub const fn new(i2c: I2C, address: u8) -> Self {
26+ /// # Self { i2c, address }
27+ /// # }
28+ /// # }
29+ /// # type PressureSensor = Sensor;
30+ /// # type TemperatureSensor = Sensor;
31+ /// # pub struct I2c0;
32+ /// # #[derive(Debug, Copy, Clone, Eq, PartialEq)]
33+ /// # pub enum Error { }
34+ /// # impl hali2c::Error for Error {
35+ /// # fn kind(&self) -> hali2c::ErrorKind {
36+ /// # ErrorKind::Other
37+ /// # }
38+ /// # }
39+ /// # impl hali2c::ErrorType for I2c0 {
40+ /// # type Error = Error;
41+ /// # }
42+ /// # impl I2c<SevenBitAddress> for I2c0 {
43+ /// # fn transaction(&mut self, address: u8, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> {
44+ /// # Ok(())
45+ /// # }
46+ /// # }
47+ /// # struct Hal;
48+ /// # impl Hal {
49+ /// # fn i2c(&self) -> I2c0 {
50+ /// # I2c0
51+ /// # }
52+ /// # }
53+ /// # let hal = Hal;
54+ ///
55+ /// let i2c = hal.i2c();
56+ /// let i2c_ref_cell = RefCell::new(i2c);
57+ /// let mut temperature_sensor = TemperatureSensor::new(
58+ /// i2c::RefCellDevice::new(&i2c_ref_cell),
59+ /// 0x20,
60+ /// );
61+ /// let mut pressure_sensor = PressureSensor::new(
62+ /// i2c::RefCellDevice::new(&i2c_ref_cell),
63+ /// 0x42,
64+ /// );
65+ /// ```
966pub struct RefCellDevice < ' a , T > {
1067 bus : & ' a RefCell < T > ,
1168}
1269
1370impl < ' a , T > RefCellDevice < ' a , T > {
1471 /// Create a new `RefCellDevice`
15- ///
16- /// # Examples
17- ///
18- /// Assuming there is a pressure sensor with address `0x42` on the same bus as a temperature sensor
19- /// with address `0x20`; [`RefCellDevice`] can be used to give access to both of these sensors
20- /// from a single `i2c` instance.
21- ///
22- /// ```
23- /// use embedded_hal_bus::i2c;
24- /// use core::cell::RefCell;
25- /// # use embedded_hal::i2c::{self as hali2c, SevenBitAddress, TenBitAddress, I2c, Operation, ErrorKind};
26- /// # pub struct Sensor<I2C> {
27- /// # i2c: I2C,
28- /// # address: u8,
29- /// # }
30- /// # impl<I2C: I2c> Sensor<I2C> {
31- /// # pub const fn new(i2c: I2C, address: u8) -> Self {
32- /// # Self { i2c, address }
33- /// # }
34- /// # }
35- /// # type PressureSensor = Sensor;
36- /// # type TemperatureSensor = Sensor;
37- /// # pub struct I2c0;
38- /// # #[derive(Debug, Copy, Clone, Eq, PartialEq)]
39- /// # pub enum Error { }
40- /// # impl hali2c::Error for Error {
41- /// # fn kind(&self) -> hali2c::ErrorKind {
42- /// # ErrorKind::Other
43- /// # }
44- /// # }
45- /// # impl hali2c::ErrorType for I2c0 {
46- /// # type Error = Error;
47- /// # }
48- /// # impl I2c<SevenBitAddress> for I2c0 {
49- /// # fn transaction(&mut self, address: u8, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> {
50- /// # Ok(())
51- /// # }
52- /// # }
53- /// # struct Hal;
54- /// # impl Hal {
55- /// # fn i2c(&self) -> I2c0 {
56- /// # I2c0
57- /// # }
58- /// # }
59- /// # let hal = Hal;
60- ///
61- /// let i2c = hal.i2c();
62- /// let i2c_ref_cell = RefCell::new(i2c);
63- /// let mut temperature_sensor = TemperatureSensor::new(
64- /// i2c::RefCellDevice::new(&i2c_ref_cell),
65- /// 0x20,
66- /// );
67- /// let mut pressure_sensor = PressureSensor::new(
68- /// i2c::RefCellDevice::new(&i2c_ref_cell),
69- /// 0x42,
70- /// );
71- /// ```
7272 pub fn new ( bus : & ' a RefCell < T > ) -> Self {
7373 Self { bus }
7474 }
0 commit comments