Skip to main content

SimpleTimer

Struct SimpleTimer 

Source
pub struct SimpleTimer { /* private fields */ }
Expand description

Simple One-Shot Timer

A simple timer for one-shot timing operations. Unlike super::Ton, this timer does NOT follow the IEC 61131-3 standard and is designed for imperative “start then check” patterns rather than cyclic function block calls.

Note: For standard PLC timer patterns (delay while condition is true), use super::Ton instead. SimpleTimer is intended for one-shot use cases like “do X, wait, then do Y”.

§Safety

This timer uses Instant (monotonic clock) internally, making it immune to system clock adjustments (NTP sync, manual changes, DST). This is critical for reliable timing in industrial control applications.

§Example

use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let mut timer = SimpleTimer::new(Duration::from_millis(100));

// Timer hasn't started yet
assert_eq!(timer.is_done(), false);
assert_eq!(timer.elapsed(), Duration::ZERO);

// Start the timer
timer.start();

// Check if done (will be false initially)
assert_eq!(timer.is_done(), false);

// After waiting...
std::thread::sleep(Duration::from_millis(110));
assert_eq!(timer.is_done(), true);

// Reset for reuse
timer.reset();
assert_eq!(timer.is_done(), false);

§Use Cases

  • One-shot delays (“wait 500ms then continue”)
  • Tracking time since an event occurred
  • Non-cyclic async contexts
  • Simple timeout checks

§Comparison with Ton

AspectTonSimpleTimer
IEC 61131-3YesNo
PatternCyclic call()Imperative start()/is_done()
Auto-reset on disableYesNo (explicit reset())
Best forPLC-style controlOne-shot operations

Implementations§

Source§

impl SimpleTimer

Source

pub fn new(preset: Duration) -> Self

Creates a new simple timer with the specified preset duration.

The timer starts in the stopped state and must be explicitly started with start().

§Arguments
  • preset - The duration after which is_done() returns true
§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let timer = SimpleTimer::new(Duration::from_secs(5));
assert_eq!(timer.is_done(), false);
Source

pub fn start(&mut self)

Starts (or restarts) the timer.

Records the current time as the start time. If the timer was already running, this restarts it from zero.

§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let mut timer = SimpleTimer::new(Duration::from_millis(100));
timer.start();
// Timer is now counting...
Source

pub fn is_done(&self) -> bool

Checks if the preset time has elapsed since the timer was started.

Returns false if the timer hasn’t been started yet.

§Returns

true if the timer was started and the preset duration has elapsed, false otherwise.

§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let mut timer = SimpleTimer::new(Duration::from_millis(50));

// Not started yet
assert_eq!(timer.is_done(), false);

timer.start();
std::thread::sleep(Duration::from_millis(60));
assert_eq!(timer.is_done(), true);
Source

pub fn elapsed(&self) -> Duration

Returns the elapsed time since the timer was started.

Returns Duration::ZERO if the timer hasn’t been started yet.

§Returns

The elapsed time since start() was called, or Duration::ZERO if not started.

§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let mut timer = SimpleTimer::new(Duration::from_secs(10));

// Not started
assert_eq!(timer.elapsed(), Duration::ZERO);

timer.start();
std::thread::sleep(Duration::from_millis(50));
assert!(timer.elapsed() >= Duration::from_millis(50));
Source

pub fn reset(&mut self)

Resets the timer to its initial (stopped) state.

After calling this, is_done() will return false and elapsed() will return zero until start() is called again.

§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let mut timer = SimpleTimer::new(Duration::from_millis(50));
timer.start();
std::thread::sleep(Duration::from_millis(60));
assert_eq!(timer.is_done(), true);

timer.reset();
assert_eq!(timer.is_done(), false);
assert_eq!(timer.elapsed(), Duration::ZERO);
Source

pub fn preset(&self) -> Duration

Returns the preset duration for this timer.

§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let timer = SimpleTimer::new(Duration::from_secs(5));
assert_eq!(timer.preset(), Duration::from_secs(5));
Source

pub fn set_preset(&mut self, preset: Duration)

Sets a new preset duration.

This does not reset or restart the timer. If the timer is running, the new preset takes effect immediately for is_done() checks.

§Arguments
  • preset - The new duration after which is_done() returns true
§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let mut timer = SimpleTimer::new(Duration::from_secs(5));
timer.set_preset(Duration::from_secs(10));
assert_eq!(timer.preset(), Duration::from_secs(10));
Source

pub fn is_running(&self) -> bool

Returns whether the timer is currently running (has been started but not reset).

§Example
use autocore_std::fb::SimpleTimer;
use std::time::Duration;

let mut timer = SimpleTimer::new(Duration::from_secs(5));
assert_eq!(timer.is_running(), false);

timer.start();
assert_eq!(timer.is_running(), true);

timer.reset();
assert_eq!(timer.is_running(), false);

Trait Implementations§

Source§

impl Clone for SimpleTimer

Source§

fn clone(&self) -> SimpleTimer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SimpleTimer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SimpleTimer

Source§

fn default() -> Self

Creates a timer with a preset of zero (will be immediately done when started).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V