From cfcdf928741b5ebb04a5b8cfe049f95113a793c2 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Wed, 22 Jun 2022 22:55:24 -0700 Subject: [PATCH] status state machine --- sphinx-key/src/core/events.rs | 10 ++++----- sphinx-key/src/periph/led.rs | 38 +++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 77f2208..aa8ce09 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -18,15 +18,15 @@ pub enum Event { Message(Vec), } -#[derive(Debug)] +#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)] pub enum Status { Starting, WifiAccessPoint, - WifiAccessPointClientConnected, + Configuring, ConnectingToWifi, ConnectingToMqtt, Connected, - MessageReceived, + Signing, } // the main event loop @@ -76,7 +76,7 @@ pub fn make_event_loop( led_tx.send(Status::Connected).unwrap(); } Event::Message(ref msg_bytes) => { - led_tx.send(Status::MessageReceived).unwrap(); + led_tx.send(Status::Signing).unwrap(); let _ret = match sphinx_key_signer::handle( &root_handler, msg_bytes.clone(), @@ -117,7 +117,7 @@ pub fn make_event_loop( .expect("could not MQTT subscribe"); } Event::Message(msg_bytes) => { - led_tx.send(Status::MessageReceived).unwrap(); + led_tx.send(Status::Signing).unwrap(); let b = sphinx_key_signer::parse_ping_and_form_response(msg_bytes); if do_log { log::info!("GOT A PING MESSAGE! returning pong now..."); diff --git a/sphinx-key/src/periph/led.rs b/sphinx-key/src/periph/led.rs index 620dc5a..550ba3c 100644 --- a/sphinx-key/src/periph/led.rs +++ b/sphinx-key/src/periph/led.rs @@ -8,6 +8,7 @@ use esp_idf_hal::rmt::{FixedLengthSignal, PinState, Pulse, Transmit}; use std::sync::mpsc; use std::thread; use std::time::Duration; +use std::collections::BTreeMap; use std::sync::{LazyLock, Mutex}; @@ -19,34 +20,55 @@ static TX: LazyLock, esp_idf_hal Mutex::new(Transmit::new(led, channel, &config).unwrap()) }); +type Color = u32; +type Time = u32; + pub struct Led { - brg: u32, - blink_length: u32, + brg: Color, + blink_length: Time, +} + +fn states() -> BTreeMap { + let mut s = BTreeMap::new(); + s.insert(Status::Starting, (0x000001, 100)); + s.insert(Status::WifiAccessPoint, (0x000100, 100)); + s.insert(Status::Configuring, (0x010000, 20)); + s.insert(Status::ConnectingToWifi, (0x010100, 350)); + s.insert(Status::ConnectingToMqtt, (0x010001, 100)); + s.insert(Status::Connected, (0x000101, 400)); + s.insert(Status::Signing, (0x111111, 100)); + s } pub fn led_control_loop(rx: mpsc::Receiver) { thread::spawn(move || { - 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 + let mut led = Led::new(0x000001, 100); loop { if let Ok(status) = rx.try_recv() { log::info!("LED STATUS: {:?}", status); - state = status; + if let Some(s) = states().get(&status) { + led.set(s.0, s.1); + } } - thread::sleep(Duration::from_millis(100)); + led.blink(); + thread::sleep(Duration::from_millis(400)); } }); } impl Led { - pub fn new(rgb: u32, blink_length: u32) -> Led { + pub fn new(rgb: Color, blink_length: Time) -> Led { Led { brg: rotate_rgb(rgb), blink_length, } } + pub fn set(&mut self, rgb: Color, blink_length: Time) { + self.brg = rotate_rgb(rgb); + self.blink_length = blink_length; + } + pub fn blink(&mut self) { let mut tx = TX.lock().unwrap(); // Prepare signal