diff options
| author | Quentin Smith <[email protected]> | 2023-07-17 21:31:43 -0400 |
|---|---|---|
| committer | Quentin Smith <[email protected]> | 2023-07-17 21:31:43 -0400 |
| commit | 6f02403184eb7fb7990fb88fc9df9c4328a690a3 (patch) | |
| tree | 748f510e190bb2724750507a6e69ed1a8e08cb20 /embassy-time/src/timer.rs | |
| parent | d896f80405aa8963877049ed999e4aba25d6e2bb (diff) | |
| parent | 6b5df4523aa1c4902f02e803450ae4b418e0e3ca (diff) | |
Merge remote-tracking branch 'origin/main' into nrf-pdm
Diffstat (limited to 'embassy-time/src/timer.rs')
| -rw-r--r-- | embassy-time/src/timer.rs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index bd791b817..d3d1f9f5f 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::{poll_fn, Future}; |
| 2 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 3 | use core::task::{Context, Poll, Waker}; | 3 | use core::task::{Context, Poll, Waker}; |
| 4 | 4 | ||
| @@ -26,6 +26,7 @@ pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Out | |||
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | /// A future that completes at a specified [Instant](struct.Instant.html). | 28 | /// A future that completes at a specified [Instant](struct.Instant.html). |
| 29 | #[must_use = "futures do nothing unless you `.await` or poll them"] | ||
| 29 | pub struct Timer { | 30 | pub struct Timer { |
| 30 | expires_at: Instant, | 31 | expires_at: Instant, |
| 31 | yielded_once: bool, | 32 | yielded_once: bool, |
| @@ -108,7 +109,6 @@ impl Future for Timer { | |||
| 108 | /// # #![feature(type_alias_impl_trait)] | 109 | /// # #![feature(type_alias_impl_trait)] |
| 109 | /// # | 110 | /// # |
| 110 | /// use embassy_time::{Duration, Ticker}; | 111 | /// use embassy_time::{Duration, Ticker}; |
| 111 | /// use futures::StreamExt; | ||
| 112 | /// # fn foo(){} | 112 | /// # fn foo(){} |
| 113 | /// | 113 | /// |
| 114 | /// #[embassy_executor::task] | 114 | /// #[embassy_executor::task] |
| @@ -131,6 +131,20 @@ impl Ticker { | |||
| 131 | let expires_at = Instant::now() + duration; | 131 | let expires_at = Instant::now() + duration; |
| 132 | Self { expires_at, duration } | 132 | Self { expires_at, duration } |
| 133 | } | 133 | } |
| 134 | |||
| 135 | /// Waits for the next tick | ||
| 136 | pub fn next(&mut self) -> impl Future<Output = ()> + '_ { | ||
| 137 | poll_fn(|cx| { | ||
| 138 | if self.expires_at <= Instant::now() { | ||
| 139 | let dur = self.duration; | ||
| 140 | self.expires_at += dur; | ||
| 141 | Poll::Ready(()) | ||
| 142 | } else { | ||
| 143 | schedule_wake(self.expires_at, cx.waker()); | ||
| 144 | Poll::Pending | ||
| 145 | } | ||
| 146 | }) | ||
| 147 | } | ||
| 134 | } | 148 | } |
| 135 | 149 | ||
| 136 | impl Unpin for Ticker {} | 150 | impl Unpin for Ticker {} |
