From 663fa2addd30463328f6186e14f98680b4a35c9b Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Mon, 11 Dec 2023 13:27:55 +0100 Subject: Introduce reset_{at|after} functions for Ticker. --- embassy-time/src/timer.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 574d715da..3444d3e24 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -184,6 +184,16 @@ impl Ticker { self.expires_at = Instant::now() + self.duration; } + /// Reset the ticker to fire for the next time on the deadline. + pub fn reset_at(&mut self, deadline: Instant) { + self.expires_at = deadline; + } + + /// Resets the ticker, after the specified duration has passed. + pub fn reset_after(&mut self, after: Duration) { + self.expires_at = Instant::now() + after; + } + /// Waits for the next tick. pub fn next(&mut self) -> impl Future + '_ { poll_fn(|cx| { -- cgit From 8707462ec23807782796fbac4295bc5bce9ff136 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Mon, 11 Dec 2023 16:11:57 +0100 Subject: Adjusted documentation and reset_after behaviour. --- embassy-time/src/timer.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 3444d3e24..fe0e93951 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -185,13 +185,15 @@ impl Ticker { } /// Reset the ticker to fire for the next time on the deadline. + /// If the deadline is in the past, the ticker will fire instantly. pub fn reset_at(&mut self, deadline: Instant) { self.expires_at = deadline; } /// Resets the ticker, after the specified duration has passed. + /// If the specified duration is zero, the next tick will be after the duration of the ticker. pub fn reset_after(&mut self, after: Duration) { - self.expires_at = Instant::now() + after; + self.expires_at = Instant::now() + after + self.duration; } /// Waits for the next tick. -- cgit From 8b36a32ed5d834b23e970d5b723dd7df1f1c94a2 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 21 Dec 2023 14:57:49 +0100 Subject: ci: use beta, add secondary nightly ci. --- embassy-time/src/timer.rs | 7 ------- 1 file changed, 7 deletions(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 574d715da..2705ba03f 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -47,9 +47,6 @@ impl Timer { /// /// Example: /// ``` no_run - /// # #![feature(type_alias_impl_trait)] - /// # - /// # fn foo() {} /// use embassy_time::{Duration, Timer}; /// /// #[embassy_executor::task] @@ -132,8 +129,6 @@ impl Future for Timer { /// /// For instance, consider the following code fragment. /// ``` no_run -/// # #![feature(type_alias_impl_trait)] -/// # /// use embassy_time::{Duration, Timer}; /// # fn foo() {} /// @@ -152,8 +147,6 @@ impl Future for Timer { /// Example using ticker, which will consistently call `foo` once a second. /// /// ``` no_run -/// # #![feature(type_alias_impl_trait)] -/// # /// use embassy_time::{Duration, Ticker}; /// # fn foo(){} /// -- cgit From f0606da9adc8032cc92c06c0661b385742459fc8 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 11 Jan 2024 22:47:05 +0100 Subject: time: split queue driver too, don't reexport drivers. --- embassy-time/src/timer.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 2705ba03f..565a65cb8 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -1,6 +1,6 @@ use core::future::{poll_fn, Future}; use core::pin::Pin; -use core::task::{Context, Poll, Waker}; +use core::task::{Context, Poll}; use futures_util::future::{select, Either}; use futures_util::stream::FusedStream; @@ -116,7 +116,7 @@ impl Future for Timer { if self.yielded_once && self.expires_at <= Instant::now() { Poll::Ready(()) } else { - schedule_wake(self.expires_at, cx.waker()); + embassy_time_queue_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker()); self.yielded_once = true; Poll::Pending } @@ -185,7 +185,7 @@ impl Ticker { self.expires_at += dur; Poll::Ready(()) } else { - schedule_wake(self.expires_at, cx.waker()); + embassy_time_queue_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker()); Poll::Pending } }) @@ -202,7 +202,7 @@ impl Stream for Ticker { self.expires_at += dur; Poll::Ready(Some(())) } else { - schedule_wake(self.expires_at, cx.waker()); + embassy_time_queue_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker()); Poll::Pending } } @@ -214,11 +214,3 @@ impl FusedStream for Ticker { false } } - -extern "Rust" { - fn _embassy_time_schedule_wake(at: Instant, waker: &Waker); -} - -fn schedule_wake(at: Instant, waker: &Waker) { - unsafe { _embassy_time_schedule_wake(at, waker) } -} -- cgit From 1e698af05bc6e7e520d3f35ef661f34ea6ea359e Mon Sep 17 00:00:00 2001 From: Caleb Jamison Date: Wed, 31 Jan 2024 14:04:48 -0500 Subject: Add timeout_at convenience function and example. --- embassy-time/src/timer.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 565a65cb8..dbce9297c 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -8,7 +8,7 @@ use futures_util::{pin_mut, Stream}; use crate::{Duration, Instant}; -/// Error returned by [`with_timeout`] on timeout. +/// Error returned by [`with_timeout`] and [`timeout_at`] on timeout. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct TimeoutError; @@ -26,6 +26,19 @@ pub async fn with_timeout(timeout: Duration, fut: F) -> Result(at: Instant, fut: F) -> Result { + let timeout_fut = Timer::at(at); + pin_mut!(fut); + match select(fut, timeout_fut).await { + Either::Left((r, _)) => Ok(r), + Either::Right(_) => Err(TimeoutError), + } +} + /// A future that completes at a specified [Instant](struct.Instant.html). #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct Timer { -- cgit From 8b7d85619537fc20ad7ad533433d84ba4975ddc4 Mon Sep 17 00:00:00 2001 From: Caleb Jamison Date: Wed, 31 Jan 2024 16:26:11 -0500 Subject: Rename timeout_at to with_deadline --- embassy-time/src/timer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index dbce9297c..bcd6bf4f7 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -30,7 +30,7 @@ pub async fn with_timeout(timeout: Duration, fut: F) -> Result(at: Instant, fut: F) -> Result { +pub async fn with_deadline(at: Instant, fut: F) -> Result { let timeout_fut = Timer::at(at); pin_mut!(fut); match select(fut, timeout_fut).await { -- cgit From 0ab0b5590a0b07756ceaed9b26c78a6a821b34d5 Mon Sep 17 00:00:00 2001 From: Caleb Jamison Date: Wed, 31 Jan 2024 16:28:06 -0500 Subject: Fixup docs --- embassy-time/src/timer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index bcd6bf4f7..daa4c1699 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -8,7 +8,7 @@ use futures_util::{pin_mut, Stream}; use crate::{Duration, Instant}; -/// Error returned by [`with_timeout`] and [`timeout_at`] on timeout. +/// Error returned by [`with_timeout`] and [`with_deadline`] on timeout. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct TimeoutError; -- cgit From 2ea1040e073a54d2a97f75c7d68d0d9a230d15c8 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Fri, 22 Mar 2024 08:50:56 +0100 Subject: Adjusted behavior. --- embassy-time/src/timer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 763bfdeeb..d6d0a46e2 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -190,10 +190,10 @@ impl Ticker { self.expires_at = Instant::now() + self.duration; } - /// Reset the ticker to fire for the next time on the deadline. + /// Reset the ticker at the deadline. /// If the deadline is in the past, the ticker will fire instantly. pub fn reset_at(&mut self, deadline: Instant) { - self.expires_at = deadline; + self.expires_at = deadline + self.duration; } /// Resets the ticker, after the specified duration has passed. -- cgit