1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Implementation of the [`eh0::blocking::delay`] and [`eh1::delay`]
//! traits.

/// Delay structure.
///
/// This is an empty structure that forwards delays to [`std::thread::sleep`].
///
/// [`sleep`]: std::thread::sleep
#[derive(Debug, Clone, Copy)]
pub struct Delay {
    _0: (),
}

impl Delay {
    /// Create a new delay structure.
    ///
    /// # Example
    ///
    /// ```
    /// use ftdi_embedded_hal::Delay;
    ///
    /// let mut my_delay: Delay = Delay::new();
    /// ```
    pub const fn new() -> Delay {
        Delay { _0: () }
    }
}

impl Default for Delay {
    fn default() -> Self {
        Delay::new()
    }
}

impl eh1::delay::DelayUs for Delay {
    type Error = std::convert::Infallible;

    fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
        std::thread::sleep(std::time::Duration::from_micros(us.into()));
        Ok(())
    }

    fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
        std::thread::sleep(std::time::Duration::from_millis(ms.into()));
        Ok(())
    }
}

macro_rules! impl_eh0_delay_for {
    ($UXX:ty) => {
        impl eh0::blocking::delay::DelayMs<$UXX> for Delay {
            fn delay_ms(&mut self, ms: $UXX) {
                std::thread::sleep(std::time::Duration::from_millis(ms.into()))
            }
        }

        impl eh0::blocking::delay::DelayUs<$UXX> for Delay {
            fn delay_us(&mut self, us: $UXX) {
                std::thread::sleep(std::time::Duration::from_micros(us.into()))
            }
        }
    };
}

impl_eh0_delay_for!(u8);
impl_eh0_delay_for!(u16);
impl_eh0_delay_for!(u32);
impl_eh0_delay_for!(u64);