embedded_interfaces/
i2c.rs

1#[maybe_async_cfg::maybe(
2    idents(hal(sync = "embedded_hal", async = "embedded_hal_async")),
3    sync(feature = "sync"),
4    async(feature = "async")
5)]
6/// This represents a specific device bound to an I2C bus.
7pub struct I2cBoundBus<I, A>
8where
9    I: hal::i2c::I2c<A> + hal::i2c::ErrorType,
10    A: hal::i2c::AddressMode + Copy,
11{
12    /// I2c interface
13    pub interface: I,
14    /// Device address
15    pub address: A,
16}
17
18#[maybe_async_cfg::maybe(
19    idents(hal(sync = "embedded_hal", async = "embedded_hal_async"), Codec, I2cBoundBus),
20    sync(feature = "sync"),
21    async(feature = "async")
22)]
23/// This represents an I2C device on an I2C bus.
24pub struct I2cDevice<I, A>
25where
26    I: hal::i2c::I2c<A> + hal::i2c::ErrorType,
27    A: hal::i2c::AddressMode + Copy,
28{
29    /// I2c interface and device address
30    pub bound_bus: I2cBoundBus<I, A>,
31}
32
33#[maybe_async_cfg::maybe(
34    idents(hal(sync = "embedded_hal", async = "embedded_hal_async"), Codec, I2cBoundBus),
35    sync(feature = "sync"),
36    async(feature = "async")
37)]
38impl<I, A> I2cDevice<I, A>
39where
40    I: hal::i2c::I2c<A> + hal::i2c::ErrorType,
41    A: hal::i2c::AddressMode + Copy,
42{
43    /// Create a new I2cDevice from an interface and device address
44    pub fn new(interface: I, address: A) -> Self {
45        Self {
46            bound_bus: I2cBoundBus { interface, address },
47        }
48    }
49}