|
| 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 | +use core::future::Future; |
| 20 | + |
| 21 | +/// Asynchronously wait for GPIO pin state. |
| 22 | +pub trait Wait: embedded_hal::digital::ErrorType { |
| 23 | + /// The future returned by the `wait_for_high` function. |
| 24 | + type WaitForHighFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 25 | + where |
| 26 | + Self: 'a; |
| 27 | + |
| 28 | + /// Wait until the pin is high. If it is already high, return immediately. |
| 29 | + /// |
| 30 | + /// # Note for implementers |
| 31 | + /// The pin may have switched back to low before the task was run after |
| 32 | + /// being woken. The future should still resolve in that case. |
| 33 | + fn wait_for_high<'a>(&'a mut self) -> Self::WaitForHighFuture<'a>; |
| 34 | + |
| 35 | + /// The future returned by `wait_for_low`. |
| 36 | + type WaitForLowFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 37 | + where |
| 38 | + Self: 'a; |
| 39 | + |
| 40 | + /// Wait until the pin is low. If it is already low, return immediately. |
| 41 | + /// |
| 42 | + /// # Note for implementers |
| 43 | + /// The pin may have switched back to high before the task was run after |
| 44 | + /// being woken. The future should still resolve in that case. |
| 45 | + fn wait_for_low<'a>(&'a mut self) -> Self::WaitForLowFuture<'a>; |
| 46 | + |
| 47 | + /// The future returned from `wait_for_rising_edge`. |
| 48 | + type WaitForRisingEdgeFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 49 | + where |
| 50 | + Self: 'a; |
| 51 | + |
| 52 | + /// Wait for the pin to undergo a transition from low to high. |
| 53 | + /// |
| 54 | + /// If the pin is already low, this does *not* return immediately, it'll wait for the |
| 55 | + /// pin to go high and then low again. |
| 56 | + fn wait_for_rising_edge<'a>(&'a mut self) -> Self::WaitForRisingEdgeFuture<'a>; |
| 57 | + |
| 58 | + /// The future returned from `wait_for_falling_edge`. |
| 59 | + type WaitForFallingEdgeFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 60 | + where |
| 61 | + Self: 'a; |
| 62 | + |
| 63 | + /// Wait for the pin to undergo a transition from high to low. |
| 64 | + /// |
| 65 | + /// If the pin is already low, this does *not* return immediately, it'll wait for the |
| 66 | + /// pin to go high and then low again. |
| 67 | + fn wait_for_falling_edge<'a>(&'a mut self) -> Self::WaitForFallingEdgeFuture<'a>; |
| 68 | + |
| 69 | + /// The future returned from `wait_for_any_edge`. |
| 70 | + type WaitForAnyEdgeFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 71 | + where |
| 72 | + Self: 'a; |
| 73 | + |
| 74 | + /// Wait for the pin to undergo any transition, i.e low to high OR high to low. |
| 75 | + fn wait_for_any_edge<'a>(&'a mut self) -> Self::WaitForAnyEdgeFuture<'a>; |
| 76 | +} |
0 commit comments