try_recv for changing led status

This commit is contained in:
Evan Feenstra
2022-06-22 16:08:45 -07:00
parent 1d2d099a22
commit ae3d946912
3 changed files with 18 additions and 10 deletions

View File

@@ -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();

View File

@@ -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));

View File

@@ -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<Status>) {
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));
}
});
}