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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use core::{convert::Infallible, marker::PhantomData};
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};

/// Pin level marker types for usage of `DummyPin` as an `InputPin` or `StatefulOutputPin`.
pub mod level {
    #[non_exhaustive]
    /// `DummyPin` will always behave as being high when checked.
    pub struct High;

    #[non_exhaustive]
    /// `DummyPin` will always behave as being low when checked.
    pub struct Low;
}

/// Dummy (no-op, zero-cost) pin
///
/// The implementation will discard any value written to it. When read,
/// it will always behave according to the value provided at construction
/// time (high or low).
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct DummyPin<L = level::Low> {
    _l: PhantomData<L>,
}

impl DummyPin<level::Low> {
    /// Create new instance
    ///
    /// When read it will always behave as being low.
    pub fn new_low() -> Self {
        DummyPin { _l: PhantomData }
    }
}

impl DummyPin<level::High> {
    /// Create new instance
    ///
    /// When read it will always behave as being high.
    pub fn new_high() -> Self {
        DummyPin { _l: PhantomData }
    }
}

impl<L> ErrorType for DummyPin<L> {
    type Error = Infallible;
}

impl<L> OutputPin for DummyPin<L> {
    fn set_high(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }

    fn set_low(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl InputPin for DummyPin<level::Low> {
    fn is_high(&mut self) -> Result<bool, Self::Error> {
        Ok(false)
    }

    fn is_low(&mut self) -> Result<bool, Self::Error> {
        Ok(true)
    }
}

impl InputPin for DummyPin<level::High> {
    fn is_high(&mut self) -> Result<bool, Self::Error> {
        Ok(true)
    }

    fn is_low(&mut self) -> Result<bool, Self::Error> {
        Ok(false)
    }
}

impl StatefulOutputPin for DummyPin<level::Low> {
    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
        Ok(false)
    }

    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
        Ok(true)
    }
}

impl StatefulOutputPin for DummyPin<level::High> {
    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
        Ok(true)
    }

    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
        Ok(false)
    }
}