[][src]Crate 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 clock is considered monotonic in this context as long as it fulfills the other requirements.

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

Instant: A specific instant in time ("time-point") read from a clock.

Duration: The difference of two instances. The time that has elapsed since an instant. 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(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);

Modules

prelude

Public traits

units

Time-based units of measure (Milliseconds, Hertz, etc)

Structs

Instant

Represents an instant of time relative to a specific Clock

Period

A fractional time period

Traits

Clock

An abstraction for time-keeping items such as hardware timers

Duration

A duration of time with signed, generic storage

TimeInt

Create time-based values from primitive and core numeric types.