Skip to content

Commit 1439504

Browse files
Merge #458
458: bus: Add a doc example for i2c::RefCellDevice r=Dirbaio a=tomgilligan `embedded-hal-bus` is turning out to be very useful for something I'm working on. It wasn't too hard to work out how to use it but it feels like an example would still be helpful here. Not sure: - is this the best place to put such an example? - how much of the example should be hidden with `#`? Co-authored-by: Thomas Gilligan <[email protected]>
2 parents 3a7c3ca + 6a0c442 commit 1439504

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

embedded-hal-bus/src/i2c/refcell.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,63 @@ 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 fn new(i2c: I2C, address: u8) -> Self {
26+
/// # Self { i2c, address }
27+
/// # }
28+
/// # }
29+
/// # type PressureSensor<I2C> = Sensor<I2C>;
30+
/// # type TemperatureSensor<I2C> = Sensor<I2C>;
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+
/// ```
966
pub struct RefCellDevice<'a, T> {
1067
bus: &'a RefCell<T>,
1168
}

0 commit comments

Comments
 (0)