Skip to content

Commit

Permalink
Merge #269
Browse files Browse the repository at this point in the history
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 <mroff@google.com>
  • Loading branch information
bors[bot] and Morgan Roff authored Apr 27, 2021
2 parents 4221894 + b37282e commit 1d22385
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- Added `IoPin` trait for pins that can change between being inputs or outputs
dynamically.

### Changed
- Swap PWM channel arguments to references
Expand Down
46 changes: 46 additions & 0 deletions src/digital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,49 @@ pub trait InputPin {
/// Is the input pin low?
fn try_is_low(&self) -> Result<bool, Self::Error>;
}

/// Single pin that can switch from input to output mode, and vice-versa.
///
/// Example use (assumes the `Error` type is the same for the `IoPin`,
/// `InputPin`, and `OutputPin`):
///
/// ```
/// use core::time::Duration;
/// use embedded_hal::digital::{IoPin, InputPin, OutputPin};
///
/// pub fn ping_and_read<TInputPin, TOutputPin, TError>(
/// mut pin: TOutputPin, delay_fn: &dyn Fn(Duration) -> ()) -> Result<bool, TError>
/// where
/// TInputPin : InputPin<Error = TError> + IoPin<TInputPin, TOutputPin, Error = TError>,
/// TOutputPin : OutputPin<Error = TError> + IoPin<TInputPin, TOutputPin, Error = TError>,
/// {
/// // Ping
/// pin.try_set_low()?;
/// delay_fn(Duration::from_millis(10));
/// pin.try_set_high()?;
///
/// // Read
/// let pin = pin.try_into_input_pin()?;
/// delay_fn(Duration::from_millis(10));
/// pin.try_is_high()
/// }
/// ```
pub trait IoPin<TInput, TOutput>
where
TInput: InputPin + IoPin<TInput, TOutput>,
TOutput: OutputPin + IoPin<TInput, TOutput>,
{
/// Error type.
type Error;

/// Tries to convert this pin to input mode.
///
/// If the pin is already in input mode, this method should succeed.
fn try_into_input_pin(self) -> Result<TInput, Self::Error>;

/// Tries to convert this pin to output mode with the given initial state.
///
/// If the pin is already in the requested state, this method should
/// succeed.
fn try_into_output_pin(self, state: PinState) -> Result<TOutput, Self::Error>;
}

0 comments on commit 1d22385

Please sign in to comment.