aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-09-28 21:46:03 +0200
committerDario Nieuwenhuis <[email protected]>2020-09-28 21:46:03 +0200
commit3d64a8abef32f474234c9f8b6b2939e41acf4215 (patch)
treeb700c3e340258a412303b7aad473975598c92f9a
parent68eac3a57cf5e27b454489384aa42083962f56cb (diff)
Add from_secs, from_millis.
Uses a hardcoded tick rate for now.
-rw-r--r--embassy/src/time.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/embassy/src/time.rs b/embassy/src/time.rs
index d42d0e5ee..9a53c4daf 100644
--- a/embassy/src/time.rs
+++ b/embassy/src/time.rs
@@ -25,6 +25,9 @@ pub struct Instant {
25 ticks: u64, 25 ticks: u64,
26} 26}
27 27
28// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something.
29pub const TICKS_PER_SECOND: u32 = 32768;
30
28impl Instant { 31impl Instant {
29 pub fn now() -> Instant { 32 pub fn now() -> Instant {
30 Instant { ticks: now() } 33 Instant { ticks: now() }
@@ -128,6 +131,18 @@ impl Duration {
128 Duration { ticks } 131 Duration { ticks }
129 } 132 }
130 133
134 pub const fn from_secs(secs: u32) -> Duration {
135 Duration {
136 ticks: secs * TICKS_PER_SECOND,
137 }
138 }
139
140 pub const fn from_millis(millis: u32) -> Duration {
141 Duration {
142 ticks: millis * TICKS_PER_SECOND / 1000,
143 }
144 }
145
131 pub fn checked_add(self, rhs: Duration) -> Option<Duration> { 146 pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
132 self.ticks 147 self.ticks
133 .checked_add(rhs.ticks) 148 .checked_add(rhs.ticks)