diff options
fix: interrupts
-rw-r--r-- | src/bin/main.rs | 117 |
1 files changed, 85 insertions, 32 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index 246aa8f..e9b2386 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -13,7 +13,7 @@ #![no_std] #![no_main] -use core::cell::RefCell; +use core::cell::{OnceCell, RefCell}; use core::ops::Deref; use core::str::FromStr; @@ -45,6 +45,32 @@ use esp_wifi::{ use log::{debug, error, info, trace, warn}; use smoltcp::iface::{SocketSet, SocketStorage}; +pub enum OurOption<T> { + /// No value. + None, + /// Some value of type `T`. + Some(T), +} + +static BUTTON: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None)); +static mut IS_OPEN: bool = false; +static mut LED: OurOption<RefCell<Output<'_>>> = OurOption::None; + +fn set_led_state(val: bool) { + let v = unsafe { + #[allow(static_mut_refs)] + &LED + }; + let v = match v { + OurOption::None => { + return; + } + OurOption::Some(v) => v, + }; + let mut b = v.borrow_mut(); + b.set_level(if val { Level::High } else { Level::Low }); +} + #[main] fn main() -> ! { esp_println::logger::init_logger_from_env(); @@ -57,16 +83,33 @@ fn main() -> ! { info!("Preparing GPIO"); + let mut io = Io::new(peripherals.IO_MUX); + // Set the interrupt handler for GPIO interrupts. + io.set_interrupt_handler(handler); + // Set GPIO1 as an output, and set its state low initially. - let mut led = Output::new(peripherals.GPIO1, Level::Low); + let led = RefCell::new(Output::new(peripherals.GPIO1, Level::Low)); + let led = unsafe { + LED = OurOption::Some(led); + #[allow(static_mut_refs)] + match &LED { + OurOption::None => panic!(), + OurOption::Some(v) => v, + } + }; // Set GPIO4 as an input - let button = Input::new(peripherals.GPIO3, Pull::Up); + let mut button = Input::new(peripherals.GPIO3, Pull::Up); + + critical_section::with(|cs| { + button.listen(Event::FallingEdge); + BUTTON.borrow_ref_mut(cs).replace(button) + }); info!("Waiting 500ms pre-init"); delay.delay_millis(500); - led.set_low(); + led.borrow_mut().set_low(); let timg0 = TimerGroup::new(peripherals.TIMG0); @@ -119,9 +162,9 @@ fn main() -> ! { delay.delay_millis(1000); high = !high; if high { - led.set_high(); + led.borrow_mut().set_high(); } else { - led.set_low(); + led.borrow_mut().set_low(); } } } @@ -152,9 +195,9 @@ fn main() -> ! { for i in 0..4 { delay.delay_millis(100); if i % 2 == 0 { - led.set_high(); + led.borrow_mut().set_high(); } else { - led.set_low(); + led.borrow_mut().set_low(); } } @@ -167,38 +210,34 @@ fn main() -> ! { let mut tx_buffer = [0u8; 1536]; let mut socket = stack.get_socket(&mut rx_buffer, &mut tx_buffer); - let mut is_open = false; - socket.listen(8080).unwrap(); - fn update_open(is_open: bool, button: &Input<'_>, led: &mut Output<'_>, delay: &Delay) -> bool { - let mut is_open = if is_open { true } else { false }; - if button.is_low() { - is_open = !is_open; - - if is_open { - led.set_high(); - } else { - led.set_low(); - } - while button.is_low() { - trace!("waiting for unpress btn"); - delay.delay_millis(10); - } - } - is_open - } + // fn update_open(is_open: bool, button: &Input<'_>, led: &mut Output<'_>, delay: &Delay) -> bool { + // let mut is_open = if is_open { true } else { false }; + // if button.is_low() { + // is_open = !is_open; + + // if is_open { + // led.set_high(); + // } else { + // led.set_low(); + // } + // while button.is_low() { + // trace!("waiting for unpress btn"); + // delay.delay_millis(10); + // } + // } + // is_open + // } loop { socket.work(); - is_open = update_open(is_open, &button, &mut led, &delay); if !socket.is_open() { socket.listen(8080).unwrap(); } if socket.is_connected() { - is_open = update_open(is_open, &button, &mut led, &delay); // debug!("Established Connection"); // let mut time_out = false; @@ -229,7 +268,7 @@ fn main() -> ! { // break; // } // } - + let is_open = unsafe { IS_OPEN }; let parts = [ (r##"{ "api": "0.13", @@ -308,13 +347,11 @@ UwU: if u read this u have been catgirled :3\r\n\ // } socket.close(); - is_open = update_open(is_open, &button, &mut led, &delay); } // TODO: what let deadline = time::now() + Duration::millis(100); while time::now() < deadline && !socket.is_connected() { socket.work(); - is_open = update_open(is_open, &button, &mut led, &delay); } } } @@ -326,3 +363,19 @@ fn parse_ip(ip: &str) -> [u8; 4] { } result } + +#[handler] +fn handler() { + critical_section::with(|cs| { + info!("GPIO interrupt"); + let mut binding = BUTTON.borrow_ref_mut(cs); + let v = binding.as_mut().unwrap(); + if v.is_low() { + unsafe { + IS_OPEN = !IS_OPEN; + set_led_state(IS_OPEN); + } + } + v.clear_interrupt(); + }); +} |