From 4c242c298353b15d8c91486f67c1812d3a2d0b52 Mon Sep 17 00:00:00 2001 From: Luca Date: Sat, 20 Apr 2024 19:48:20 +0200 Subject: [PATCH] fix: clear button interrupt --- src/main.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index cbd323c..eb7742f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ #![no_std] #![no_main] -use core::cell::Cell; +use core::cell::{Cell, RefCell}; use critical_section::Mutex; use esp_hal::clock::ClockControl; use esp_hal::delay::Delay; -use esp_hal::gpio::{Event, IO}; +use esp_hal::gpio::{Event, GpioPin, Input, PullUp, IO}; use esp_hal::ledc::{channel, timer, LSGlobalClkSource, LEDC}; use esp_hal::peripherals::Peripherals; use esp_hal::prelude::*; @@ -26,6 +26,8 @@ const COLORS: [[u8; 3]; 6] = [ [1, 0, 1], ]; +static BUTTON: Mutex, 9>>>> = Mutex::new(RefCell::new(None)); + static LAST_TRIGGERED: Mutex> = Mutex::new(Cell::new(0)); static STOP_CHASE: Mutex> = Mutex::new(Cell::new(false)); @@ -84,7 +86,9 @@ fn main() -> ! { }) .unwrap(); - io.pins.gpio9.into_pull_up_input().listen(Event::RisingEdge); + let mut btn = io.pins.gpio9.into_pull_up_input(); + btn.listen(Event::RisingEdge); + critical_section::with(|cs| BUTTON.borrow(cs).replace(Some(btn))); let delay = Delay::new(&clocks); @@ -106,13 +110,15 @@ fn main() -> ! { fn gpio_handler() { critical_section::with(|cs| { let last_triggered = LAST_TRIGGERED.borrow(cs); - if last_triggered.get() + BUTTON_TIMEOUT > SystemTimer::now() { - return; + if last_triggered.get() + BUTTON_TIMEOUT < SystemTimer::now() { + let stop_chase = STOP_CHASE.borrow(cs); + stop_chase.set(!stop_chase.get()); + + last_triggered.set(SystemTimer::now()); } - let stop_chase = STOP_CHASE.borrow(cs); - stop_chase.set(!stop_chase.get()); - - last_triggered.set(SystemTimer::now()); + if let Some(button) = BUTTON.borrow(cs).borrow_mut().as_mut() { + button.clear_interrupt(); + } }); }