From 66a97af6ef7c38fec90bcbfa7d9e75105a7f4833 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 22 Jul 2020 09:04:07 +0200 Subject: [PATCH 1/6] Add dummy GPIO pin --- CHANGELOG.md | 2 ++ src/digital.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d3c4a147..d3c867ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/digital.rs b/src/digital.rs index 05897fc84..040b5a839 100644 --- a/src/digital.rs +++ b/src/digital.rs @@ -123,3 +123,82 @@ pub trait InputPin { /// Is the input pin low? fn try_is_low(&self) -> Result; } + +/// 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 + /// + /// This will discard any value set to it and when checked always behave + /// according to the value provided at construction time (high/low). + pub struct DummyPin { + _l: PhantomData, + } + + impl DummyPin { + /// Create new instance + /// + /// When checked it will always behave as being low. + pub fn new_low() -> Self { + DummyPin { _l: PhantomData } + } + } + + impl DummyPin { + /// Create new instance + /// + /// When checked it will always behave as being high. + pub fn new_high() -> Self { + DummyPin { _l: PhantomData } + } + } + + impl OutputPin for DummyPin { + 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 { + type Error = Infallible; + + fn try_is_high(&self) -> Result { + Ok(false) + } + + fn try_is_low(&self) -> Result { + Ok(true) + } + } + + impl InputPin for DummyPin { + type Error = Infallible; + + fn try_is_high(&self) -> Result { + Ok(true) + } + + fn try_is_low(&self) -> Result { + Ok(false) + } + } +} From 1ca18a29a84fd556920ac67a0df4b02aed21be16 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 10 Mar 2021 08:18:30 +0100 Subject: [PATCH 2/6] Update CHANGELOG.md Co-authored-by: Danilo Bargen --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3c867ca1..d2d33c64d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ 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 +- Dummy GPIO pin (no-op, zero cost) struct `DummyPin`, useful when dealing with optional pins. ### Changed From e4deceabe5362fd16427e6ffb4464e42b05241f6 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 10 Mar 2021 08:18:36 +0100 Subject: [PATCH 3/6] Update src/digital.rs Co-authored-by: Danilo Bargen --- src/digital.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/digital.rs b/src/digital.rs index 040b5a839..dda8e8e6b 100644 --- a/src/digital.rs +++ b/src/digital.rs @@ -140,7 +140,7 @@ pub mod dummy { pub struct Low; } - /// Dummy (No-op, zero-cost) pin + /// Dummy (no-op, zero-cost) pin /// /// This will discard any value set to it and when checked always behave /// according to the value provided at construction time (high/low). From 2d2b8aa41e301034cf8958e5d6fe88dd8a066d76 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 10 Mar 2021 08:18:46 +0100 Subject: [PATCH 4/6] Update src/digital.rs Co-authored-by: Danilo Bargen --- src/digital.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/digital.rs b/src/digital.rs index dda8e8e6b..0c124cdbd 100644 --- a/src/digital.rs +++ b/src/digital.rs @@ -142,8 +142,9 @@ pub mod dummy { /// Dummy (no-op, zero-cost) pin /// - /// This will discard any value set to it and when checked always behave - /// according to the value provided at construction time (high/low). + /// 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: PhantomData, } From 0c9392092760d2cb8a0c77df29e3bfdfe984859e Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 10 Mar 2021 08:18:53 +0100 Subject: [PATCH 5/6] Update src/digital.rs Co-authored-by: Danilo Bargen --- src/digital.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/digital.rs b/src/digital.rs index 0c124cdbd..0b4e7004e 100644 --- a/src/digital.rs +++ b/src/digital.rs @@ -161,7 +161,7 @@ pub mod dummy { impl DummyPin { /// Create new instance /// - /// When checked it will always behave as being high. + /// When read it will always behave as being high. pub fn new_high() -> Self { DummyPin { _l: PhantomData } } From 93e472ad6b6d4eb8652925e788c13c7be94e3202 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 10 Mar 2021 08:19:00 +0100 Subject: [PATCH 6/6] Update src/digital.rs Co-authored-by: Danilo Bargen --- src/digital.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/digital.rs b/src/digital.rs index 0b4e7004e..a38857f78 100644 --- a/src/digital.rs +++ b/src/digital.rs @@ -152,7 +152,7 @@ pub mod dummy { impl DummyPin { /// Create new instance /// - /// When checked it will always behave as being low. + /// When read it will always behave as being low. pub fn new_low() -> Self { DummyPin { _l: PhantomData } }