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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! [`Instrument`] implementations for GPIO pins.
//!
//! Types implementing `embedded-hal v1.0`
//! [`OutputPin`](embedded_hal_1::digital::OutputPin) and `embedded-hal v0.2`
//! [`OutputPin`](embedded_hal_0_2::digital::v2::OutputPin) are supported.

use crate::Instrument;
use embedded_hal_1 as embedded_hal;

#[cfg(feature = "embedded-hal_0_2")]
use embedded_hal_0_2 as embedded_hal_legacy;

/// [`Instrument`] implementation for an [`OutputPin`] (`embedded-hal` version
/// 1.0).
///
/// This type is also available as [`GpioRef`], which does not consume the
/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
/// supports `embedded-hal` version 0.2, see [`LegacyGpio`].
///
/// [`OutputPin`]: embedded_hal::digital::OutputPin
pub struct Gpio<P: embedded_hal::digital::OutputPin> {
    pin: P,
}

impl<P: embedded_hal::digital::OutputPin> From<P> for Gpio<P> {
    #[inline]
    fn from(pin: P) -> Self {
        Self::new(pin)
    }
}

impl<P: embedded_hal::digital::OutputPin> Gpio<P> {
    /// Create a new [`Gpio`].
    #[inline]
    pub fn new(pin: P) -> Self {
        Self { pin }
    }

    /// Return the underlying [`OutputPin`](embedded_hal::digital::OutputPin).
    #[inline]
    pub fn free(self) -> P {
        self.pin
    }
}

impl<P: embedded_hal::digital::OutputPin> Instrument for Gpio<P> {
    #[inline]
    fn on_enter(&mut self) {
        let _ = self.pin.set_high();
    }

    #[inline]
    fn on_exit(&mut self) {
        let _ = self.pin.set_low();
    }
}

/// Reference-taking version of an [`Instrument`] implementation for an
/// [`OutputPin`] (`embedded-hal` version 1.0).
///
/// This type is also available as [`Gpio`], which does consumes the
/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
/// supports `embedded-hal` version 0.2, see [`LegacyGpioRef`].
///
/// [`OutputPin`]: embedded_hal::digital::OutputPin
pub struct GpioRef<'a, P: embedded_hal::digital::OutputPin> {
    pin: &'a mut P,
}

impl<'a, P: embedded_hal::digital::OutputPin> GpioRef<'a, P> {
    /// Create a new [`GpioRef`].
    #[inline]
    pub fn new(pin: &'a mut P) -> Self {
        Self { pin }
    }
}

impl<'a, P: embedded_hal::digital::OutputPin> Instrument for GpioRef<'a, P> {
    #[inline]
    fn on_enter(&mut self) {
        let _ = self.pin.set_high();
    }

    #[inline]
    fn on_exit(&mut self) {
        let _ = self.pin.set_low();
    }
}

/// [`Instrument`] implementation for an [`OutputPin`] (`embedded-hal` version
/// 0.2).
///
/// This type is also available as [`LegacyGpioRef`], which does not consume the
/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
/// supports `embedded-hal` version 1.0, see [`Gpio`].
///
/// [`OutputPin`]: embedded_hal::digital::OutputPin
#[cfg(feature = "embedded-hal_0_2")]
pub struct LegacyGpio<P: embedded_hal_legacy::digital::v2::OutputPin> {
    pin: P,
}

#[cfg(feature = "embedded-hal_0_2")]
impl<P: embedded_hal_legacy::digital::v2::OutputPin> From<P> for LegacyGpio<P> {
    fn from(pin: P) -> Self {
        Self { pin }
    }
}

#[cfg(feature = "embedded-hal_0_2")]
impl<P: embedded_hal_legacy::digital::v2::OutputPin> LegacyGpio<P> {
    /// Create a new [`LegacyGpio`].
    #[inline]
    pub fn new(pin: P) -> Self {
        Self { pin }
    }

    /// Return the underlying
    /// [`OutputPin`](embedded_hal_legacy::digital::v2::OutputPin).
    #[inline]
    pub fn free(self) -> P {
        self.pin
    }
}

#[cfg(feature = "embedded-hal_0_2")]
impl<P: embedded_hal_legacy::digital::v2::OutputPin> Instrument for LegacyGpio<P> {
    #[inline]
    fn on_enter(&mut self) {
        let _ = self.pin.set_high();
    }

    #[inline]
    fn on_exit(&mut self) {
        let _ = self.pin.set_low();
    }
}

/// Reference-taking version of an [`Instrument`] implementation for an
/// [`OutputPin`] (`embedded-hal` version 0.2).
///
/// This type is also available as [`LegacyGpio`], which consumes the
/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
/// supports `embedded-hal` version 1.0, see [`GpioRef`].
///
/// [`OutputPin`]: embedded_hal::digital::OutputPin
#[cfg(feature = "embedded-hal_0_2")]
pub struct LegacyGpioRef<'a, P: embedded_hal_legacy::digital::v2::OutputPin> {
    pin: &'a mut P,
}

#[cfg(feature = "embedded-hal_0_2")]
impl<'a, P: embedded_hal_legacy::digital::v2::OutputPin> LegacyGpioRef<'a, P> {
    /// Create a new [`LegacyGpioRef`].
    #[inline]
    pub fn new(pin: &'a mut P) -> Self {
        Self { pin }
    }
}

#[cfg(feature = "embedded-hal_0_2")]
impl<'a, P: embedded_hal_legacy::digital::v2::OutputPin> Instrument for LegacyGpioRef<'a, P> {
    #[inline]
    fn on_enter(&mut self) {
        let _ = self.pin.set_high();
    }

    #[inline]
    fn on_exit(&mut self) {
        let _ = self.pin.set_low();
    }
}