Expand description
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:
delay_nsfor nanosecond delaysdelay_usfor microsecond delaysdelay_msfor 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,
Measuring,
}
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::Measuring
}
}
LoopState::Measuring => {
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;
}
}
}
}Structs§
- Thread
Delay - Empty struct that implements the
DelayNstrait usingstd::thread::sleep
Type Aliases§
- Delay
- A convienvce alias for keeping the
Delaystruct name