|
1 | 1 | //! Asynchronous digital I/O
|
| 2 | +//! |
| 3 | +//! # Examples |
| 4 | +//! ```rust |
| 5 | +//! # use embedded_hal::futures::digital::AsyncInputPin; |
| 6 | +//! //! Asynchronously wait until the `ready_pin` becomes high. |
| 7 | +//! async fn wait_until_ready<P>(ready_pin: &P) |
| 8 | +//! where |
| 9 | +//! P: WaitFor, |
| 10 | +//! { |
| 11 | +//! ready_pin |
| 12 | +//! .wait_for_high() |
| 13 | +//! .await |
| 14 | +//! .expect("failed to await input pin") |
| 15 | +//! } |
| 16 | +//! ``` |
| 17 | +//! |
| 18 | +//! ```rust,ignore |
| 19 | +//! # use embedded_hal::futures::digital::WaitForHigh; |
| 20 | +//! # use embedded_hal::futures::delay::Delay; |
| 21 | +//! use core::time::Duration; |
| 22 | +//! |
| 23 | +//! //! Wait until the `ready_pin` is high or timeout after 1 millisecond. |
| 24 | +//! //! Returns true is the pin became high or false if it timed-out. |
| 25 | +//! async fn wait_until_ready_or_timeout<P, D>(ready_pin: &P, delay: &mut D) -> bool |
| 26 | +//! where |
| 27 | +//! P: WaitForHigh, |
| 28 | +//! D: Delay, |
| 29 | +//! { |
| 30 | +//! futures::select_biased! { |
| 31 | +//! x => ready_pin.wait_for_high() => { |
| 32 | +//! x.expect("failed to await input pin"); |
| 33 | +//! true |
| 34 | +//! }, |
| 35 | +//! _ => delay.delay(Duration::from_millis(1)) => false, // ignore the error |
| 36 | +//! } |
| 37 | +//! } |
| 38 | +//! ``` |
2 | 39 |
|
3 | 40 | use core::future::Future;
|
4 | 41 |
|
5 |
| -use crate::blocking::digital::InputPin; |
6 |
| - |
7 |
| -/// Asynchronously wait for a pin to become high or low. |
8 |
| -/// |
9 |
| -/// # Examples |
10 |
| -/// ```rust |
11 |
| -/// # use embedded_hal::futures::digital::AsyncInputPin; |
12 |
| -/// /// Asynchronously wait until the `ready_pin` becomes high. |
13 |
| -/// async fn wait_until_ready<P>(ready_pin: &P) |
14 |
| -/// where |
15 |
| -/// P: WaitFor, |
16 |
| -/// { |
17 |
| -/// ready_pin |
18 |
| -/// .wait_for_high() |
19 |
| -/// .await |
20 |
| -/// .expect("failed to await input pin") |
21 |
| -/// } |
22 |
| -/// ``` |
23 |
| -/// |
24 |
| -/// ```rust,ignore |
25 |
| -/// # use embedded_hal::futures::digital::AsyncInputPin; |
26 |
| -/// # use embedded_hal::futures::delay::Delay; |
27 |
| -/// use core::time::Duration; |
28 |
| -/// |
29 |
| -/// /// Wait until the `ready_pin` is high or timeout after 1 millisecond. |
30 |
| -/// /// Returns true is the pin became high or false if it timed-out. |
31 |
| -/// async fn wait_until_ready_or_timeout<P, D>(ready_pin: &P, delay: &mut D) -> bool |
32 |
| -/// where |
33 |
| -/// P: WaitFor, |
34 |
| -/// D: Delay, |
35 |
| -/// { |
36 |
| -/// futures::select_biased! { |
37 |
| -/// x => ready_pin.wait_for_high() => { |
38 |
| -/// x.expect("failed to await input pin"); |
39 |
| -/// true |
40 |
| -/// }, |
41 |
| -/// _ => delay.delay(Duration::from_millis(1)) => false, // ignore the error |
42 |
| -/// } |
43 |
| -/// } |
44 |
| -/// ``` |
45 |
| -pub trait WaitFor: InputPin { |
46 |
| - /// The future returned by the `until_high` function. |
47 |
| - type UntilHighFuture<'a>: Future<Output=Result<(), Self::Error>> + 'a; |
48 |
| - |
49 |
| - /// The future returned by the `until_low` function. |
50 |
| - type UntilLowFuture<'a>: Future<Output=Result<(), Self::Error>> + 'a; |
| 42 | +/// Asynchronously wait for a pin to be high. |
| 43 | +pub trait WaitForHigh { |
| 44 | + /// The future returned by the `wait_for_high` function. |
| 45 | + type WaitForHigh<'a>: Future<Output=()> + 'a |
| 46 | + where |
| 47 | + Self: 'a; |
51 | 48 |
|
52 | 49 | /// Returns a future that resolves when this pin _is_ high. If the pin
|
53 | 50 | /// is already high, the future resolves immediately.
|
54 | 51 | ///
|
55 | 52 | /// # Note for implementers
|
56 | 53 | /// The pin may have switched back to low before the task was run after
|
57 | 54 | /// being woken. The future should still resolve in that case.
|
58 |
| - fn wait_for_high<'a>(&'a mut self) -> Self::UntilHighFuture<'a>; |
| 55 | + fn wait_for_high<'a>(&'a mut self) -> Self::WaitForHigh<'a>; |
| 56 | +} |
| 57 | + |
| 58 | +/// Asynchronously wait for a pin to be low. |
| 59 | +pub trait WaitForLow { |
| 60 | + /// The future returned by `wait_for_low`. |
| 61 | + type WaitForLow<'a>: Future<Output = ()> + 'a |
| 62 | + where |
| 63 | + Self: 'a; |
59 | 64 |
|
60 |
| - /// Returns a future that resolves when this pin becomes low. |
| 65 | + /// Returns a future that resolves when this pin _is_ low. If the pin |
| 66 | + /// is already low, the future resolves immediately. |
61 | 67 | ///
|
62 | 68 | /// # Note for implementers
|
63 | 69 | /// The pin may have switched back to high before the task was run after
|
64 | 70 | /// being woken. The future should still resolve in that case.
|
65 |
| - fn wait_for_low<'a>(&'a mut self) -> Self::UntilLowFuture<'a>; |
| 71 | + fn wait_for_low<'a>(&'a mut self) -> Self::WaitForLow<'a>; |
| 72 | +} |
| 73 | + |
| 74 | +/// Wait for a rising edge (transition from low to high). |
| 75 | +pub trait WaitForRisingEdge { |
| 76 | + /// The future returned from `wait_for_rising_edge`. |
| 77 | + type WaitForRisingEdgeFuture<'a>: Future<Output = ()> + 'a |
| 78 | + where |
| 79 | + Self: 'a; |
| 80 | + |
| 81 | + /// Returns a future that resolves when this pin transitions from low to high. |
| 82 | + fn wait_for_rising_edge<'a>(&'a mut self) -> Self::WaitForRisingEdgeFuture<'a>; |
| 83 | +} |
| 84 | + |
| 85 | +/// Wait for a falling edge (transition from high to low). |
| 86 | +pub trait WaitForFallingEdge { |
| 87 | + /// The future returned from `wait_for_falling_edge`. |
| 88 | + type WaitForFallingEdgeFuture<'a>: Future<Output = ()> + 'a |
| 89 | + where |
| 90 | + Self: 'a; |
| 91 | + |
| 92 | + /// Returns a future that resolves when this pin transitions from high to low. |
| 93 | + fn wait_for_falling_edge<'a>(&'a mut self) -> Self::WaitForFallingEdgeFuture<'a>; |
| 94 | +} |
| 95 | + |
| 96 | +/// Wait for any edge (transition from low to high OR high to low). |
| 97 | +pub trait WaitForAnyEdge { |
| 98 | + /// The future returned from `wait_for_any_edge`. |
| 99 | + type WaitForAnyEdgeFuture<'a>: Future<Output = ()> + 'a |
| 100 | + where |
| 101 | + Self: 'a; |
| 102 | + |
| 103 | + /// Returns a future that resolves when this pin undergoes any transition, e.g. |
| 104 | + /// low to high OR high to low. |
| 105 | + fn wait_for_any_edge<'a>(&'a mut self) -> Self::WaitForAnyEdgeFuture<'a>; |
66 | 106 | }
|
0 commit comments