diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-12-13 19:54:18 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2020-12-13 19:54:18 +0100 |
| commit | 3be7ace878a7ded8fa30709d91d358ea41cf6b49 (patch) | |
| tree | 17836e094eec1a26a4f76888615ffbbb05e7af75 | |
| parent | ca2ff632bad91acb4b890aed6e05a35ca1927ee1 (diff) | |
add time::Ticker
| -rw-r--r-- | embassy/src/time/mod.rs | 2 | ||||
| -rw-r--r-- | embassy/src/time/timer.rs | 33 |
2 files changed, 34 insertions, 1 deletions
diff --git a/embassy/src/time/mod.rs b/embassy/src/time/mod.rs index d6a9a3013..2b4631557 100644 --- a/embassy/src/time/mod.rs +++ b/embassy/src/time/mod.rs | |||
| @@ -5,7 +5,7 @@ mod traits; | |||
| 5 | 5 | ||
| 6 | pub use duration::Duration; | 6 | pub use duration::Duration; |
| 7 | pub use instant::Instant; | 7 | pub use instant::Instant; |
| 8 | pub use timer::Timer; | 8 | pub use timer::{Ticker, Timer}; |
| 9 | pub use traits::*; | 9 | pub use traits::*; |
| 10 | 10 | ||
| 11 | use crate::fmt::*; | 11 | use crate::fmt::*; |
diff --git a/embassy/src/time/timer.rs b/embassy/src/time/timer.rs index 109563008..8756368c7 100644 --- a/embassy/src/time/timer.rs +++ b/embassy/src/time/timer.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 3 | use core::task::{Context, Poll}; | 3 | use core::task::{Context, Poll}; |
| 4 | use futures::Stream; | ||
| 4 | use futures_intrusive::timer::{LocalTimer, LocalTimerFuture}; | 5 | use futures_intrusive::timer::{LocalTimer, LocalTimerFuture}; |
| 5 | 6 | ||
| 6 | use super::{Duration, Instant}; | 7 | use super::{Duration, Instant}; |
| @@ -28,3 +29,35 @@ impl Future for Timer { | |||
| 28 | unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }.poll(cx) | 29 | unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }.poll(cx) |
| 29 | } | 30 | } |
| 30 | } | 31 | } |
| 32 | |||
| 33 | pub struct Ticker { | ||
| 34 | inner: LocalTimerFuture<'static>, | ||
| 35 | next: Instant, | ||
| 36 | dur: Duration, | ||
| 37 | } | ||
| 38 | |||
| 39 | impl Ticker { | ||
| 40 | pub fn every(dur: Duration) -> Self { | ||
| 41 | let next = Instant::now() + dur; | ||
| 42 | Self { | ||
| 43 | inner: current_timer_queue().deadline(next.as_ticks()), | ||
| 44 | next, | ||
| 45 | dur, | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | impl Stream for Ticker { | ||
| 51 | type Item = (); | ||
| 52 | fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| 53 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 54 | match unsafe { Pin::new_unchecked(&mut this.inner) }.poll(cx) { | ||
| 55 | Poll::Ready(_) => { | ||
| 56 | this.next += this.dur; | ||
| 57 | this.inner = current_timer_queue().deadline(this.next.as_ticks()); | ||
| 58 | Poll::Ready(Some(())) | ||
| 59 | } | ||
| 60 | Poll::Pending => Poll::Pending, | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
