Skip to content

Commit

Permalink
Fix eh0.2 set_high/set_low (esp-rs#2265)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ authored Oct 2, 2024
1 parent f5b8e4b commit a3304c6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
4 changes: 2 additions & 2 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2601,11 +2601,11 @@ mod embedded_hal_02_impls {
type Error = core::convert::Infallible;

fn set_high(&mut self) -> Result<(), Self::Error> {
self.pin.is_set_high();
self.pin.set_high();
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.pin.is_set_low();
self.pin.set_low();
Ok(())
}
}
Expand Down
94 changes: 94 additions & 0 deletions hil-test/tests/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,100 @@ mod tests {
assert_eq!(test_gpio2.is_set_high(), true);
}

#[test]
fn test_gpio_output_embedded_hal_0_2(ctx: Context) {
let test_gpio1 = Input::new(ctx.test_gpio1, Pull::Down);
let mut test_gpio2 = Output::new(ctx.test_gpio2, Level::Low);

fn set<T>(pin: &mut T, state: bool)
where
T: embedded_hal_02::digital::v2::OutputPin,
{
if state {
pin.set_high().ok();
} else {
pin.set_low().ok();
}
}

fn toggle<T>(pin: &mut T)
where
T: embedded_hal_02::digital::v2::ToggleableOutputPin,
{
pin.toggle().ok();
}

// `StatefulOutputPin`:
assert_eq!(test_gpio2.is_set_low(), true);
assert_eq!(test_gpio2.is_set_high(), false);
assert_eq!(test_gpio1.is_low(), true);
assert_eq!(test_gpio1.is_high(), false);
set(&mut test_gpio2, true);
assert_eq!(test_gpio2.is_set_low(), false);
assert_eq!(test_gpio2.is_set_high(), true);
assert_eq!(test_gpio1.is_low(), false);
assert_eq!(test_gpio1.is_high(), true);

// `ToggleableOutputPin`:
toggle(&mut test_gpio2);
assert_eq!(test_gpio2.is_set_low(), true);
assert_eq!(test_gpio2.is_set_high(), false);
assert_eq!(test_gpio1.is_low(), true);
assert_eq!(test_gpio1.is_high(), false);
toggle(&mut test_gpio2);
assert_eq!(test_gpio2.is_set_low(), false);
assert_eq!(test_gpio2.is_set_high(), true);
assert_eq!(test_gpio1.is_low(), false);
assert_eq!(test_gpio1.is_high(), true);
}

#[test]
fn test_gpio_output_embedded_hal_1_0(ctx: Context) {
let test_gpio1 = Input::new(ctx.test_gpio1, Pull::Down);
let mut test_gpio2 = Output::new(ctx.test_gpio2, Level::Low);

fn set<T>(pin: &mut T, state: bool)
where
T: embedded_hal::digital::OutputPin,
{
if state {
pin.set_high().ok();
} else {
pin.set_low().ok();
}
}

fn toggle<T>(pin: &mut T)
where
T: embedded_hal::digital::StatefulOutputPin,
{
pin.toggle().ok();
}

// `StatefulOutputPin`:
assert_eq!(test_gpio2.is_set_low(), true);
assert_eq!(test_gpio2.is_set_high(), false);
assert_eq!(test_gpio1.is_low(), true);
assert_eq!(test_gpio1.is_high(), false);
set(&mut test_gpio2, true);
assert_eq!(test_gpio2.is_set_low(), false);
assert_eq!(test_gpio2.is_set_high(), true);
assert_eq!(test_gpio1.is_low(), false);
assert_eq!(test_gpio1.is_high(), true);

// `ToggleableOutputPin`:
toggle(&mut test_gpio2);
assert_eq!(test_gpio2.is_set_low(), true);
assert_eq!(test_gpio2.is_set_high(), false);
assert_eq!(test_gpio1.is_low(), true);
assert_eq!(test_gpio1.is_high(), false);
toggle(&mut test_gpio2);
assert_eq!(test_gpio2.is_set_low(), false);
assert_eq!(test_gpio2.is_set_high(), true);
assert_eq!(test_gpio1.is_low(), false);
assert_eq!(test_gpio1.is_high(), true);
}

#[test]
fn test_gpio_interrupt(ctx: Context) {
let mut test_gpio1 = Input::new(ctx.test_gpio1, Pull::Down);
Expand Down

0 comments on commit a3304c6

Please sign in to comment.