Skip to content
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

Improvement: Infallible GPIO #140

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ fn main() -> anyhow::Result<()> {
let mut led = PinDriver::output(peripherals.pins.gpio4)?;

loop {
led.set_high()?;
led.set_high();
// we are sleeping here to make sure the watchdog isn't triggered
FreeRtos::delay_ms(1000);

led.set_low()?;
led.set_low();
FreeRtos::delay_ms(1000);
}
}
4 changes: 2 additions & 2 deletions examples/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ fn main() -> anyhow::Result<()> {
thread::sleep(Duration::from_millis(10));

if button.is_high() {
led.set_low()?;
led.set_low();
} else {
led.set_high()?;
led.set_high();
}
}
}
49 changes: 27 additions & 22 deletions src/gpio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! GPIO and pin configuration

use core::convert::Infallible;
use core::marker::PhantomData;

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -896,24 +897,24 @@ impl<'d, T: Pin, MODE> PinDriver<'d, T, MODE> {
}

#[inline]
pub fn set_high(&mut self) -> Result<(), EspError>
pub fn set_high(&mut self)
where
MODE: OutputMode,
{
self.set_level(Level::High)
self.set_level(Level::High);
}

/// Set the output as low.
#[inline]
pub fn set_low(&mut self) -> Result<(), EspError>
pub fn set_low(&mut self)
where
MODE: OutputMode,
{
self.set_level(Level::Low)
self.set_level(Level::Low);
}

#[inline]
pub fn set_level(&mut self, level: Level) -> Result<(), EspError>
pub fn set_level(&mut self, level: Level)
where
MODE: OutputMode,
{
Expand All @@ -924,27 +925,25 @@ impl<'d, T: Pin, MODE> PinDriver<'d, T, MODE> {

if MODE::RTC {
#[cfg(all(not(feature = "riscv-ulp-hal"), not(esp32c3)))]
esp!(unsafe { rtc_gpio_set_level(self.pin.pin(), on) })?;
esp!(unsafe { rtc_gpio_set_level(self.pin.pin(), on) }).unwrap();

#[cfg(any(feature = "riscv-ulp-hal", esp32c3))]
unreachable!();
} else {
esp!(unsafe { gpio_set_level(self.pin.pin(), on) })?;
esp!(unsafe { gpio_set_level(self.pin.pin(), on) }).unwrap();
}

Ok(())
}

/// Toggle pin output
#[inline]
pub fn toggle(&mut self) -> Result<(), EspError>
pub fn toggle(&mut self)
where
MODE: OutputMode,
{
if self.is_set_low() {
self.set_high()
self.set_high();
} else {
self.set_low()
self.set_low();
}
}

Expand Down Expand Up @@ -1122,7 +1121,7 @@ impl<'d, T: Pin, MODE> embedded_hal_0_2::digital::v2::InputPin for PinDriver<'d,
where
MODE: InputMode,
{
type Error = EspError;
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
Ok(PinDriver::is_high(self))
Expand All @@ -1134,7 +1133,7 @@ where
}

impl<'d, T: Pin, MODE> embedded_hal::digital::ErrorType for PinDriver<'d, T, MODE> {
type Error = EspError;
type Error = Infallible;
}

impl<'d, T: Pin, MODE> embedded_hal::digital::blocking::InputPin for PinDriver<'d, T, MODE>
Expand All @@ -1154,14 +1153,16 @@ impl<'d, T: Pin, MODE> embedded_hal_0_2::digital::v2::OutputPin for PinDriver<'d
where
MODE: OutputMode,
{
type Error = EspError;
type Error = Infallible;

fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_level(Level::High)
self.set_level(Level::High);
Ok(())
}

fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_level(Level::Low)
self.set_level(Level::Low);
Ok(())
}
}

Expand All @@ -1170,11 +1171,13 @@ where
MODE: OutputMode,
{
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_level(Level::High)
self.set_level(Level::High);
Ok(())
}

fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_level(Level::Low)
self.set_level(Level::Low);
Ok(())
}
}

Expand Down Expand Up @@ -1208,10 +1211,11 @@ impl<'d, T: Pin, MODE> embedded_hal_0_2::digital::v2::ToggleableOutputPin for Pi
where
MODE: OutputMode,
{
type Error = EspError;
type Error = Infallible;

fn toggle(&mut self) -> Result<(), Self::Error> {
self.set_level(Level::from(!bool::from(self.get_output_level())))
self.toggle();
Ok(())
}
}

Expand All @@ -1221,7 +1225,8 @@ where
MODE: OutputMode,
{
fn toggle(&mut self) -> Result<(), Self::Error> {
self.set_level(Level::from(!bool::from(self.get_output_level())))
self.toggle();
Ok(())
}
}

Expand Down