From d711303bd180c7199f50704fa5b73ce4b6a1a72f Mon Sep 17 00:00:00 2001 From: Claas Date: Sat, 16 Nov 2024 18:57:42 +0100 Subject: [PATCH] Implement untested status LED display --- fan-controller/README.md | 2 +- fan-controller/src/fan.rs | 2 +- fan-controller/src/main.rs | 39 +++++++++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/fan-controller/README.md b/fan-controller/README.md index 04c3590..b6016f3 100644 --- a/fan-controller/README.md +++ b/fan-controller/README.md @@ -7,5 +7,5 @@ install [probe.rs](https://probe.rs) `cargo run` - [ ] Read fan speed on boot in case fans were already running - [x] Debounce button tap - [ ] Retry if there was an error writing to one or two fans -- [ ] Confirm fan speed is set on homeassistant and retry otherwise +- [ ] Confirm fan speed is set on homeassistant and retry otherwise (there might be network interference) - [ ] Flash LEDs for one second after boot to indicate if they work diff --git a/fan-controller/src/fan.rs b/fan-controller/src/fan.rs index 25f8437..1c58b23 100644 --- a/fan-controller/src/fan.rs +++ b/fan-controller/src/fan.rs @@ -32,7 +32,7 @@ const BLOCK_FOR: Duration = Duration::from_micros(5_000); pub(crate) const MAX_SET_POINT: u16 = 64_000; -#[derive(Debug, Format, Clone, Copy, PartialEq)] +#[derive(Debug, Format, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub(crate) struct Setting(pub(crate) u16); #[derive(Debug, Format)] diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index b95e8dd..f72ff50 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -1177,16 +1177,49 @@ static FAN_STATE: Watch = Watch::new(); /// On On -> Fan on high setting #[embassy_executor::task] async fn display_status(pin_21: PIN_21, pin_20: PIN_20) { + // Setup LEDs + let mut led_1 = Output::new(pin_21, Level::Low); + let mut led_2 = Output::new(pin_20, Level::Low); + let Some(mut receiver) = FAN_STATE.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."); return; }; - let mut previous = FAN_STATE.try_get().unwrap_or(FanState { + // Set initial state + let mut current_state = FAN_STATE.try_get().unwrap_or(FanState { is_on: false, setting: fan::Setting::ZERO, }); + + loop { + let led_state = match current_state { + FanState { + is_on: false, + setting: _, + } => (Level::Low, Level::Low), + FanState { + is_on: true, + setting, + } => { + if setting >= fan::user_setting::HIGH { + (Level::High, Level::High) + } else if setting >= fan::user_setting::MEDIUM { + (Level::Low, Level::High) + } else { + // (Low and lower than Low) + (Level::High, Level::Low) + } + } + }; + + led_1.set_level(led_state.0); + led_2.set_level(led_state.1); + + // Wait for state update + current_state = receiver.changed().await; + } } #[embassy_executor::main] @@ -1205,6 +1238,9 @@ async fn main(spawner: Spawner) { PIN_12: pin_12, PIN_13: pin_13, PIN_18: pin_18, + // Status LEDs + PIN_20: pin_20, + PIN_21: pin_21, .. } = embassy_rp::init(Default::default()); @@ -1234,6 +1270,7 @@ async fn main(spawner: Spawner) { unwrap!(spawner.spawn(mqtt_task( spawner, pin_23, pin_25, pio0, dma_ch0, pin_24, pin_29 ))); + unwrap!(spawner.spawn(display_status(pin_21, pin_20))); } #[cfg(test)] -- 2.51.2