[][src]Crate embedded_time

Embedded Time

embedded-time provides a comprehensive library for implementing Clock abstractions over hardware to generate Instants and using Durations (Seconds, Milliseconds, etc) in embedded systems. The approach is similar to the C++ chrono library. A Duration consists of an integer (whose type is chosen by the user to be either i32 or i64) as well as a const ratio where the integer value multiplied by the ratio is the Duration in seconds. Put another way, the ratio is the precision of the LSbit of the integer. This structure avoids unnecessary arithmetic. For example, if the Duration type is Milliseconds, a call to the Duration::count() method simply returns the stored integer value directly which is the number of milliseconds being represented. Conversion arithmetic is only performed when explicitly converting between time units.

Definitions

Clock: Any entity that periodically counts (ie a hardware timer peripheral). Generally, this needs to be monotonic. A wrapping timer is considered monotonic in this context as long as it fulfills the other requirements.

Wrapping Timer: A timer that when at its maximum value, the next count is the minimum value.

Instant: A specific instant in time ("time-point") returned by calling Clock::now().

Duration: The difference of two instances. The duration of time elapsed from one instant until another. A span of time.

Notes

Some parts of this crate were derived from various sources:

Example Usage

struct SomeClock;
impl embedded_time::Clock for SomeClock {
    type Rep = i64;
    const PERIOD: Period = Period::new_raw(1, 16_000_000);

    fn now() -> Instant<Self> {
        // ...
    }
}

let instant1 = SomeClock::now();
// ...
let instant2 = SomeClock::now();
assert!(instant1 < instant2);    // instant1 is *before* instant2

// duration is the difference between the instances
let duration: Option<Microseconds<i64>> = instant2.duration_since(&instant1);    

assert!(duration.is_some());
assert_eq!(instant1 + duration.unwrap(), instant2);

Re-exports

pub use duration::time_units;
pub use duration::Duration;

Modules

duration

Duration types/units creation and conversion.

instant
prelude

A collection of imports that are widely useful.

Traits

Clock
TimeRep

Create Durations from primitive and core numeric types.

Type Definitions

Period