embedded_trace/instruments/
gpio.rs

1//! [`Instrument`] implementations for GPIO pins.
2//!
3//! Types implementing `embedded-hal v1.0`
4//! [`OutputPin`](embedded_hal_1::digital::OutputPin) and `embedded-hal v0.2`
5//! [`OutputPin`](embedded_hal_0_2::digital::v2::OutputPin) are supported.
6
7use crate::Instrument;
8use embedded_hal_1 as embedded_hal;
9
10#[cfg(feature = "embedded-hal_0_2")]
11use embedded_hal_0_2 as embedded_hal_legacy;
12
13/// [`Instrument`] implementation for an [`OutputPin`] (`embedded-hal` version
14/// 1.0).
15///
16/// This type is also available as [`GpioRef`], which does not consume the
17/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
18/// supports `embedded-hal` version 0.2, see [`LegacyGpio`].
19///
20/// [`OutputPin`]: embedded_hal::digital::OutputPin
21pub struct Gpio<P: embedded_hal::digital::OutputPin> {
22    pin: P,
23}
24
25impl<P: embedded_hal::digital::OutputPin> From<P> for Gpio<P> {
26    #[inline]
27    fn from(pin: P) -> Self {
28        Self::new(pin)
29    }
30}
31
32impl<P: embedded_hal::digital::OutputPin> Gpio<P> {
33    /// Create a new [`Gpio`].
34    #[inline]
35    pub fn new(pin: P) -> Self {
36        Self { pin }
37    }
38
39    /// Return the underlying [`OutputPin`](embedded_hal::digital::OutputPin).
40    #[inline]
41    pub fn free(self) -> P {
42        self.pin
43    }
44}
45
46impl<P: embedded_hal::digital::OutputPin> Instrument for Gpio<P> {
47    #[inline]
48    fn on_enter(&mut self) {
49        let _ = self.pin.set_high();
50    }
51
52    #[inline]
53    fn on_exit(&mut self) {
54        let _ = self.pin.set_low();
55    }
56}
57
58/// Reference-taking version of an [`Instrument`] implementation for an
59/// [`OutputPin`] (`embedded-hal` version 1.0).
60///
61/// This type is also available as [`Gpio`], which does consumes the
62/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
63/// supports `embedded-hal` version 0.2, see [`LegacyGpioRef`].
64///
65/// [`OutputPin`]: embedded_hal::digital::OutputPin
66pub struct GpioRef<'a, P: embedded_hal::digital::OutputPin> {
67    pin: &'a mut P,
68}
69
70impl<'a, P: embedded_hal::digital::OutputPin> GpioRef<'a, P> {
71    /// Create a new [`GpioRef`].
72    #[inline]
73    pub fn new(pin: &'a mut P) -> Self {
74        Self { pin }
75    }
76}
77
78impl<'a, P: embedded_hal::digital::OutputPin> Instrument for GpioRef<'a, P> {
79    #[inline]
80    fn on_enter(&mut self) {
81        let _ = self.pin.set_high();
82    }
83
84    #[inline]
85    fn on_exit(&mut self) {
86        let _ = self.pin.set_low();
87    }
88}
89
90/// [`Instrument`] implementation for an [`OutputPin`] (`embedded-hal` version
91/// 0.2).
92///
93/// This type is also available as [`LegacyGpioRef`], which does not consume the
94/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
95/// supports `embedded-hal` version 1.0, see [`Gpio`].
96///
97/// [`OutputPin`]: embedded_hal::digital::OutputPin
98#[cfg(feature = "embedded-hal_0_2")]
99pub struct LegacyGpio<P: embedded_hal_legacy::digital::v2::OutputPin> {
100    pin: P,
101}
102
103#[cfg(feature = "embedded-hal_0_2")]
104impl<P: embedded_hal_legacy::digital::v2::OutputPin> From<P> for LegacyGpio<P> {
105    fn from(pin: P) -> Self {
106        Self { pin }
107    }
108}
109
110#[cfg(feature = "embedded-hal_0_2")]
111impl<P: embedded_hal_legacy::digital::v2::OutputPin> LegacyGpio<P> {
112    /// Create a new [`LegacyGpio`].
113    #[inline]
114    pub fn new(pin: P) -> Self {
115        Self { pin }
116    }
117
118    /// Return the underlying
119    /// [`OutputPin`](embedded_hal_legacy::digital::v2::OutputPin).
120    #[inline]
121    pub fn free(self) -> P {
122        self.pin
123    }
124}
125
126#[cfg(feature = "embedded-hal_0_2")]
127impl<P: embedded_hal_legacy::digital::v2::OutputPin> Instrument for LegacyGpio<P> {
128    #[inline]
129    fn on_enter(&mut self) {
130        let _ = self.pin.set_high();
131    }
132
133    #[inline]
134    fn on_exit(&mut self) {
135        let _ = self.pin.set_low();
136    }
137}
138
139/// Reference-taking version of an [`Instrument`] implementation for an
140/// [`OutputPin`] (`embedded-hal` version 0.2).
141///
142/// This type is also available as [`LegacyGpio`], which consumes the
143/// underlying [`OutputPin`]. For an [`Instrument`] implementation which
144/// supports `embedded-hal` version 1.0, see [`GpioRef`].
145///
146/// [`OutputPin`]: embedded_hal::digital::OutputPin
147#[cfg(feature = "embedded-hal_0_2")]
148pub struct LegacyGpioRef<'a, P: embedded_hal_legacy::digital::v2::OutputPin> {
149    pin: &'a mut P,
150}
151
152#[cfg(feature = "embedded-hal_0_2")]
153impl<'a, P: embedded_hal_legacy::digital::v2::OutputPin> LegacyGpioRef<'a, P> {
154    /// Create a new [`LegacyGpioRef`].
155    #[inline]
156    pub fn new(pin: &'a mut P) -> Self {
157        Self { pin }
158    }
159}
160
161#[cfg(feature = "embedded-hal_0_2")]
162impl<'a, P: embedded_hal_legacy::digital::v2::OutputPin> Instrument for LegacyGpioRef<'a, P> {
163    #[inline]
164    fn on_enter(&mut self) {
165        let _ = self.pin.set_high();
166    }
167
168    #[inline]
169    fn on_exit(&mut self) {
170        let _ = self.pin.set_low();
171    }
172}