embedded_hal_async/
digital.rs

1//! Asynchronous digital I/O.
2//!
3//! # Example
4//!
5//! ```rust
6//! # use embedded_hal_async::digital::Wait;
7//! /// Asynchronously wait until the `ready_pin` becomes high.
8//! async fn wait_until_ready<P>(ready_pin: &mut P)
9//! where
10//!     P: Wait,
11//! {
12//!     ready_pin
13//!         .wait_for_high()
14//!         .await
15//!         .expect("failed to await input pin")
16//! }
17//! ```
18
19/// Asynchronously wait for GPIO pin state.
20pub trait Wait: embedded_hal::digital::ErrorType {
21    /// Wait until the pin is high. If it is already high, return immediately.
22    ///
23    /// # Note for implementers
24    /// The pin may have switched back to low before the task was run after
25    /// being woken. The future should still resolve in that case.
26    async fn wait_for_high(&mut self) -> Result<(), Self::Error>;
27
28    /// Wait until the pin is low. If it is already low, return immediately.
29    ///
30    /// # Note for implementers
31    /// The pin may have switched back to high before the task was run after
32    /// being woken. The future should still resolve in that case.
33    async fn wait_for_low(&mut self) -> Result<(), Self::Error>;
34
35    /// Wait for the pin to undergo a transition from low to high.
36    ///
37    /// If the pin is already high, this does *not* return immediately, it'll wait for the
38    /// pin to go low and then high again.
39    async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error>;
40
41    /// Wait for the pin to undergo a transition from high to low.
42    ///
43    /// If the pin is already low, this does *not* return immediately, it'll wait for the
44    /// pin to go high and then low again.
45    async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error>;
46
47    /// Wait for the pin to undergo any transition, i.e low to high OR high to low.
48    async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error>;
49}
50
51impl<T: Wait + ?Sized> Wait for &mut T {
52    #[inline]
53    async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
54        T::wait_for_high(self).await
55    }
56
57    #[inline]
58    async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
59        T::wait_for_low(self).await
60    }
61
62    #[inline]
63    async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
64        T::wait_for_rising_edge(self).await
65    }
66
67    #[inline]
68    async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
69        T::wait_for_falling_edge(self).await
70    }
71
72    #[inline]
73    async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
74        T::wait_for_any_edge(self).await
75    }
76}