Skip to content

Commit

Permalink
More work on the Timer/Instant API
Browse files Browse the repository at this point in the history
  • Loading branch information
David O'Connor committed May 23, 2024
1 parent f16ae22 commit b9a7421
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ pub struct Instant {
// todo: Count ticks instead?
}

/// An instant. Designed to be, in its most basic sense, similar to `std::time::Instant`. Created
/// from the `now()` method on a `Timer`. Can be compared to create a `core::Duration`. Standalone
/// methods on this struct are similar to those on `Duration`, but return timestamps since the timer start.
impl Instant {
pub fn new(count_ns: i128) -> Self {
pub(crate) fn new(count_ns: i128) -> Self {
Self { count_ns }
}

Expand Down
44 changes: 4 additions & 40 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ macro_rules! make_timer {
///
/// Important: the value returned here will only be correct if the ARR and PSC are set
/// only using the constructor, `set_freq`, or `set_period` methods.
pub fn elapsed(&mut self) -> Instant {
pub fn now(&mut self) -> Instant {
// let wrap_count = self.wrap_count;
let wrap_count = TICK_OVERFLOW_COUNT.load(Ordering::Acquire);

Expand All @@ -851,45 +851,9 @@ macro_rules! make_timer {
Instant::new(count_ns)
}

/// Get time elapsed since the timer start, as a `core::Duration`.
pub fn elapsed2(&mut self) -> Duration {
// let wrap_count = self.wrap_count;
let wrap_count = TICK_OVERFLOW_COUNT.load(Ordering::Acquire) as u64;

let ticks = (self.read_count() as u64 + wrap_count as u64 *
self.get_max_duty() as u64);
let count_ns = ticks as i128 * self.ns_per_tick as i128;

Duration::from_nanos(count_ns as u64)
}


// /// Get the current timestamp, while keeping track of overflows. You must increment `TICK_OVERFLOW_COUNT`
// /// in firmware using an ISR, and the timer must be kept running. This is useful for tracking
// /// longer periods of time, such as system uptime.
// pub fn get_timestamp(&mut self) -> f32 {
// let elapsed = self.time_elapsed().as_secs();
//
// TICK_OVERFLOW_COUNT.load(Ordering::Acquire) as f32 * self.period + elapsed
// }

// /// An alternative to `get_timestamp` that returns the result in milliseconds. It avoids
// /// floating point problems on longer runs.
// pub fn get_timestamp_ms(&mut self) -> u64 {
// let elapsed_ms = self.elapsed().count_ns as u64 / 1_000_000;
// let period_ms = (self.period * 1_000.) as u64;
//
// TICK_OVERFLOW_COUNT.load(Ordering::Acquire) as u64 * period_ms + elapsed_ms
// }
//
// /// An alternative to `get_timestamp` that returns the result in microseconds. It avoids
// /// floating point problems on longer runs.
// pub fn get_timestamp_us(&mut self) -> u64 {
// let elapsed_us = self.elapsed().count_ns as u64 / 1_000;
// let period_us = (self.period * 1_000_000.) as u64;
//
// TICK_OVERFLOW_COUNT.load(Ordering::Acquire) as u64 * period_us as u64 + elapsed_us
// }
pub fn elapsed(&mut self, since: Instant) -> Duration {
self.now() - since
}
}

#[cfg(feature = "monotonic")]
Expand Down

0 comments on commit b9a7421

Please sign in to comment.