Skip to content

Commit

Permalink
Add an example of using interrupt directly without async (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyx0329 committed Sep 9, 2024
1 parent b9baeed commit e20ef5c
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions examples/button_interrupt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Toggle an LED on/off with a button
//!
//! This assumes that a LED is connected to GPIO4.
//! Additionally this assumes a button connected to GPIO9.
//! On an ESP32C3 development board this is the BOOT button.
//!
//! Depending on your target and the board you are using you should change the pins.
//! If your board doesn't have on-board LEDs don't forget to add an appropriate resistor.

use core::num::NonZero;
use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::gpio::{InterruptType, PinDriver, Pull};
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::task::notification::Notification;

fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take()?;
let mut led = PinDriver::output(peripherals.pins.gpio4)?;
let mut button = PinDriver::input(peripherals.pins.gpio9)?;

button.set_pull(Pull::Down)?;
button.set_interrupt_type(InterruptType::PosEdge)?;

let mut led_state = false;
led.set_low()?;

loop {
// prepare communication channel
let notification = Notification::new();
let waker = notification.notifier();

// register interrupt callback, here it's a closure on stack
unsafe {
button
.subscribe_nonstatic(move || {
waker.notify(NonZero::new(1).unwrap());
})
.unwrap();
}

// enable interrupt, will be automatically disabled after being triggered
button.enable_interrupt()?;
// block until notified
notification.wait_any();

// toggle the LED
if led_state {
led.set_low()?;
led_state = false;
} else {
led.set_high()?;
led_state = true;
}

// debounce
FreeRtos::delay_ms(200);
}
}

0 comments on commit e20ef5c

Please sign in to comment.