Skip to content

Add dummy GPIO pins #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- 10-bit addressing mode for I2C traits.
- Dummy GPIO pin (no-op, zero cost) struct `DummyPin`, useful when dealing
with optional pins.

### Changed

Expand Down
80 changes: 80 additions & 0 deletions src/digital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,83 @@ pub trait InputPin {
/// Is the input pin low?
fn try_is_low(&self) -> Result<bool, Self::Error>;
}

/// Dummy GPIO pin
///
/// These structures are useful when using optional pins, for example
/// when using some SPI devices.
pub mod dummy {
use super::{InputPin, OutputPin};
use core::{convert::Infallible, marker::PhantomData};

/// Pin level marker types for usage of `DummyPin` as an `InputPin`.
pub mod level {
/// `DummyPin` will always behave as being high when checked.
pub struct High;
/// `DummyPin` will always behave as being low when checked.
pub struct Low;
}

/// Dummy (no-op, zero-cost) pin
///
/// The implementation will discard any value written to it. When read,
/// it will always behave according to the value provided at construction
/// time (high or low).
pub struct DummyPin<L = level::Low> {
_l: PhantomData<L>,
}

impl DummyPin<level::Low> {
/// Create new instance
///
/// When read it will always behave as being low.
pub fn new_low() -> Self {
DummyPin { _l: PhantomData }
}
}

impl DummyPin<level::High> {
/// Create new instance
///
/// When read it will always behave as being high.
pub fn new_high() -> Self {
DummyPin { _l: PhantomData }
}
}

impl<L> OutputPin for DummyPin<L> {
type Error = Infallible;

fn try_set_high(&mut self) -> Result<(), Self::Error> {
Ok(())
}

fn try_set_low(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}

impl InputPin for DummyPin<level::Low> {
type Error = Infallible;

fn try_is_high(&self) -> Result<bool, Self::Error> {
Ok(false)
}

fn try_is_low(&self) -> Result<bool, Self::Error> {
Ok(true)
}
}

impl InputPin for DummyPin<level::High> {
type Error = Infallible;

fn try_is_high(&self) -> Result<bool, Self::Error> {
Ok(true)
}

fn try_is_low(&self) -> Result<bool, Self::Error> {
Ok(false)
}
}
}