Merge pull request #63 from xoviat/timer-trait

add delay trait
This commit is contained in:
Dario Nieuwenhuis 2021-03-02 15:57:33 +01:00 committed by GitHub
commit 7991b05e4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 2 deletions

View 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>;
}

View File

@ -5,6 +5,7 @@
#![feature(const_option)]
#![allow(incomplete_features)]
pub mod delay;
pub mod flash;
pub mod gpio;
pub mod uart;

View File

@ -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,

View File

@ -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;

View File

@ -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,
}
}