Skip to main content

linux_embedded_hal/
delay.rs

1//! Implementation of [`embedded-hal`] delay traits
2//!
3//! [`embedded-hal`]: https://docs.rs/embedded-hal
4
5use embedded_hal::delay::DelayNs;
6use std::thread;
7use std::time::Duration;
8
9/// Empty struct that provides delay functionality on top of `thread::sleep`,
10/// and `tokio::time::sleep` if the `async-tokio` feature is enabled.
11pub struct Delay;
12
13impl DelayNs for Delay {
14    fn delay_ns(&mut self, n: u32) {
15        thread::sleep(Duration::from_nanos(n.into()));
16    }
17
18    fn delay_us(&mut self, n: u32) {
19        thread::sleep(Duration::from_micros(n.into()));
20    }
21
22    fn delay_ms(&mut self, n: u32) {
23        thread::sleep(Duration::from_millis(n.into()));
24    }
25}
26
27#[cfg(feature = "async-tokio")]
28impl embedded_hal_async::delay::DelayNs for Delay {
29    async fn delay_ns(&mut self, n: u32) {
30        tokio::time::sleep(Duration::from_nanos(n.into())).await;
31    }
32
33    async fn delay_us(&mut self, n: u32) {
34        tokio::time::sleep(Duration::from_micros(n.into())).await;
35    }
36
37    async fn delay_ms(&mut self, n: u32) {
38        tokio::time::sleep(Duration::from_millis(n.into())).await;
39    }
40}