From a6cf482e92ee22db337158a39e0b7b0df8e9156a Mon Sep 17 00:00:00 2001 From: Michael Heinrichs Date: Tue, 4 Oct 2022 12:50:24 +0200 Subject: [PATCH 1/4] Make PinDriver's output methods infallible More specifically: - set_level - set_high/set_low - toggle --- src/gpio.rs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/gpio.rs b/src/gpio.rs index 667251e6983..89fb55f1b55 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -896,24 +896,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, { @@ -924,27 +924,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(); } } @@ -1157,11 +1155,13 @@ where type Error = EspError; 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(()) } } @@ -1170,11 +1170,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(()) } } @@ -1211,7 +1213,8 @@ where type Error = EspError; fn toggle(&mut self) -> Result<(), Self::Error> { - self.set_level(Level::from(!bool::from(self.get_output_level()))) + self.toggle(); + Ok(()) } } @@ -1221,7 +1224,9 @@ where MODE: OutputMode, { fn toggle(&mut self) -> Result<(), Self::Error> { - self.set_level(Level::from(!bool::from(self.get_output_level()))) + + self.toggle(); + Ok(()) } } From aec356adc1fe5467030e6a8d06d991f032958cbf Mon Sep 17 00:00:00 2001 From: Michael Heinrichs Date: Tue, 4 Oct 2022 12:50:24 +0200 Subject: [PATCH 2/4] Make gpio trait implementations infallible --- src/gpio.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/gpio.rs b/src/gpio.rs index 89fb55f1b55..6be03d01837 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -1,5 +1,6 @@ //! GPIO and pin configuration +use core::convert::Infallible; use core::marker::PhantomData; #[cfg(feature = "alloc")] @@ -1120,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 { Ok(PinDriver::is_high(self)) @@ -1132,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> @@ -1152,7 +1153,7 @@ 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); @@ -1210,7 +1211,7 @@ 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.toggle(); From 8c12d5e0cd2b477457a620381636feba47cd1b13 Mon Sep 17 00:00:00 2001 From: Michael Heinrichs Date: Sun, 9 Oct 2022 21:02:37 +0200 Subject: [PATCH 3/4] format --- src/gpio.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/gpio.rs b/src/gpio.rs index 6be03d01837..e4078e30b45 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -1225,7 +1225,6 @@ where MODE: OutputMode, { fn toggle(&mut self) -> Result<(), Self::Error> { - self.toggle(); Ok(()) } From 27e9396a447fd30020ce9cc5730d4e2194228c4d Mon Sep 17 00:00:00 2001 From: Michael Heinrichs Date: Sun, 9 Oct 2022 21:16:04 +0200 Subject: [PATCH 4/4] Fix examples --- examples/blinky.rs | 4 ++-- examples/button.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/blinky.rs b/examples/blinky.rs index 31422f16df7..a4ba3e28504 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -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); } } diff --git a/examples/button.rs b/examples/button.rs index e4a3126e618..a1cd8a3ff15 100644 --- a/examples/button.rs +++ b/examples/button.rs @@ -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(); } } }