traits: add delay trait
delay allows downstream libraries to use async delay without depending on a specific delay implementation
This commit is contained in:
parent
3e4abe9e9d
commit
7ef81c75e7
9
embassy-traits/src/delay.rs
Normal file
9
embassy-traits/src/delay.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
|
||||
pub trait Delay {
|
||||
type DelayFuture<'a>: Future<Output = ()> + '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>;
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
#![feature(const_option)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
pub mod delay;
|
||||
pub mod flash;
|
||||
pub mod gpio;
|
||||
pub mod uart;
|
||||
|
@ -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<bool>,
|
||||
}
|
||||
|
||||
impl Delay {
|
||||
pub fn new() -> Self {
|
||||
Delay {
|
||||
_data: PhantomData {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::traits::delay::Delay for Delay {
|
||||
type DelayFuture<'a> = impl Future<Output = ()> + '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,
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user