mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-25 16:23:10 +00:00
time: 64bit duration, add some methods
This commit is contained in:
parent
041a22a958
commit
a0cc229a3a
@ -1,28 +1,37 @@
|
||||
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use core::fmt;
|
||||
|
||||
use super::TICKS_PER_SECOND;
|
||||
|
||||
#[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Duration {
|
||||
pub(crate) ticks: u32,
|
||||
pub(crate) ticks: u64,
|
||||
}
|
||||
|
||||
impl Duration {
|
||||
pub const fn into_ticks(&self) -> u32 {
|
||||
pub const fn as_ticks(&self) -> u64 {
|
||||
self.ticks
|
||||
}
|
||||
|
||||
pub const fn from_ticks(ticks: u32) -> Duration {
|
||||
pub const fn as_secs(&self) -> u64 {
|
||||
self.ticks / TICKS_PER_SECOND
|
||||
}
|
||||
|
||||
pub const fn as_millis(&self) -> u64 {
|
||||
self.ticks * 1000 / TICKS_PER_SECOND
|
||||
}
|
||||
|
||||
pub const fn from_ticks(ticks: u64) -> Duration {
|
||||
Duration { ticks }
|
||||
}
|
||||
|
||||
pub const fn from_secs(secs: u32) -> Duration {
|
||||
pub const fn from_secs(secs: u64) -> Duration {
|
||||
Duration {
|
||||
ticks: secs * TICKS_PER_SECOND,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn from_millis(millis: u32) -> Duration {
|
||||
pub const fn from_millis(millis: u64) -> Duration {
|
||||
Duration {
|
||||
ticks: millis * TICKS_PER_SECOND / 1000,
|
||||
}
|
||||
@ -41,11 +50,11 @@ impl Duration {
|
||||
}
|
||||
|
||||
pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
|
||||
self.ticks.checked_mul(rhs).map(|ticks| Duration { ticks })
|
||||
self.ticks.checked_mul(rhs as _).map(|ticks| Duration { ticks })
|
||||
}
|
||||
|
||||
pub fn checked_div(self, rhs: u32) -> Option<Duration> {
|
||||
self.ticks.checked_div(rhs).map(|ticks| Duration { ticks })
|
||||
self.ticks.checked_div(rhs as _).map(|ticks| Duration { ticks })
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,3 +125,9 @@ impl DivAssign<u32> for Duration {
|
||||
*self = *self / rhs;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Duration {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{} ticks", self.ticks)
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
use core::convert::TryInto;
|
||||
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use core::pin::Pin;
|
||||
use core::ptr;
|
||||
use core::sync::atomic::{AtomicPtr, Ordering};
|
||||
use core::task::{Context, Poll};
|
||||
use core::ops::{Add, AddAssign, Sub, SubAssign};
|
||||
use core::fmt;
|
||||
|
||||
use super::TICKS_PER_SECOND;
|
||||
use super::{now, Duration};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)]
|
||||
@ -21,10 +19,30 @@ impl Instant {
|
||||
Self { ticks }
|
||||
}
|
||||
|
||||
pub const fn into_ticks(&self) -> u64 {
|
||||
pub const fn from_millis(millis: u64) -> Self {
|
||||
Self {
|
||||
ticks: millis * TICKS_PER_SECOND as u64 / 1000,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn from_secs(seconds: u64) -> Self {
|
||||
Self {
|
||||
ticks: seconds * TICKS_PER_SECOND as u64,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn as_ticks(&self) -> u64 {
|
||||
self.ticks
|
||||
}
|
||||
|
||||
pub const fn as_secs(&self) -> u64 {
|
||||
self.ticks / TICKS_PER_SECOND as u64
|
||||
}
|
||||
|
||||
pub const fn as_millis(&self) -> u64 {
|
||||
self.ticks * 1000 / TICKS_PER_SECOND as u64
|
||||
}
|
||||
|
||||
pub fn duration_since(&self, earlier: Instant) -> Duration {
|
||||
Duration {
|
||||
ticks: (self.ticks - earlier.ticks).try_into().unwrap(),
|
||||
@ -104,3 +122,9 @@ impl Sub<Instant> for Instant {
|
||||
self.duration_since(other)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Instant {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{} ticks", self.ticks)
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ pub use traits::*;
|
||||
use crate::util::Dewrap;
|
||||
|
||||
// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something.
|
||||
pub const TICKS_PER_SECOND: u32 = 32768;
|
||||
pub const TICKS_PER_SECOND: u64 = 32768;
|
||||
|
||||
static mut CLOCK: Option<&'static dyn Clock> = None;
|
||||
|
||||
|
@ -13,7 +13,7 @@ pub struct Timer {
|
||||
impl Timer {
|
||||
pub fn at(when: Instant) -> Self {
|
||||
Self {
|
||||
inner: current_timer_queue().deadline(when.into_ticks()),
|
||||
inner: current_timer_queue().deadline(when.as_ticks()),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user