diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 738d4ba..77f2208 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -20,11 +20,12 @@ pub enum Event { #[derive(Debug)] pub enum Status { + Starting, WifiAccessPoint, WifiAccessPointClientConnected, ConnectingToWifi, ConnectingToMqtt, - ConnectedToMqtt, + Connected, MessageReceived, } @@ -45,7 +46,7 @@ pub fn make_event_loop( log::info!("SUBSCRIBE to {}", TOPIC); mqtt.subscribe(TOPIC, QOS) .expect("could not MQTT subscribe"); - led_tx.send(Status::ConnectedToMqtt).unwrap(); + led_tx.send(Status::Connected).unwrap(); } Event::Message(ref msg_bytes) => { let InitResponse { @@ -72,7 +73,7 @@ pub fn make_event_loop( log::info!("SUBSCRIBE TO {}", TOPIC); mqtt.subscribe(TOPIC, QOS) .expect("could not MQTT subscribe"); - led_tx.send(Status::ConnectedToMqtt).unwrap(); + led_tx.send(Status::Connected).unwrap(); } Event::Message(ref msg_bytes) => { led_tx.send(Status::MessageReceived).unwrap(); diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index fae467f..16ad9ab 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -29,6 +29,10 @@ const NETWORK: Option<&'static str> = option_env!("NETWORK"); fn main() -> Result<()> { + // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, + // or else some patches to the runtime implemented by esp-idf-sys might not link properly. + esp_idf_sys::link_patches(); + let network: Network = if let Some(n) = NETWORK { match n { "bitcoin" => Network::Bitcoin, @@ -42,10 +46,6 @@ fn main() -> Result<()> { Network::Regtest }; - // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, - // or else some patches to the runtime implemented by esp-idf-sys might not link properly. - esp_idf_sys::link_patches(); - esp_idf_svc::log::EspLogger::initialize_default(); thread::sleep(Duration::from_secs(1)); diff --git a/sphinx-key/src/periph/led.rs b/sphinx-key/src/periph/led.rs index ca0bcd4..620dc5a 100644 --- a/sphinx-key/src/periph/led.rs +++ b/sphinx-key/src/periph/led.rs @@ -1,5 +1,4 @@ use crate::core::events::Status; -use core::time::Duration; use embedded_hal::delay::blocking::DelayUs; use esp_idf_hal::delay::Ets; use esp_idf_hal::gpio::Gpio8; @@ -8,6 +7,7 @@ use esp_idf_hal::rmt::config::TransmitConfig; use esp_idf_hal::rmt::{FixedLengthSignal, PinState, Pulse, Transmit}; use std::sync::mpsc; use std::thread; +use std::time::Duration; use std::sync::{LazyLock, Mutex}; @@ -26,8 +26,15 @@ pub struct Led { pub fn led_control_loop(rx: mpsc::Receiver) { thread::spawn(move || { - while let Ok(status) = rx.recv() { - log::info!("LED STATUS: {:?}", status); + let mut state: Status = Status::Starting; + // each iteration: check if theres a new status, and change the color if so + // if not, continue the current status blink color/time + loop { + if let Ok(status) = rx.try_recv() { + log::info!("LED STATUS: {:?}", status); + state = status; + } + thread::sleep(Duration::from_millis(100)); } }); }