thread-delay 0.1.1

Implementors of DelayNs from embedded_hal, for Linux systems.
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented0 out of 1 items with examples
  • Size
  • Source code size: 21.66 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.35 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • iancohee/thread_delay
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • iancohee

thread-delay License: MIT OR Apache-2.0 thread-delay on crates.io thread-delay on docs.rs

This crate provides a blocking implementation of DelayNs from embedded_hal that can be used on Linux systems using the Linux standard library. It is intended to be a drop-in replacement for application using libraries that were written for embedded devices but have access to the standard library, such as a single board computer (SBC) running Linux.

The Delay implementation is like the one from linux_embedded_hal but provides all the delay_* functions defined by the trait.

The functions are:

  1. delay_ns for nanosecond delays
  2. delay_us for microsecond delays
  3. delay_ms for milliseconds delays

Example

This example is an application running on a Raspberry Pi Zero 2 W that takes readings from a bme280 atmospheric sensor.

use thread_delay::Delay;

use bme280::i2c::BME280;
use rppal::i2c::I2c;
use std::time::Instant;
 
enum LoopState {
    Waiting,
    Measureing,
}

fn main() -> ! {
    let i2c = I2c::with_bus(1).expect("failed to find i2c bus 1");
    assert!(i2c.bus() == 1_u8);

    let mut delay = Delay {};

    let mut bme280 = BME280::new_primary(i2c);

    bme280.init(&mut delay).expect("failed to initialize bme280 sensor");

    let mut state = LoopState::Waiting;

    let delay_millis: u128 = 1_000;

    let mut last_update = Instant::now();

    loop {
        let elapsed = last_update.elapsed();

        match state {
            LoopState::Waiting => {
                if elapsed.as_millis() >= delay_millis {
                    state = LoopState::Measureing
                }
            }

            LoopState::Measureing => {
                let measurements = bme280
                    .measure(&mut delay)
                    .expect("failed to read measurements from bme280");

                println!("Temp:     |{:-10.3} C |", measurements.temperature);
                println!("Humidity: |{:-10.3} % |", measurements.humidity);
                println!("Pressure: |{:-10.3} Pa|", measurements.pressure);
                println!("-----");

                last_update = Instant::now();
                state = LoopState::Waiting;
            }
        }
    }
}