ftdi_embedded_hal/
delay.rs

1//! Implementation of the [`eh0::blocking::delay`] and [`eh1::delay`]
2//! traits.
3
4/// Delay structure.
5///
6/// This is an empty structure that forwards delays to [`std::thread::sleep`].
7///
8/// [`sleep`]: std::thread::sleep
9#[derive(Debug, Clone, Copy)]
10pub struct Delay {
11    _0: (),
12}
13
14impl Delay {
15    /// Create a new delay structure.
16    ///
17    /// # Example
18    ///
19    /// ```
20    /// use ftdi_embedded_hal::Delay;
21    ///
22    /// let mut my_delay: Delay = Delay::new();
23    /// ```
24    pub const fn new() -> Delay {
25        Delay { _0: () }
26    }
27}
28
29impl Default for Delay {
30    fn default() -> Self {
31        Delay::new()
32    }
33}
34
35impl eh1::delay::DelayNs for Delay {
36    fn delay_ns(&mut self, ns: u32) {
37        std::thread::sleep(std::time::Duration::from_nanos(ns.into()))
38    }
39
40    fn delay_us(&mut self, us: u32) {
41        std::thread::sleep(std::time::Duration::from_micros(us.into()))
42    }
43
44    fn delay_ms(&mut self, ms: u32) {
45        std::thread::sleep(std::time::Duration::from_millis(ms.into()))
46    }
47}
48
49macro_rules! impl_eh0_delay_for {
50    ($UXX:ty) => {
51        impl eh0::blocking::delay::DelayMs<$UXX> for Delay {
52            fn delay_ms(&mut self, ms: $UXX) {
53                std::thread::sleep(std::time::Duration::from_millis(ms.into()))
54            }
55        }
56
57        impl eh0::blocking::delay::DelayUs<$UXX> for Delay {
58            fn delay_us(&mut self, us: $UXX) {
59                std::thread::sleep(std::time::Duration::from_micros(us.into()))
60            }
61        }
62    };
63}
64
65impl_eh0_delay_for!(u8);
66impl_eh0_delay_for!(u16);
67impl_eh0_delay_for!(u32);
68impl_eh0_delay_for!(u64);