embedded_platform/i2c/
initialize.rs

1//! Defines futures for initializing an I²C peripheral based off of GPIO pins.
2use core::future;
3use core::pin;
4use core::task;
5
6/// A future which initializes an I²C peripheral based off of GPIO pins.
7#[derive(Debug)]
8#[must_use = "futures do nothing unless you `.await` or poll them"]
9pub struct Initialize<A, SDA, SCL>
10where
11    A: super::I2cBusMapping<SDA, SCL> + Unpin,
12    SDA: Unpin,
13    SCL: Unpin,
14{
15    mapping: A,
16    sda: SDA,
17    scl: SCL,
18}
19
20/// Creates a new [`Initialize`] based off of a I²C bus pin mapping, as well as an SDA and SCL pin.
21pub fn initialize<A, SDA, SCL>(mapping: A, sda: SDA, scl: SCL) -> Initialize<A, SDA, SCL>
22where
23    A: super::I2cBusMapping<SDA, SCL> + Unpin,
24    SDA: Unpin,
25    SCL: Unpin,
26{
27    Initialize { mapping, sda, scl }
28}
29
30impl<A, SDA, SCL> future::Future for Initialize<A, SDA, SCL>
31where
32    A: super::I2cBusMapping<SDA, SCL> + Unpin,
33    SDA: Unpin,
34    SCL: Unpin,
35{
36    type Output = Result<A::Bus, A::Error>;
37
38    fn poll(mut self: pin::Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
39        let this = &mut *self;
40        pin::Pin::new(&mut this.mapping).poll_initialize(cx, &mut this.sda, &mut this.scl)
41    }
42}