@@ -174,3 +174,49 @@ pub trait InputPin {
174
174
/// Is the input pin low?
175
175
fn try_is_low ( & self ) -> Result < bool , Self :: Error > ;
176
176
}
177
+
178
+ /// Single pin that can switch from input to output mode, and vice-versa.
179
+ ///
180
+ /// Example use (assumes the `Error` type is the same for the `IoPin`,
181
+ /// `InputPin`, and `OutputPin`):
182
+ ///
183
+ /// ```
184
+ /// use core::time::Duration;
185
+ /// use embedded_hal::digital::{IoPin, InputPin, OutputPin};
186
+ ///
187
+ /// pub fn ping_and_read<TInputPin, TOutputPin, TError>(
188
+ /// mut pin: TOutputPin, delay_fn: &dyn Fn(Duration) -> ()) -> Result<bool, TError>
189
+ /// where
190
+ /// TInputPin : InputPin<Error = TError> + IoPin<TInputPin, TOutputPin, Error = TError>,
191
+ /// TOutputPin : OutputPin<Error = TError> + IoPin<TInputPin, TOutputPin, Error = TError>,
192
+ /// {
193
+ /// // Ping
194
+ /// pin.try_set_low()?;
195
+ /// delay_fn(Duration::from_millis(10));
196
+ /// pin.try_set_high()?;
197
+ ///
198
+ /// // Read
199
+ /// let pin = pin.try_into_input_pin()?;
200
+ /// delay_fn(Duration::from_millis(10));
201
+ /// pin.try_is_high()
202
+ /// }
203
+ /// ```
204
+ pub trait IoPin < TInput , TOutput >
205
+ where
206
+ TInput : InputPin + IoPin < TInput , TOutput > ,
207
+ TOutput : OutputPin + IoPin < TInput , TOutput > ,
208
+ {
209
+ /// Error type.
210
+ type Error ;
211
+
212
+ /// Tries to convert this pin to input mode.
213
+ ///
214
+ /// If the pin is already in input mode, this method should succeed.
215
+ fn try_into_input_pin ( self ) -> Result < TInput , Self :: Error > ;
216
+
217
+ /// Tries to convert this pin to output mode with the given initial state.
218
+ ///
219
+ /// If the pin is already in the requested state, this method should
220
+ /// succeed.
221
+ fn try_into_output_pin ( self , state : PinState ) -> Result < TOutput , Self :: Error > ;
222
+ }
0 commit comments