dummy_pin/
dummy.rs

1use core::{convert::Infallible, marker::PhantomData};
2use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};
3
4/// Pin level marker types for usage of `DummyPin` as an `InputPin` or `StatefulOutputPin`.
5pub mod level {
6    #[non_exhaustive]
7    /// `DummyPin` will always behave as being high when checked.
8    pub struct High;
9
10    #[non_exhaustive]
11    /// `DummyPin` will always behave as being low when checked.
12    pub struct Low;
13}
14
15/// Dummy (no-op, zero-cost) pin
16///
17/// The implementation will discard any value written to it. When read,
18/// it will always behave according to the value provided at construction
19/// time (high or low).
20#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
21pub struct DummyPin<L = level::Low> {
22    _l: PhantomData<L>,
23}
24
25impl DummyPin<level::Low> {
26    /// Create new instance
27    ///
28    /// When read it will always behave as being low.
29    pub fn new_low() -> Self {
30        DummyPin { _l: PhantomData }
31    }
32}
33
34impl DummyPin<level::High> {
35    /// Create new instance
36    ///
37    /// When read it will always behave as being high.
38    pub fn new_high() -> Self {
39        DummyPin { _l: PhantomData }
40    }
41}
42
43impl<L> ErrorType for DummyPin<L> {
44    type Error = Infallible;
45}
46
47impl<L> OutputPin for DummyPin<L> {
48    fn set_high(&mut self) -> Result<(), Self::Error> {
49        Ok(())
50    }
51
52    fn set_low(&mut self) -> Result<(), Self::Error> {
53        Ok(())
54    }
55}
56
57impl InputPin for DummyPin<level::Low> {
58    fn is_high(&mut self) -> Result<bool, Self::Error> {
59        Ok(false)
60    }
61
62    fn is_low(&mut self) -> Result<bool, Self::Error> {
63        Ok(true)
64    }
65}
66
67impl InputPin for DummyPin<level::High> {
68    fn is_high(&mut self) -> Result<bool, Self::Error> {
69        Ok(true)
70    }
71
72    fn is_low(&mut self) -> Result<bool, Self::Error> {
73        Ok(false)
74    }
75}
76
77impl StatefulOutputPin for DummyPin<level::Low> {
78    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
79        Ok(false)
80    }
81
82    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
83        Ok(true)
84    }
85}
86
87impl StatefulOutputPin for DummyPin<level::High> {
88    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
89        Ok(true)
90    }
91
92    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
93        Ok(false)
94    }
95}