2020-10-26 12:39:53 +01:00
|
|
|
use core::fmt;
|
2020-10-31 16:37:34 +01:00
|
|
|
use core::ops::{Add, AddAssign, Sub, SubAssign};
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2022-09-02 00:58:31 +02:00
|
|
|
use super::{driver, Duration, GCD_1K, GCD_1M, TICK_HZ};
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2020-12-01 17:46:56 +01:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-03-22 00:45:53 +01:00
|
|
|
/// An Instant in time, based on the MCU's clock ticks since startup.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub struct Instant {
|
|
|
|
ticks: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Instant {
|
2021-08-24 22:46:07 +02:00
|
|
|
/// The smallest (earliest) value that can be represented by the `Instant` type.
|
2020-12-26 23:44:53 +01:00
|
|
|
pub const MIN: Instant = Instant { ticks: u64::MIN };
|
2021-08-24 22:46:07 +02:00
|
|
|
/// The largest (latest) value that can be represented by the `Instant` type.
|
2020-12-26 23:44:53 +01:00
|
|
|
pub const MAX: Instant = Instant { ticks: u64::MAX };
|
|
|
|
|
2021-03-22 00:45:53 +01:00
|
|
|
/// Returns an Instant representing the current time.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn now() -> Instant {
|
2022-06-12 22:15:44 +02:00
|
|
|
Instant { ticks: driver::now() }
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Create an Instant from a tick count since system boot.
|
2020-10-19 21:21:43 +02:00
|
|
|
pub const fn from_ticks(ticks: u64) -> Self {
|
|
|
|
Self { ticks }
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:01:43 +01:00
|
|
|
/// Create an Instant from a microsecond count since system boot.
|
|
|
|
pub const fn from_micros(micros: u64) -> Self {
|
|
|
|
Self {
|
2022-09-02 00:58:31 +02:00
|
|
|
ticks: micros * (TICK_HZ / GCD_1M) / (1_000_000 / GCD_1M),
|
2022-02-12 03:01:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Create an Instant from a millisecond count since system boot.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn from_millis(millis: u64) -> Self {
|
|
|
|
Self {
|
2022-09-02 00:58:31 +02:00
|
|
|
ticks: millis * (TICK_HZ / GCD_1K) / (1000 / GCD_1K),
|
2020-10-26 12:39:53 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-22 01:01:13 +01:00
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Create an Instant from a second count since system boot.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn from_secs(seconds: u64) -> Self {
|
|
|
|
Self {
|
2022-09-02 00:58:31 +02:00
|
|
|
ticks: seconds * TICK_HZ,
|
2020-10-26 12:39:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Tick count since system boot.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn as_ticks(&self) -> u64 {
|
2020-10-19 21:15:24 +02:00
|
|
|
self.ticks
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Seconds since system boot.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn as_secs(&self) -> u64 {
|
2022-09-02 00:58:31 +02:00
|
|
|
self.ticks / TICK_HZ
|
2020-10-26 12:39:53 +01:00
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Milliseconds since system boot.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn as_millis(&self) -> u64 {
|
2022-09-02 00:58:31 +02:00
|
|
|
self.ticks * (1000 / GCD_1K) / (TICK_HZ / GCD_1K)
|
2020-10-26 12:39:53 +01:00
|
|
|
}
|
|
|
|
|
2022-02-12 03:01:43 +01:00
|
|
|
/// Microseconds since system boot.
|
|
|
|
pub const fn as_micros(&self) -> u64 {
|
2022-09-02 00:58:31 +02:00
|
|
|
self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
|
2022-02-12 03:01:43 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:45:53 +01:00
|
|
|
/// Duration between this Instant and another Instant
|
|
|
|
/// Panics on over/underflow.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn duration_since(&self, earlier: Instant) -> Duration {
|
|
|
|
Duration {
|
2021-02-14 01:41:36 +01:00
|
|
|
ticks: self.ticks.checked_sub(earlier.ticks).unwrap(),
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:45:53 +01:00
|
|
|
/// Duration between this Instant and another Instant
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
|
|
|
|
if self.ticks < earlier.ticks {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Duration {
|
2021-02-14 01:41:36 +01:00
|
|
|
ticks: self.ticks - earlier.ticks,
|
2020-10-19 21:15:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:45:53 +01:00
|
|
|
/// Returns the duration since the "earlier" Instant.
|
|
|
|
/// If the "earlier" instant is in the future, the duration is set to zero.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
|
|
|
|
Duration {
|
|
|
|
ticks: if self.ticks < earlier.ticks {
|
|
|
|
0
|
|
|
|
} else {
|
2021-02-14 01:41:36 +01:00
|
|
|
self.ticks - earlier.ticks
|
2020-10-19 21:15:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:45:53 +01:00
|
|
|
/// Duration elapsed since this Instant.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn elapsed(&self) -> Duration {
|
|
|
|
Instant::now() - *self
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Adds one Duration to self, returning a new `Instant` or None in the event of an overflow.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.ticks.checked_add(duration.ticks).map(|ticks| Instant { ticks })
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
2021-08-24 22:46:07 +02:00
|
|
|
|
|
|
|
/// Subtracts one Duration to self, returning a new `Instant` or None in the event of an overflow.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.ticks.checked_sub(duration.ticks).map(|ticks| Instant { ticks })
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<Duration> for Instant {
|
|
|
|
type Output = Instant;
|
|
|
|
|
|
|
|
fn add(self, other: Duration) -> Instant {
|
|
|
|
self.checked_add(other)
|
|
|
|
.expect("overflow when adding duration to instant")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign<Duration> for Instant {
|
|
|
|
fn add_assign(&mut self, other: Duration) {
|
|
|
|
*self = *self + other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<Duration> for Instant {
|
|
|
|
type Output = Instant;
|
|
|
|
|
|
|
|
fn sub(self, other: Duration) -> Instant {
|
|
|
|
self.checked_sub(other)
|
|
|
|
.expect("overflow when subtracting duration from instant")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubAssign<Duration> for Instant {
|
|
|
|
fn sub_assign(&mut self, other: Duration) {
|
|
|
|
*self = *self - other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<Instant> for Instant {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn sub(self, other: Instant) -> Duration {
|
|
|
|
self.duration_since(other)
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 12:39:53 +01:00
|
|
|
|
|
|
|
impl<'a> fmt::Display for Instant {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{} ticks", self.ticks)
|
|
|
|
}
|
|
|
|
}
|