Skip to content

Commit 48ab3a8

Browse files
committed
Switch futures::Delay to take core::time::Duration and add futures::digital docs
1 parent 41af589 commit 48ab3a8

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/futures/delay.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! The `Delay` trait provides an asynchronous delay abstraction and it's meant to be used either
66
//! to build higher-level abstractions like I/O timeouts or by itself.
77
8-
use core::future::Future;
8+
use core::{future::Future, time::Duration};
99

1010
/// Asynchronously wait a duration of time.
1111
pub trait Delay {
@@ -17,11 +17,8 @@ pub trait Delay {
1717
where
1818
Self: 'a;
1919

20-
/// The unit of time used by this delay timer.
21-
type Time;
22-
2320
/// Returns a future that will resolve when `duration` has passed.
2421
/// It is not guaranteed that _exactly_ `duration` will pass, but it will
2522
/// be `duration` or longer.
26-
fn delay<'a>(&mut self, duration: impl Into<Self::Time>) -> Self::DelayFuture<'a>;
23+
fn delay<'a>(&mut self, duration: Duration) -> Self::DelayFuture<'a>;
2724
}

src/futures/digital.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,40 @@
22
33
use core::future::Future;
44

5+
use crate::blocking::digital::InputPin;
6+
57
/// Asynchronously wait for a pin to become high or low.
6-
pub trait AsyncInputPin {
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: AsyncInputPin,
16+
/// {
17+
/// ready_pin.until_high().await
18+
/// }
19+
/// ```
20+
///
21+
/// ```rust,ignore
22+
/// # use embedded_hal::futures::digital::AsyncInputPin;
23+
/// # use embedded_hal::futures::delay::Delay;
24+
/// # use core::time::Duration;
25+
/// /// Wait until the `ready_pin` is high or timeout after 1 millisecond.
26+
/// /// Returns true is the pin became high or false if it timed-out.
27+
/// async fn wait_until_ready_or_timeout<P, D>(ready_pin: &P, delay: &mut D) -> bool
28+
/// where
29+
/// P: AsyncInputPin,
30+
/// D: Delay,
31+
/// {
32+
/// futures::select_biased! {
33+
/// _ => ready_pin.until_high() => true,
34+
/// _ => delay.delay(Duration::from_millis(1)) => false,
35+
/// }
36+
/// }
37+
/// ```
38+
pub trait AsyncInputPin: InputPin {
739
/// The future returned by the `until_high` function.
840
type UntilHighFuture<'a>: Future<Output=()> + 'a;
941

0 commit comments

Comments
 (0)