Skip to content

Commit

Permalink
Merge pull request #694 from jannic/issue-689-implement-input
Browse files Browse the repository at this point in the history
Implement InputPin for all pin configurations via .as_input() function
  • Loading branch information
jannic authored Sep 28, 2023
2 parents fcebdea + ec1b013 commit d70164c
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions rp2040-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,17 @@ impl<I: PinId, F: func::Function, P: PullType> Pin<I, F, P> {
}
}
}

/// Return a wrapper that implements InputPin.
///
/// This allows to read from the pin independent of the selected function.
/// Depending on the pad configuration, reading from the pin may not return a
/// meaningful result.
///
/// Calling this function does not set the pad's input enable bit.
pub fn as_input(&self) -> AsInputPin<I, F, P> {
AsInputPin(self)
}
}
impl<I: PinId, C: SioConfig, P: PullType> Pin<I, FunctionSio<C>, P> {
/// Is bypass enabled
Expand Down Expand Up @@ -842,6 +853,9 @@ impl<I: PinId, F: func::Function> Pin<I, F, DynPullType> {
}
}

/// Wrapper providing input pin functions for GPIO pins independent of the configured mode.
pub struct AsInputPin<'a, I: PinId, F: func::Function, P: PullType>(&'a Pin<I, F, P>);

//==============================================================================
// Embedded-HAL
//==============================================================================
Expand All @@ -867,6 +881,8 @@ where
}
}

/// Deprecated: Instead of implicitly implementing InputPin for function SioOutput,
/// use `pin.as_input()` to get access to input values indepentent of the selected function.
impl<I, P> embedded_hal::digital::v2::InputPin for Pin<I, FunctionSio<SioOutput>, P>
where
I: PinId,
Expand All @@ -883,6 +899,20 @@ where
}
}

impl<'a, I: PinId, F: func::Function, P: PullType> embedded_hal::digital::v2::InputPin
for AsInputPin<'a, I, F, P>
{
type Error = core::convert::Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.0._is_high())
}

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.0._is_low())
}
}

impl<I, P> embedded_hal::digital::v2::StatefulOutputPin for Pin<I, FunctionSio<SioOutput>, P>
where
I: PinId,
Expand Down Expand Up @@ -1399,20 +1429,6 @@ mod eh1 {
}
}

impl<I, P> InputPin for Pin<I, FunctionSio<SioOutput>, P>
where
I: PinId,
P: PullType,
{
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self._is_high())
}

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self._is_low())
}
}

impl<I, P> StatefulOutputPin for Pin<I, FunctionSio<SioOutput>, P>
where
I: PinId,
Expand All @@ -1437,6 +1453,7 @@ mod eh1 {
Ok(())
}
}

impl<I, P> InputPin for Pin<I, FunctionSio<SioInput>, P>
where
I: PinId,
Expand All @@ -1450,4 +1467,25 @@ mod eh1 {
Ok(self._is_low())
}
}

impl<'a, I, F, P> ErrorType for super::AsInputPin<'a, I, F, P>
where
I: PinId,
F: super::func::Function,
P: PullType,
{
type Error = Error;
}

impl<'a, I: PinId, F: super::func::Function, P: PullType> InputPin
for super::AsInputPin<'a, I, F, P>
{
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.0._is_high())
}

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.0._is_low())
}
}
}

0 comments on commit d70164c

Please sign in to comment.