Skip to content

Commit 2eb2b64

Browse files
bors[bot]Morgan Roff
and
Morgan Roff
authored
Merge #269
269: Add digital::IoPin to resolve issue 29 r=therealprof a=MorganR Closes #29. This implementation is inspired by [chrismoos's comment](#29 (comment)) in #29. I've demonstrated using this trait for a [no-std DHT11 driver](https://github.com/MorganR/rust-simple-sensors/blob/main/src/dht11.rs), and verified this on a Raspberry Pi 3B+ using an implementation in linux-embedded-hal (PR to follow). I'll also be sending a PR to implement this in at least one additional HAL implementation library (recommendations welcome). One question I have is how copyright/authorship is tracked in this repo? I believe the copyright owner for my code would technically be Google LLC. When patching repos, I'm meant to add that to the appropriate copyright/authors list. Co-authored-by: Morgan Roff <[email protected]>
2 parents 4221894 + 4306114 commit 2eb2b64

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased]
99

1010
### Added
11+
- Added `IoPin` trait for pins that can change between being inputs or outputs
12+
dynamically.
1113

1214
### Changed
1315
- Swap PWM channel arguments to references

src/digital.rs

+46
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,49 @@ pub trait InputPin {
174174
/// Is the input pin low?
175175
fn try_is_low(&self) -> Result<bool, Self::Error>;
176176
}
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

Comments
 (0)