From 9b147e05b4f6900d9d31227d2851e03a0e60b82e Mon Sep 17 00:00:00 2001 From: Jared Burce Date: Thu, 29 Oct 2020 09:06:58 -0700 Subject: [PATCH] Locking for hue control --- src/main.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 482e486..8daff1f 100644 --- a/src/main.rs +++ b/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> = RefCell::new(None) + static AUDIO_CHILD: RefCell> = 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! {