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
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};

/// Inverted input/output pin
///
/// If wrapping an output pin, whenever setting this pin to a high or low level,
/// the wrapped pin will be set to the opposite level.
///
/// Likewise, if wrapping an input pin, whenever reading this pin it will read
/// the wrapped input pin and return the opposite level.
#[derive(Debug, Clone, Copy)]
pub struct InvertedPin<P> {
    pin: P,
}

impl<P> InvertedPin<P> {
    /// Create new instance
    pub fn new(pin: P) -> Self {
        Self { pin }
    }

    /// Destroy instance and return the wrapped pin
    pub fn destroy(self) -> P {
        self.pin
    }
}

impl<P, E> OutputPin for InvertedPin<P>
where
    P: OutputPin<Error = E>,
{
    type Error = E;

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

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

impl<P, E> InputPin for InvertedPin<P>
where
    P: InputPin<Error = E>,
{
    type Error = E;

    fn is_high(&self) -> Result<bool, Self::Error> {
        self.pin.is_low()
    }

    fn is_low(&self) -> Result<bool, Self::Error> {
        self.pin.is_high()
    }
}

impl<P, E> ToggleableOutputPin for InvertedPin<P>
where
    P: ToggleableOutputPin<Error = E>,
{
    type Error = E;

    fn toggle(&mut self) -> Result<(), Self::Error> {
        self.pin.toggle()
    }
}

impl<P, E> StatefulOutputPin for InvertedPin<P>
where
    P: StatefulOutputPin<Error = E>,
{
    fn is_set_high(&self) -> Result<bool, Self::Error> {
        self.pin.is_set_low()
    }

    fn is_set_low(&self) -> Result<bool, Self::Error> {
        self.pin.is_set_high()
    }
}