mirror of
https://github.com/stakwork/sphinx-key.git
synced 2026-02-23 00:24:18 +01:00
top Status enum, separate thread for LED states
This commit is contained in:
@@ -18,12 +18,24 @@ pub enum Event {
|
||||
Message(Vec<u8>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Status {
|
||||
WifiAccessPoint,
|
||||
WifiAccessPointClientConnected,
|
||||
ConnectingToWifi,
|
||||
ConnectingToMqtt,
|
||||
ConnectedToMqtt,
|
||||
MessageReceived,
|
||||
}
|
||||
|
||||
// the main event loop
|
||||
#[cfg(not(feature = "pingpong"))]
|
||||
pub fn make_event_loop(
|
||||
mut mqtt: EspMqttClient<ConnState<MessageImpl, EspError>>,
|
||||
rx: mpsc::Receiver<Event>,
|
||||
network: Network,
|
||||
do_log: bool,
|
||||
led_tx: mpsc::Sender<Status>
|
||||
) -> Result<()> {
|
||||
// initialize the RootHandler
|
||||
let root_handler = loop {
|
||||
@@ -33,6 +45,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();
|
||||
}
|
||||
Event::Message(ref msg_bytes) => {
|
||||
let InitResponse {
|
||||
@@ -44,6 +57,7 @@ pub fn make_event_loop(
|
||||
break root_handler;
|
||||
}
|
||||
Event::Disconnected => {
|
||||
led_tx.send(Status::ConnectingToMqtt).unwrap();
|
||||
log::info!("GOT an early Event::Disconnected msg!");
|
||||
}
|
||||
}
|
||||
@@ -58,8 +72,10 @@ 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();
|
||||
}
|
||||
Event::Message(ref msg_bytes) => {
|
||||
led_tx.send(Status::MessageReceived).unwrap();
|
||||
let _ret = match sphinx_key_signer::handle(
|
||||
&root_handler,
|
||||
msg_bytes.clone(),
|
||||
@@ -73,6 +89,7 @@ pub fn make_event_loop(
|
||||
};
|
||||
}
|
||||
Event::Disconnected => {
|
||||
led_tx.send(Status::ConnectingToMqtt).unwrap();
|
||||
log::info!("GOT A Event::Disconnected msg!");
|
||||
}
|
||||
}
|
||||
@@ -87,16 +104,19 @@ pub fn make_event_loop(
|
||||
rx: mpsc::Receiver<Event>,
|
||||
_network: Network,
|
||||
do_log: bool,
|
||||
led_tx: mpsc::Sender<Status>
|
||||
) -> Result<()> {
|
||||
log::info!("About to subscribe to the mpsc channel");
|
||||
while let Ok(event) = rx.recv() {
|
||||
match event {
|
||||
Event::Connected => {
|
||||
led_tx.send(Status::ConnectedToMqtt).unwrap();
|
||||
log::info!("SUBSCRIBE TO {}", TOPIC);
|
||||
mqtt.subscribe(TOPIC, QOS)
|
||||
.expect("could not MQTT subscribe");
|
||||
}
|
||||
Event::Message(msg_bytes) => {
|
||||
led_tx.send(Status::MessageReceived).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...");
|
||||
@@ -105,6 +125,7 @@ pub fn make_event_loop(
|
||||
.expect("could not publish ping response");
|
||||
}
|
||||
Event::Disconnected => {
|
||||
led_tx.send(Status::ConnectingToMqtt).unwrap();
|
||||
log::info!("GOT A Event::Disconnected msg!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ mod core;
|
||||
mod periph;
|
||||
|
||||
use crate::core::{events::*, config::*};
|
||||
use crate::periph::led::Led;
|
||||
use crate::periph::led::{Led, led_control_loop};
|
||||
|
||||
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
|
||||
use std::thread;
|
||||
@@ -50,16 +50,22 @@ fn main() -> Result<()> {
|
||||
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
|
||||
let (led_tx, led_rx) = mpsc::channel();
|
||||
// LED control thread
|
||||
led_control_loop(led_rx);
|
||||
|
||||
let default_nvs = Arc::new(EspDefaultNvs::new()?);
|
||||
let mut store = EspNvsStorage::new_default(default_nvs.clone(), "sphinx", true).expect("no storage");
|
||||
let existing: Option<Config> = store.get("config").expect("failed");
|
||||
if let Some(exist) = existing {
|
||||
println!("=============> START CLIENT NOW <============== {:?}", exist);
|
||||
// store.remove("config").expect("couldnt remove config");
|
||||
led_tx.send(Status::ConnectingToWifi).unwrap();
|
||||
let wifi = start_wifi_client(default_nvs.clone(), &exist)?;
|
||||
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
led_tx.send(Status::ConnectingToMqtt).unwrap();
|
||||
// _conn needs to stay in scope or its dropped
|
||||
let (mqtt, connection) = conn::mqtt::make_client(&exist.broker, CLIENT_ID)?;
|
||||
let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?;
|
||||
@@ -67,17 +73,10 @@ fn main() -> Result<()> {
|
||||
// this blocks forever... the "main thread"
|
||||
log::info!(">>>>>>>>>>> blocking forever...");
|
||||
let do_log = true;
|
||||
make_event_loop(mqtt_client, rx, network, do_log)?;
|
||||
make_event_loop(mqtt_client, rx, network, do_log, led_tx)?;
|
||||
|
||||
let mut blue = Led::new(0x000001, 100);
|
||||
println!("{:?}", wifi.get_status());
|
||||
loop {
|
||||
log::info!("Listening...");
|
||||
blue.blink();
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
// drop(wifi);
|
||||
} else {
|
||||
led_tx.send(Status::WifiAccessPoint).unwrap();
|
||||
println!("=============> START SERVER NOW AND WAIT <==============");
|
||||
if let Ok((wifi, config)) = start_config_server_and_wait(default_nvs.clone()) {
|
||||
store.put("config", &config).expect("could not store config");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::core::events::Status;
|
||||
use core::time::Duration;
|
||||
use embedded_hal::delay::blocking::DelayUs;
|
||||
use esp_idf_hal::delay::Ets;
|
||||
@@ -5,6 +6,8 @@ use esp_idf_hal::gpio::Gpio8;
|
||||
use esp_idf_hal::peripherals::Peripherals;
|
||||
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::sync::{LazyLock, Mutex};
|
||||
|
||||
@@ -21,6 +24,14 @@ pub struct Led {
|
||||
blink_length: u32,
|
||||
}
|
||||
|
||||
pub fn led_control_loop(rx: mpsc::Receiver<Status>) {
|
||||
thread::spawn(move || {
|
||||
while let Ok(status) = rx.recv() {
|
||||
log::info!("LED STATUS: {:?}", status);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
impl Led {
|
||||
pub fn new(rgb: u32, blink_length: u32) -> Led {
|
||||
Led {
|
||||
|
||||
Reference in New Issue
Block a user