From bbf7c8643fb207afd6e9a6e363b2cafef34519cc Mon Sep 17 00:00:00 2001 From: Claas Date: Wed, 19 Nov 2025 23:05:04 +0100 Subject: [PATCH] Simplify fan mutex by removing option workaround --- fan-controller/src/main.rs | 32 ++++++++++++++++---------------- fan-controller/src/task.rs | 9 +++------ 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index 197d558..400ddc9 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -199,7 +199,7 @@ async fn input(pin_18: PIN_18) { /// Update fans whenenver the fan setting or on state changes #[embassy_executor::task] -async fn update_fans() { +async fn update_fans(fans: FansMutex) { let Some(mut receiver) = FAN_CONTROLLER.fan_states.0.receiver() else { // Not using asserts because they are hard to debug on embedded where it crashed error!("No receiver for fan is on state. This should never happen."); @@ -221,11 +221,8 @@ async fn update_fans() { previous = state.clone(); info!("Updating fans"); - let mut fans = FANS.lock().await; - let Some(fans) = fans.deref_mut() else { - warn!("No fan client found"); - continue; - }; + let mut fans = fans.lock().await; + let fans = fans.deref_mut(); let setting = if state.is_on { state.setting @@ -251,9 +248,7 @@ async fn update_fans() { } } -type Fans = Mutex>>; -/// Use this to make calls to the fans through modbus -static FANS: Fans = Mutex::new(None); +type FansMutex = Mutex>; /// Fan state can have a setting while being off although and we emulate that behavior because /// fan devices actually don't have that behavior @@ -499,7 +494,8 @@ async fn fan_control_routine(fan_state: &'static Signal, 3> = diff --git a/fan-controller/src/task.rs b/fan-controller/src/task.rs index 6b25d71..80bf3ec 100644 --- a/fan-controller/src/task.rs +++ b/fan-controller/src/task.rs @@ -10,7 +10,7 @@ use crate::mqtt::{self}; use crate::mqtt::{non_zero_u16, TryDecode}; use crate::PingRequest; use crate::{configuration, fan, gain_control, FanState}; -use crate::{modbus, Fans}; +use crate::{modbus, FansMutex}; use ::mqtt::QualityOfService; use core::future::poll_fn; use core::num::NonZeroU16; @@ -561,13 +561,10 @@ async fn update_homeassistant( } } -async fn poll_sensors(fans: Fans) { +async fn poll_sensors(fans: FansMutex) { loop { let mut fans = fans.lock().await; - let Some(fans) = fans.deref_mut() else { - error!("Fans were not set up. Cannot poll sensors"); - return; - }; + let fans = fans.deref_mut(); let temperature = match fans.get_temperature(fan::Fan::One).await { Ok(temperature) => temperature, -- 2.51.2