From 3d64a8abef32f474234c9f8b6b2939e41acf4215 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 28 Sep 2020 21:46:03 +0200 Subject: [PATCH] Add from_secs, from_millis. Uses a hardcoded tick rate for now. --- embassy/src/time.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/embassy/src/time.rs b/embassy/src/time.rs index d42d0e5e..9a53c4da 100644 --- a/embassy/src/time.rs +++ b/embassy/src/time.rs @@ -25,6 +25,9 @@ pub struct Instant { ticks: u64, } +// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. +pub const TICKS_PER_SECOND: u32 = 32768; + impl Instant { pub fn now() -> Instant { Instant { ticks: now() } @@ -128,6 +131,18 @@ impl Duration { Duration { ticks } } + pub const fn from_secs(secs: u32) -> Duration { + Duration { + ticks: secs * TICKS_PER_SECOND, + } + } + + pub const fn from_millis(millis: u32) -> Duration { + Duration { + ticks: millis * TICKS_PER_SECOND / 1000, + } + } + pub fn checked_add(self, rhs: Duration) -> Option { self.ticks .checked_add(rhs.ticks)