diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-03-28 19:04:26 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-28 19:04:26 +0000 |
| commit | 81cf9d1143d30dd7dbcbeb7d5aa6ae4aa5db8a1a (patch) | |
| tree | 34e8a69704993d21746a79ffe622f88aa3f900b6 | |
| parent | 393de9e19f12528a11ba98658c29d4fb43f75fa0 (diff) | |
| parent | 2ea1040e073a54d2a97f75c7d68d0d9a230d15c8 (diff) | |
Merge pull request #2295 from Frostie314159/reset-at-after
Introduce `reset_{at|after}` functions for ticker.
| -rw-r--r-- | embassy-time/src/timer.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index daa4c1699..d6d0a46e2 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -190,6 +190,18 @@ impl Ticker { | |||
| 190 | self.expires_at = Instant::now() + self.duration; | 190 | self.expires_at = Instant::now() + self.duration; |
| 191 | } | 191 | } |
| 192 | 192 | ||
| 193 | /// Reset the ticker at the deadline. | ||
| 194 | /// If the deadline is in the past, the ticker will fire instantly. | ||
| 195 | pub fn reset_at(&mut self, deadline: Instant) { | ||
| 196 | self.expires_at = deadline + self.duration; | ||
| 197 | } | ||
| 198 | |||
| 199 | /// Resets the ticker, after the specified duration has passed. | ||
| 200 | /// If the specified duration is zero, the next tick will be after the duration of the ticker. | ||
| 201 | pub fn reset_after(&mut self, after: Duration) { | ||
| 202 | self.expires_at = Instant::now() + after + self.duration; | ||
| 203 | } | ||
| 204 | |||
| 193 | /// Waits for the next tick. | 205 | /// Waits for the next tick. |
| 194 | pub fn next(&mut self) -> impl Future<Output = ()> + '_ { | 206 | pub fn next(&mut self) -> impl Future<Output = ()> + '_ { |
| 195 | poll_fn(|cx| { | 207 | poll_fn(|cx| { |
