Skip to content

Commit

Permalink
Add digital::IoPin trait (closes #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Roff authored and MorganR committed Apr 27, 2021
1 parent 4221894 commit b37282e
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 b37282e

Please sign in to comment.