mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +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::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
use super::TICKS_PER_SECOND;
|
use super::TICKS_PER_SECOND;
|
||||||
|
|
||||||
#[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct Duration {
|
pub struct Duration {
|
||||||
pub(crate) ticks: u32,
|
pub(crate) ticks: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Duration {
|
impl Duration {
|
||||||
pub const fn into_ticks(&self) -> u32 {
|
pub const fn as_ticks(&self) -> u64 {
|
||||||
self.ticks
|
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 }
|
Duration { ticks }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn from_secs(secs: u32) -> Duration {
|
pub const fn from_secs(secs: u64) -> Duration {
|
||||||
Duration {
|
Duration {
|
||||||
ticks: secs * TICKS_PER_SECOND,
|
ticks: secs * TICKS_PER_SECOND,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn from_millis(millis: u32) -> Duration {
|
pub const fn from_millis(millis: u64) -> Duration {
|
||||||
Duration {
|
Duration {
|
||||||
ticks: millis * TICKS_PER_SECOND / 1000,
|
ticks: millis * TICKS_PER_SECOND / 1000,
|
||||||
}
|
}
|
||||||
@ -41,11 +50,11 @@ impl Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_mul(self, rhs: u32) -> Option<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> {
|
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;
|
*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::convert::TryInto;
|
||||||
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
|
use core::ops::{Add, AddAssign, Sub, SubAssign};
|
||||||
use core::pin::Pin;
|
use core::fmt;
|
||||||
use core::ptr;
|
|
||||||
use core::sync::atomic::{AtomicPtr, Ordering};
|
|
||||||
use core::task::{Context, Poll};
|
|
||||||
|
|
||||||
|
use super::TICKS_PER_SECOND;
|
||||||
use super::{now, Duration};
|
use super::{now, Duration};
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)]
|
||||||
@ -21,10 +19,30 @@ impl Instant {
|
|||||||
Self { ticks }
|
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
|
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 {
|
pub fn duration_since(&self, earlier: Instant) -> Duration {
|
||||||
Duration {
|
Duration {
|
||||||
ticks: (self.ticks - earlier.ticks).try_into().unwrap(),
|
ticks: (self.ticks - earlier.ticks).try_into().unwrap(),
|
||||||
@ -104,3 +122,9 @@ impl Sub<Instant> for Instant {
|
|||||||
self.duration_since(other)
|
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;
|
use crate::util::Dewrap;
|
||||||
|
|
||||||
// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something.
|
// 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;
|
static mut CLOCK: Option<&'static dyn Clock> = None;
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ pub struct Timer {
|
|||||||
impl Timer {
|
impl Timer {
|
||||||
pub fn at(when: Instant) -> Self {
|
pub fn at(when: Instant) -> Self {
|
||||||
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