Locking for hue control
This commit is contained in:
17
src/main.rs
17
src/main.rs
@@ -1,6 +1,7 @@
|
||||
use std::cell::RefCell;
|
||||
use std::convert::Infallible;
|
||||
use std::process;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -20,8 +21,9 @@ const BUTTON_PIN: u8 = 26;
|
||||
const CHANNEL_VEC_SIZE: usize = 32;
|
||||
|
||||
thread_local! {
|
||||
static AUDIO_CHILD: RefCell<Option<process::Child>> = RefCell::new(None)
|
||||
static AUDIO_CHILD: RefCell<Option<process::Child>> = RefCell::new(None);
|
||||
}
|
||||
static HUE_LOCKED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
#[tokio::main(basic_scheduler)]
|
||||
async fn main() {
|
||||
@@ -39,11 +41,16 @@ async fn main() {
|
||||
let reqwest = reqwest::Client::new();
|
||||
let (tx, rx) = mpsc::channel(1);
|
||||
events_clients.lock().unwrap().push(tx);
|
||||
let pushes = rx.for_each(|()| async {
|
||||
if !audio_busy() { play_chime() }
|
||||
else { debug!("doorbell still ringing, not playing new chime"); }
|
||||
let pushes = rx.for_each_concurrent(2, |()| async {
|
||||
if !audio_busy() {
|
||||
play_chime()
|
||||
} else { debug!("doorbell still ringing, not playing new chime"); }
|
||||
|
||||
flash_patio(&reqwest).await;
|
||||
let locked = HUE_LOCKED.compare_and_swap(false, true, Ordering::Acquire);
|
||||
if !locked {
|
||||
flash_patio(&reqwest).await;
|
||||
HUE_LOCKED.store(false, Ordering::Release);
|
||||
} else { debug!("hue already in use, not scheduling new flashing"); }
|
||||
});
|
||||
|
||||
select! {
|
||||
|
||||
Reference in New Issue
Block a user