diff options
| author | xoviat <[email protected]> | 2021-03-02 08:45:22 -0600 |
|---|---|---|
| committer | xoviat <[email protected]> | 2021-03-02 08:45:22 -0600 |
| commit | 7ef81c75e75570c2e40ccd78a34e470a98ff4531 (patch) | |
| tree | 13f57a12ae4d6167ce2118efd8790806907c32c6 | |
| parent | 3e4abe9e9d9a8101d8555d4e39bc613513484924 (diff) | |
traits: add delay trait
delay allows downstream libraries
to use async delay without depending
on a specific delay implementation
| -rw-r--r-- | embassy-traits/src/delay.rs | 9 | ||||
| -rw-r--r-- | embassy-traits/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy/src/executor/timer.rs | 24 | ||||
| -rw-r--r-- | embassy/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy/src/time/duration.rs | 4 |
5 files changed, 37 insertions, 2 deletions
diff --git a/embassy-traits/src/delay.rs b/embassy-traits/src/delay.rs new file mode 100644 index 000000000..b893ee4a3 --- /dev/null +++ b/embassy-traits/src/delay.rs | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | use core::pin::Pin; | ||
| 3 | |||
| 4 | pub trait Delay { | ||
| 5 | type DelayFuture<'a>: Future<Output = ()> + 'a; | ||
| 6 | |||
| 7 | fn delay_ms<'a>(self: Pin<&'a mut Self>, millis: u64) -> Self::DelayFuture<'a>; | ||
| 8 | fn delay_us<'a>(self: Pin<&'a mut Self>, micros: u64) -> Self::DelayFuture<'a>; | ||
| 9 | } | ||
diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs index 55af46cff..849bbaec3 100644 --- a/embassy-traits/src/lib.rs +++ b/embassy-traits/src/lib.rs | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #![feature(const_option)] | 5 | #![feature(const_option)] |
| 6 | #![allow(incomplete_features)] | 6 | #![allow(incomplete_features)] |
| 7 | 7 | ||
| 8 | pub mod delay; | ||
| 8 | pub mod flash; | 9 | pub mod flash; |
| 9 | pub mod gpio; | 10 | pub mod gpio; |
| 10 | pub mod uart; | 11 | pub mod uart; |
diff --git a/embassy/src/executor/timer.rs b/embassy/src/executor/timer.rs index 9bd98925c..256ecf665 100644 --- a/embassy/src/executor/timer.rs +++ b/embassy/src/executor/timer.rs | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::marker::PhantomData; | ||
| 2 | use core::pin::Pin; | 3 | use core::pin::Pin; |
| 3 | use core::task::{Context, Poll}; | 4 | use core::task::{Context, Poll}; |
| 4 | use futures::Stream; | 5 | use futures::Stream; |
| @@ -6,6 +7,29 @@ use futures::Stream; | |||
| 6 | use super::raw; | 7 | use super::raw; |
| 7 | use crate::time::{Duration, Instant}; | 8 | use crate::time::{Duration, Instant}; |
| 8 | 9 | ||
| 10 | pub struct Delay { | ||
| 11 | _data: PhantomData<bool>, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl Delay { | ||
| 15 | pub fn new() -> Self { | ||
| 16 | Delay { | ||
| 17 | _data: PhantomData {}, | ||
| 18 | } | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | impl crate::traits::delay::Delay for Delay { | ||
| 23 | type DelayFuture<'a> = impl Future<Output = ()> + 'a; | ||
| 24 | |||
| 25 | fn delay_ms<'a>(self: Pin<&'a mut Self>, millis: u64) -> Self::DelayFuture<'a> { | ||
| 26 | Timer::after(Duration::from_millis(millis)) | ||
| 27 | } | ||
| 28 | fn delay_us<'a>(self: Pin<&'a mut Self>, micros: u64) -> Self::DelayFuture<'a> { | ||
| 29 | Timer::after(Duration::from_micros(micros)) | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 9 | pub struct Timer { | 33 | pub struct Timer { |
| 10 | expires_at: Instant, | 34 | expires_at: Instant, |
| 11 | yielded_once: bool, | 35 | yielded_once: bool, |
diff --git a/embassy/src/lib.rs b/embassy/src/lib.rs index f32a7a795..5e98736f9 100644 --- a/embassy/src/lib.rs +++ b/embassy/src/lib.rs | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | #![feature(const_fn_fn_ptr_basics)] | 4 | #![feature(const_fn_fn_ptr_basics)] |
| 5 | #![feature(const_option)] | 5 | #![feature(const_option)] |
| 6 | #![allow(incomplete_features)] | 6 | #![allow(incomplete_features)] |
| 7 | #![feature(type_alias_impl_trait)] | ||
| 7 | 8 | ||
| 8 | // This mod MUST go first, so that the others see its macros. | 9 | // This mod MUST go first, so that the others see its macros. |
| 9 | pub(crate) mod fmt; | 10 | pub(crate) mod fmt; |
diff --git a/embassy/src/time/duration.rs b/embassy/src/time/duration.rs index ba8960606..e04afa184 100644 --- a/embassy/src/time/duration.rs +++ b/embassy/src/time/duration.rs | |||
| @@ -45,9 +45,9 @@ impl Duration { | |||
| 45 | /* | 45 | /* |
| 46 | NOTE: us delays may not be as accurate | 46 | NOTE: us delays may not be as accurate |
| 47 | */ | 47 | */ |
| 48 | pub const fn from_micros(millis: u64) -> Duration { | 48 | pub const fn from_micros(micros: u64) -> Duration { |
| 49 | Duration { | 49 | Duration { |
| 50 | ticks: millis * TICKS_PER_SECOND / 1_000_000, | 50 | ticks: micros * TICKS_PER_SECOND / 1_000_000, |
| 51 | } | 51 | } |
| 52 | } | 52 | } |
| 53 | 53 | ||
