diff --git a/src/main.rs b/src/main.rs index f041b4a..47e293c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,10 +19,6 @@ const HUE_KEY: &str = include_str!("../hue.key"); const BUTTON_PIN: u8 = 26; const CHANNEL_VEC_SIZE: usize = 32; -thread_local! { - static AUDIO_CHILD: RefCell> = RefCell::new(None); -} - #[tokio::main(basic_scheduler)] async fn main() { pretty_env_logger::init_timed(); @@ -36,13 +32,15 @@ async fn main() { pin.set_async_interrupt(Trigger::FallingEdge, move |_| button_pressed(&clients)) .expect("set interrupt"); - let reqwest = &reqwest::Client::new(); let (tx, rx) = mpsc::channel(1); events_clients.lock().unwrap().push(tx); + + let reqwest = &reqwest::Client::new(); + let audio_child = &RefCell::new(None); let hue_busy = &Cell::new(false); let pushes = rx.for_each_concurrent(2, |()| async move { - if !audio_busy() { - play_chime() + if !audio_busy(&audio_child) { + play_chime(&audio_child) } else { debug!("doorbell still ringing, not playing new chime"); } if !hue_busy.replace(true) { @@ -107,25 +105,23 @@ async fn warp(clients: Arc; CHANNEL_VEC_SIZE]>> warp::serve(routes).run(([0, 0, 0, 0], 8060)).await; } -fn audio_busy() -> bool { - AUDIO_CHILD.with(|cell| { - if let Some(ref mut audio_child) = *cell.borrow_mut() { - if let Ok(None) = audio_child.try_wait() { - return true; - } +fn audio_busy(audio_child: &RefCell>) -> bool { + if let Some(ref mut audio_child) = *audio_child.borrow_mut() { + if let Ok(None) = audio_child.try_wait() { + return true; } - false - }) + } + false } -fn play_chime() { +fn play_chime(audio_child: &RefCell>) { trace!("Playing doorbell chime"); match process::Command::new("mplayer") .arg(OUTDOOR_CHIME_FILE) .stdout(process::Stdio::null()) .stderr(process::Stdio::null()) .spawn() { - Ok(child) => { AUDIO_CHILD.with(|cell| cell.replace(Some(child))); }, + Ok(child) => { audio_child.replace(Some(child)); }, Err(err) => error!("Error playing outdoor chime: {}", err) }; }