From 7ef81c75e75570c2e40ccd78a34e470a98ff4531 Mon Sep 17 00:00:00 2001 From: xoviat Date: Tue, 2 Mar 2021 08:45:22 -0600 Subject: [PATCH] traits: add delay trait delay allows downstream libraries to use async delay without depending on a specific delay implementation --- embassy-traits/src/delay.rs | 9 +++++++++ embassy-traits/src/lib.rs | 1 + embassy/src/executor/timer.rs | 24 ++++++++++++++++++++++++ embassy/src/lib.rs | 1 + embassy/src/time/duration.rs | 4 ++-- 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 embassy-traits/src/delay.rs diff --git a/embassy-traits/src/delay.rs b/embassy-traits/src/delay.rs new file mode 100644 index 00000000..b893ee4a --- /dev/null +++ b/embassy-traits/src/delay.rs @@ -0,0 +1,9 @@ +use core::future::Future; +use core::pin::Pin; + +pub trait Delay { + type DelayFuture<'a>: Future + 'a; + + fn delay_ms<'a>(self: Pin<&'a mut Self>, millis: u64) -> Self::DelayFuture<'a>; + fn delay_us<'a>(self: Pin<&'a mut Self>, micros: u64) -> Self::DelayFuture<'a>; +} diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs index 55af46cf..849bbaec 100644 --- a/embassy-traits/src/lib.rs +++ b/embassy-traits/src/lib.rs @@ -5,6 +5,7 @@ #![feature(const_option)] #![allow(incomplete_features)] +pub mod delay; pub mod flash; pub mod gpio; pub mod uart; diff --git a/embassy/src/executor/timer.rs b/embassy/src/executor/timer.rs index 9bd98925..256ecf66 100644 --- a/embassy/src/executor/timer.rs +++ b/embassy/src/executor/timer.rs @@ -1,4 +1,5 @@ use core::future::Future; +use core::marker::PhantomData; use core::pin::Pin; use core::task::{Context, Poll}; use futures::Stream; @@ -6,6 +7,29 @@ use futures::Stream; use super::raw; use crate::time::{Duration, Instant}; +pub struct Delay { + _data: PhantomData, +} + +impl Delay { + pub fn new() -> Self { + Delay { + _data: PhantomData {}, + } + } +} + +impl crate::traits::delay::Delay for Delay { + type DelayFuture<'a> = impl Future + 'a; + + fn delay_ms<'a>(self: Pin<&'a mut Self>, millis: u64) -> Self::DelayFuture<'a> { + Timer::after(Duration::from_millis(millis)) + } + fn delay_us<'a>(self: Pin<&'a mut Self>, micros: u64) -> Self::DelayFuture<'a> { + Timer::after(Duration::from_micros(micros)) + } +} + pub struct Timer { expires_at: Instant, yielded_once: bool, diff --git a/embassy/src/lib.rs b/embassy/src/lib.rs index f32a7a79..5e98736f 100644 --- a/embassy/src/lib.rs +++ b/embassy/src/lib.rs @@ -4,6 +4,7 @@ #![feature(const_fn_fn_ptr_basics)] #![feature(const_option)] #![allow(incomplete_features)] +#![feature(type_alias_impl_trait)] // This mod MUST go first, so that the others see its macros. pub(crate) mod fmt; diff --git a/embassy/src/time/duration.rs b/embassy/src/time/duration.rs index ba896060..e04afa18 100644 --- a/embassy/src/time/duration.rs +++ b/embassy/src/time/duration.rs @@ -45,9 +45,9 @@ impl Duration { /* NOTE: us delays may not be as accurate */ - pub const fn from_micros(millis: u64) -> Duration { + pub const fn from_micros(micros: u64) -> Duration { Duration { - ticks: millis * TICKS_PER_SECOND / 1_000_000, + ticks: micros * TICKS_PER_SECOND / 1_000_000, } }