From 8d68f0ef14b017bd3ce786c1da686426299aaf7f Mon Sep 17 00:00:00 2001 From: ju6ge Date: Fri, 20 Jun 2025 19:54:49 +0200 Subject: [PATCH 1/7] remove embassy references in Cargo.toml in newer esp-rust toolchains having these dependencies defiened leeds to compile errors. --- rust/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 0fc0d3d..845c6a2 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -38,13 +38,13 @@ command = [ partition_table = "partitions.csv" [features] -default = ["std", "embassy", "esp-idf-svc/native"] +default = ["std", "esp-idf-svc/native"] pio = ["esp-idf-svc/pio"] std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] alloc = ["esp-idf-svc/alloc"] nightly = ["esp-idf-svc/nightly"] experimental = ["esp-idf-svc/experimental"] -embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] +#embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] [dependencies] #ESP stuff -- 2.47.1 From b7b08d8747d5633c1e34115861b3382979b09033 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Fri, 20 Jun 2025 20:14:20 +0200 Subject: [PATCH 2/7] fix unused variable warnings --- rust/src/hal/initial_hal.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rust/src/hal/initial_hal.rs b/rust/src/hal/initial_hal.rs index f11e416..d79c81e 100644 --- a/rust/src/hal/initial_hal.rs +++ b/rust/src/hal/initial_hal.rs @@ -50,7 +50,7 @@ impl<'a> BoardInteraction<'a> for Initial<'a> { &mut self.battery } - fn set_charge_indicator(&mut self, charging: bool) -> Result<()> { + fn set_charge_indicator(&mut self, _charging: bool) -> Result<()> { bail!("Please configure board revision") } @@ -66,7 +66,7 @@ impl<'a> BoardInteraction<'a> for Initial<'a> { bail!("Please configure board revision") } - fn backup_config(&mut self, bytes: &[u8]) -> Result<()> { + fn backup_config(&mut self, _bytes: &[u8]) -> Result<()> { bail!("Please configure board revision") } @@ -81,17 +81,17 @@ impl<'a> BoardInteraction<'a> for Initial<'a> { fn tank_sensor_voltage(&mut self) -> Result { bail!("Please configure board revision") } - fn light(&mut self, enable: bool) -> Result<()> { + fn light(&mut self, _enable: bool) -> Result<()> { bail!("Please configure board revision") } - fn pump(&mut self, plant: usize, enable: bool) -> Result<()> { + fn pump(&mut self, _plant: usize, _enable: bool) -> Result<()> { bail!("Please configure board revision") } - fn fault(&mut self, plant: usize, _enable: bool) -> Result<()> { + fn fault(&mut self, _plant: usize, _enable: bool) -> Result<()> { bail!("Please configure board revision") } - fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> Result { + fn measure_moisture_hz(&mut self, _plant: usize, _sensor: Sensor) -> Result { bail!("Please configure board revision") } @@ -107,10 +107,10 @@ impl<'a> BoardInteraction<'a> for Initial<'a> { bail!("Please configure board revision") } - fn set_rtc_time(&mut self, time: &DateTime) -> Result<()> { + fn set_rtc_time(&mut self, _time: &DateTime) -> Result<()> { bail!("Please configure board revision") } - fn test_pump(&mut self, plant: usize) -> Result<()> { + fn test_pump(&mut self, _plant: usize) -> Result<()> { bail!("Please configure board revision") } -- 2.47.1 From c1d0ce542c2674a80c1b51af9fff3aaef3f927fb Mon Sep 17 00:00:00 2001 From: ju6ge Date: Fri, 20 Jun 2025 20:18:53 +0200 Subject: [PATCH 3/7] fix confusing lifetime warning --- rust/src/sipo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/src/sipo.rs b/rust/src/sipo.rs index b4610b3..fddfacc 100644 --- a/rust/src/sipo.rs +++ b/rust/src/sipo.rs @@ -100,7 +100,7 @@ macro_rules! ShiftRegisterBuilder { } /// Get embedded-hal output pins to control the shift register outputs - pub fn decompose(&self) -> [ShiftRegisterPin; $size] { + pub fn decompose(&self) -> [ShiftRegisterPin<'_>; $size] { // Create an uninitialized array of `MaybeUninit`. The `assume_init` is // safe because the type we are claiming to have initialized here is a // bunch of `MaybeUninit`s, which do not require initialization. -- 2.47.1 From 7115809f2bd033fe68af1d08602fb14c3ee0036b Mon Sep 17 00:00:00 2001 From: ju6ge Date: Fri, 20 Jun 2025 20:36:29 +0200 Subject: [PATCH 4/7] fix all compiler warnings --- rust/src/hal/battery.rs | 1 - rust/src/hal/initial_hal.rs | 6 ++-- rust/src/hal/mod.rs | 63 ++++++++++++++------------------- rust/src/hal/v3_hal.rs | 30 ++++++++-------- rust/src/hal/v4_hal.rs | 26 +++++++------- rust/src/main.rs | 22 ++++++------ rust/src/plant_state.rs | 8 +++-- rust/src/tank.rs | 4 +-- rust/src/webserver/webserver.rs | 12 +++---- 9 files changed, 84 insertions(+), 88 deletions(-) diff --git a/rust/src/hal/battery.rs b/rust/src/hal/battery.rs index 50e76b2..7e844ca 100644 --- a/rust/src/hal/battery.rs +++ b/rust/src/hal/battery.rs @@ -1,4 +1,3 @@ -use crate::to_string; use anyhow::anyhow; use bq34z100::{Bq34Z100Error, Bq34z100g1, Bq34z100g1Driver}; use embedded_hal_bus::i2c::MutexDevice; diff --git a/rust/src/hal/initial_hal.rs b/rust/src/hal/initial_hal.rs index d79c81e..cc15945 100644 --- a/rust/src/hal/initial_hal.rs +++ b/rust/src/hal/initial_hal.rs @@ -1,7 +1,9 @@ -use crate::config::{BoardHardware, PlantControllerConfig}; -use crate::hal::battery::{BatteryInteraction, NoBatteryMonitor}; use crate::hal::esp::ESP; use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor}; +use crate::{ + config::PlantControllerConfig, + hal::battery::{BatteryInteraction, NoBatteryMonitor}, +}; use anyhow::{bail, Result}; use chrono::{DateTime, Utc}; use embedded_hal::digital::OutputPin; diff --git a/rust/src/hal/mod.rs b/rust/src/hal/mod.rs index 232664c..c000595 100644 --- a/rust/src/hal/mod.rs +++ b/rust/src/hal/mod.rs @@ -4,54 +4,45 @@ mod initial_hal; mod v3_hal; mod v4_hal; +use crate::{ + config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig}, + hal::{ + battery::{print_battery_bq34z100, BatteryInteraction, NoBatteryMonitor}, + esp::ESP, + }, + log::{log, LogMessage}, +}; +use anyhow::{Ok, Result}; use battery::BQ34Z100G1; use bq34z100::Bq34z100g1Driver; - -use crate::log::LogMessage; -use ds323x::DateTimeAccess; -use esp_ota::mark_app_valid; - -use eeprom24x::Eeprom24xTrait; +use chrono::{DateTime, Utc}; use embedded_hal_bus::i2c::MutexDevice; - -use esp_idf_hal::adc::ADC1; -use esp_idf_hal::i2c::{APBTickType, I2cConfig, I2cDriver}; -use esp_idf_hal::units::FromValueType; -use esp_idf_svc::eventloop::EspSystemEventLoop; -use esp_idf_svc::nvs::EspDefaultNvsPartition; -use esp_idf_svc::wifi::EspWifi; -use esp_idf_sys::esp_restart; +use esp_idf_hal::{ + adc::ADC1, + delay::Delay, + gpio::{ + Gpio0, Gpio1, Gpio10, Gpio11, Gpio12, Gpio13, Gpio14, Gpio15, Gpio16, Gpio17, Gpio18, + Gpio2, Gpio21, Gpio22, Gpio23, Gpio24, Gpio25, Gpio26, Gpio27, Gpio28, Gpio29, Gpio3, + Gpio30, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, IOPin, PinDriver, Pull, + }, + i2c::{APBTickType, I2cConfig, I2cDriver}, + pcnt::PCNT0, + prelude::Peripherals, + reset::ResetReason, + units::FromValueType, +}; +use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition, wifi::EspWifi}; use esp_idf_sys::{ - esp_deep_sleep, esp_sleep_enable_ext1_wakeup, + esp_deep_sleep, esp_restart, esp_sleep_enable_ext1_wakeup, esp_sleep_ext1_wakeup_mode_t_ESP_EXT1_WAKEUP_ANY_LOW, }; +use esp_ota::mark_app_valid; use once_cell::sync::Lazy; - -use anyhow::{Ok, Result}; use serde::{Deserialize, Serialize}; - -use chrono::{DateTime, Utc}; use std::result::Result::Ok as OkStd; use std::sync::Mutex; use std::time::Duration; -use crate::config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig}; -use crate::hal::battery::{print_battery_bq34z100, BatteryInteraction, NoBatteryMonitor}; -use crate::hal::esp::ESP; -use crate::hal::initial_hal::Initial; -use crate::log::log; -use embedded_hal::digital::OutputPin; -use esp_idf_hal::delay::Delay; -use esp_idf_hal::gpio::{ - Gpio0, Gpio1, Gpio10, Gpio11, Gpio12, Gpio13, Gpio14, Gpio15, Gpio16, Gpio17, Gpio18, Gpio2, - Gpio21, Gpio22, Gpio23, Gpio24, Gpio25, Gpio26, Gpio27, Gpio28, Gpio29, Gpio3, Gpio30, Gpio4, - Gpio5, Gpio6, Gpio7, Gpio8, IOPin, PinDriver, Pull, -}; -use esp_idf_hal::pcnt::PCNT0; -use esp_idf_hal::prelude::Peripherals; -use esp_idf_hal::reset::ResetReason; -use pca9535::StandardExpanderInterface; - //Only support for 8 right now! pub const PLANT_COUNT: usize = 8; const REPEAT_MOIST_MEASURE: usize = 1; diff --git a/rust/src/hal/v3_hal.rs b/rust/src/hal/v3_hal.rs index ee2fbfe..cd07b2d 100644 --- a/rust/src/hal/v3_hal.rs +++ b/rust/src/hal/v3_hal.rs @@ -1,11 +1,12 @@ -use crate::config::PlantControllerConfig; -use crate::hal::battery::BatteryInteraction; -use crate::hal::esp::ESP; use crate::hal::{ - deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor, V3Constants, I2C_DRIVER, - PLANT_COUNT, REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE, X25, + deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT, + REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE, X25, }; use crate::log::{log, LogMessage}; +use crate::{ + config::PlantControllerConfig, + hal::{battery::BatteryInteraction, esp::ESP}, +}; use anyhow::{anyhow, bail, Ok, Result}; use chrono::{DateTime, Utc}; use ds18b20::Ds18b20; @@ -13,20 +14,21 @@ use ds323x::{DateTimeAccess, Ds323x}; use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr}; use embedded_hal::digital::OutputPin; use embedded_hal_bus::i2c::MutexDevice; -use esp_idf_hal::adc::oneshot::config::AdcChannelConfig; -use esp_idf_hal::adc::oneshot::{AdcChannelDriver, AdcDriver}; -use esp_idf_hal::adc::{attenuation, Resolution}; -use esp_idf_hal::delay::Delay; -use esp_idf_hal::gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, PinDriver, Pull}; -use esp_idf_hal::i2c::I2cDriver; -use esp_idf_hal::pcnt::{ - PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex, +use esp_idf_hal::{ + adc::{ + attenuation, + oneshot::{config::AdcChannelConfig, AdcChannelDriver, AdcDriver}, + Resolution, + }, + delay::Delay, + gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, PinDriver, Pull}, + i2c::I2cDriver, + pcnt::{PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex}, }; use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError}; use one_wire_bus::OneWire; use plant_ctrl2::sipo::ShiftRegister40; use std::result::Result::Ok as OkStd; -use std::sync::Arc; const PUMP8_BIT: usize = 0; const PUMP1_BIT: usize = 1; diff --git a/rust/src/hal/v4_hal.rs b/rust/src/hal/v4_hal.rs index b63141a..fd1e897 100644 --- a/rust/src/hal/v4_hal.rs +++ b/rust/src/hal/v4_hal.rs @@ -1,4 +1,4 @@ -use crate::config::PlantControllerConfig; +pub use crate::config::PlantControllerConfig; use crate::hal::battery::BatteryInteraction; use crate::hal::esp::ESP; use crate::hal::{ @@ -13,19 +13,19 @@ use ds323x::{DateTimeAccess, Ds323x}; use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr}; use embedded_hal::digital::OutputPin; use embedded_hal_bus::i2c::MutexDevice; -use esp_idf_hal::adc::oneshot::config::AdcChannelConfig; -use esp_idf_hal::adc::oneshot::{AdcChannelDriver, AdcDriver}; -use esp_idf_hal::adc::{attenuation, Resolution}; -use esp_idf_hal::delay::Delay; -use esp_idf_hal::gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, Output, PinDriver, Pull}; -use esp_idf_hal::i2c::I2cDriver; -use esp_idf_hal::pcnt::{ - PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex, +use esp_idf_hal::{ + adc::{ + attenuation, + oneshot::{config::AdcChannelConfig, AdcChannelDriver, AdcDriver}, + Resolution, + }, + delay::Delay, + gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, Output, PinDriver, Pull}, + i2c::I2cDriver, + pcnt::{PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex}, }; -use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError}; -use ina219::address::Address; -use ina219::calibration::{Calibration, UnCalibrated}; -use ina219::SyncIna219; +use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, EspError}; +use ina219::{address::Address, calibration::UnCalibrated, SyncIna219}; use one_wire_bus::OneWire; use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface}; use std::result::Result::Ok as OkStd; diff --git a/rust/src/main.rs b/rust/src/main.rs index d2a63d1..06c8791 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,8 +1,11 @@ -use crate::webserver::webserver::httpd; +use crate::{ + config::BoardVersion::INITIAL, + hal::{PlantHal, HAL, PLANT_COUNT}, + webserver::webserver::httpd, +}; use anyhow::bail; use chrono::{DateTime, Datelike, Timelike, Utc}; -use chrono_tz::Tz; -use chrono_tz::Tz::UTC; +use chrono_tz::Tz::{self, UTC}; use esp_idf_hal::delay::Delay; use esp_idf_sys::{ esp_ota_get_app_partition_count, esp_ota_get_running_partition, esp_ota_get_state_partition, @@ -15,23 +18,21 @@ use esp_ota::{mark_app_valid, rollback_and_reboot}; use hal::battery::BatteryState; use log::{log, LogMessage}; use once_cell::sync::Lazy; +use plant_state::PlantState; use serde::{Deserialize, Serialize}; use std::sync::MutexGuard; use std::{ fmt::Display, sync::{atomic::AtomicBool, Arc, Mutex}, }; +use tank::*; + mod config; mod hal; mod log; mod plant_state; mod tank; -use crate::config::BoardVersion::INITIAL; -use crate::hal::battery::BatteryInteraction; -use crate::hal::{BoardInteraction, PlantHal, HAL, PLANT_COUNT}; -use plant_state::PlantState; -use tank::*; pub static BOARD_ACCESS: Lazy> = Lazy::new(|| PlantHal::create().unwrap()); pub static STAY_ALIVE: Lazy = Lazy::new(|| AtomicBool::new(false)); @@ -424,7 +425,7 @@ fn safe_main() -> anyhow::Result<()> { .state_charge_percent() .unwrap_or(0.); - /// try to load full battery state if failed the battery state is unknown + // try to load full battery state if failed the battery state is unknown let battery_state = board .board_hal .get_battery_monitor() @@ -739,7 +740,8 @@ fn pump_info( fn publish_battery_state(board: &mut MutexGuard<'_, HAL<'_>>) { let state = board.board_hal.get_battery_monitor().get_battery_state(); - if let Ok(serialized_battery_state_bytes) = serde_json::to_string(&state).map(|s| s.into_bytes()) + if let Ok(serialized_battery_state_bytes) = + serde_json::to_string(&state).map(|s| s.into_bytes()) { let _ = board .board_hal diff --git a/rust/src/plant_state.rs b/rust/src/plant_state.rs index e3272ee..fd331e2 100644 --- a/rust/src/plant_state.rs +++ b/rust/src/plant_state.rs @@ -1,10 +1,12 @@ +use crate::{ + config::PlantConfig, + hal::{Sensor, HAL}, + in_time_range, +}; use chrono::{DateTime, TimeDelta, Utc}; use chrono_tz::Tz; use serde::{Deserialize, Serialize}; -use crate::hal::{BoardInteraction, Sensor, HAL}; -use crate::{config::PlantConfig, in_time_range}; - const MOIST_SENSOR_MAX_FREQUENCY: f32 = 7500.; // 60kHz (500Hz margin) const MOIST_SENSOR_MIN_FREQUENCY: f32 = 150.; // this is really, really dry, think like cactus levels diff --git a/rust/src/tank.rs b/rust/src/tank.rs index fde2936..38e9da0 100644 --- a/rust/src/tank.rs +++ b/rust/src/tank.rs @@ -1,8 +1,6 @@ +use crate::{config::TankConfig, hal::HAL}; use serde::Serialize; -use crate::config::TankConfig; -use crate::hal::{BoardInteraction, HAL}; - const OPEN_TANK_VOLTAGE: f32 = 3.0; pub const WATER_FROZEN_THRESH: f32 = 4.0; diff --git a/rust/src/webserver/webserver.rs b/rust/src/webserver/webserver.rs index 53ca85d..d6f168d 100644 --- a/rust/src/webserver/webserver.rs +++ b/rust/src/webserver/webserver.rs @@ -1,8 +1,12 @@ //offer ota and config mode -use crate::hal::battery::BatteryInteraction; use crate::{ - determine_tank_state, get_version, log::LogMessage, plant_state::PlantState, BOARD_ACCESS, + config::PlantControllerConfig, + determine_tank_state, get_version, + hal::PLANT_COUNT, + log::LogMessage, + plant_state::{MoistureSensorState, PlantState}, + BOARD_ACCESS, }; use anyhow::bail; use chrono::DateTime; @@ -19,10 +23,6 @@ use std::{ }; use url::Url; -use crate::config::PlantControllerConfig; -use crate::hal::{BoardInteraction, PLANT_COUNT}; -use crate::plant_state::MoistureSensorState; - #[derive(Serialize, Debug)] struct SSIDList<'a> { ssids: Vec<&'a String<32>>, -- 2.47.1 From d0e5627815cd6392d1ab08bdb5735ab2aa3e6820 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Fri, 20 Jun 2025 20:49:27 +0200 Subject: [PATCH 5/7] keep node artifacts in webpack directory add commands to build.rs to move assets to required location within src --- rust/build.rs | 28 ++++++++++++++++++++++++++++ rust/src_webpack/webpack.config.js | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/rust/build.rs b/rust/build.rs index b0169e5..43feaea 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -22,6 +22,22 @@ fn main() { println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); assert!(output.status.success()); + + // move webpack results to rust webserver src + let _ = Command::new("cmd") + .arg("/K") + .arg("move") + .arg("./src_webpack/bundle.js") + .arg("./src/webserver") + .output() + .unwrap(); + let _ = Command::new("cmd") + .arg("/K") + .arg("move") + .arg("./src_webpack/index.html") + .arg("./src/webserver") + .output() + .unwrap(); } Err(_) => { println!("Assuming build on linux"); @@ -34,6 +50,18 @@ fn main() { println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); assert!(output.status.success()); + + // move webpack results to rust webserver src + let _ = Command::new("mv") + .arg("./src_webpack/bundle.js") + .arg("./src/webserver") + .output() + .unwrap(); + let _ = Command::new("mv") + .arg("./src_webpack/index.html") + .arg("./src/webserver") + .output() + .unwrap(); } } diff --git a/rust/src_webpack/webpack.config.js b/rust/src_webpack/webpack.config.js index 00e4ebc..fa36e6f 100644 --- a/rust/src_webpack/webpack.config.js +++ b/rust/src_webpack/webpack.config.js @@ -49,7 +49,7 @@ module.exports = { }, output: { filename: 'bundle.js', - path: path.resolve(__dirname, '../src/webserver'), + path: path.resolve(__dirname, '.'), }, devServer: { } -- 2.47.1 From 5621f17e61c0ac18a7e2303a772410b5928b7b73 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Fri, 20 Jun 2025 21:31:12 +0200 Subject: [PATCH 6/7] fix all warnings --- rust/src/hal/battery.rs | 1 + rust/src/hal/mod.rs | 6 +----- rust/src/hal/v3_hal.rs | 6 +++--- rust/src/hal/v4_hal.rs | 8 ++++---- rust/src/main.rs | 15 +-------------- rust/src/plant_state.rs | 6 +++++- rust/src/webserver/webserver.rs | 2 +- 7 files changed, 16 insertions(+), 28 deletions(-) diff --git a/rust/src/hal/battery.rs b/rust/src/hal/battery.rs index 7e844ca..4ec9f4e 100644 --- a/rust/src/hal/battery.rs +++ b/rust/src/hal/battery.rs @@ -97,6 +97,7 @@ impl BatteryInteraction for NoBatteryMonitor { } //TODO implement this battery monitor kind once controller is complete +#[allow(dead_code)] pub struct WchI2cSlave {} pub struct BQ34Z100G1<'a> { diff --git a/rust/src/hal/mod.rs b/rust/src/hal/mod.rs index c000595..92d1a8b 100644 --- a/rust/src/hal/mod.rs +++ b/rust/src/hal/mod.rs @@ -51,11 +51,6 @@ const TANK_MULTI_SAMPLE: usize = 11; pub static I2C_DRIVER: Lazy>> = Lazy::new(PlantHal::create_i2c); -#[non_exhaustive] -struct V3Constants; - -impl V3Constants {} - const X25: crc::Crc = crc::Crc::::new(&crc::CRC_16_IBM_SDLC); fn deep_sleep(duration_in_ms: u64) -> ! { @@ -132,6 +127,7 @@ pub trait BoardInteraction<'a> { fn set_config(&mut self, config: PlantControllerConfig) -> Result<()>; } +#[allow(dead_code)] pub struct FreePeripherals { pub gpio0: Gpio0, pub gpio1: Gpio1, diff --git a/rust/src/hal/v3_hal.rs b/rust/src/hal/v3_hal.rs index cd07b2d..8961c60 100644 --- a/rust/src/hal/v3_hal.rs +++ b/rust/src/hal/v3_hal.rs @@ -84,7 +84,7 @@ pub struct V3<'a> { PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, >, - shift_register_enable_invert: + _shift_register_enable_invert: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Output>, tank_channel: AdcChannelDriver<'a, Gpio5, AdcDriver<'a, esp_idf_hal::adc::ADC1>>, solar_is_day: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>, @@ -109,7 +109,7 @@ pub(crate) fn create_v3( esp: ESP<'static>, config: PlantControllerConfig, battery_monitor: Box, -) -> Result> { +) -> Result + Send>> { let mut clock = PinDriver::input_output(peripherals.gpio15.downgrade())?; clock.set_pull(Pull::Floating)?; let mut latch = PinDriver::input_output(peripherals.gpio3.downgrade())?; @@ -234,7 +234,7 @@ pub(crate) fn create_v3( battery_monitor, esp, shift_register, - shift_register_enable_invert, + _shift_register_enable_invert: shift_register_enable_invert, tank_channel, solar_is_day, light, diff --git a/rust/src/hal/v4_hal.rs b/rust/src/hal/v4_hal.rs index fd1e897..ab60976 100644 --- a/rust/src/hal/v4_hal.rs +++ b/rust/src/hal/v4_hal.rs @@ -38,7 +38,7 @@ const MS4: u8 = 2_u8; const SENSOR_ON: u8 = 5_u8; pub struct V4<'a> { - mppt_ina: SyncIna219>, UnCalibrated>, + _mppt_ina: SyncIna219>, UnCalibrated>, esp: ESP<'a>, battery_monitor: Box, config: PlantControllerConfig, @@ -63,12 +63,12 @@ pub struct V4<'a> { sensor_expander: Pca9535Immediate>>, } -pub(crate) fn create_v4( +pub(crate) fn create_v4<'a>( peripherals: FreePeripherals, esp: ESP<'static>, config: PlantControllerConfig, battery_monitor: Box, -) -> anyhow::Result> { +) -> anyhow::Result + Send>> { let mut awake = PinDriver::output(peripherals.gpio15.downgrade())?; awake.set_high()?; @@ -188,7 +188,7 @@ pub(crate) fn create_v4( println!("Shunt Current: {}", current); let v = V4 { - mppt_ina, + _mppt_ina: mppt_ina, esp, awake, tank_channel, diff --git a/rust/src/main.rs b/rust/src/main.rs index 06c8791..dfad2a7 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -20,11 +20,7 @@ use log::{log, LogMessage}; use once_cell::sync::Lazy; use plant_state::PlantState; use serde::{Deserialize, Serialize}; -use std::sync::MutexGuard; -use std::{ - fmt::Display, - sync::{atomic::AtomicBool, Arc, Mutex}, -}; +use std::sync::{atomic::AtomicBool, Arc, Mutex, MutexGuard}; use tank::*; mod config; @@ -841,15 +837,6 @@ fn main() { } } -fn to_string(value: anyhow::Result) -> String { - match value { - Ok(v) => v.to_string(), - Err(err) => { - format!("{:?}", err) - } - } -} - pub fn in_time_range(cur: &DateTime, start: u8, end: u8) -> bool { let curhour = cur.hour() as u8; //eg 10-14 diff --git a/rust/src/plant_state.rs b/rust/src/plant_state.rs index fd331e2..38204cf 100644 --- a/rust/src/plant_state.rs +++ b/rust/src/plant_state.rs @@ -239,7 +239,11 @@ impl PlantState { } } - pub fn to_mqtt_info(&self, plant_conf: &PlantConfig, current_time: &DateTime) -> PlantInfo { + pub fn to_mqtt_info( + &self, + plant_conf: &PlantConfig, + current_time: &DateTime, + ) -> PlantInfo<'_> { PlantInfo { sensor_a: &self.sensor_a, sensor_b: &self.sensor_b, diff --git a/rust/src/webserver/webserver.rs b/rust/src/webserver/webserver.rs index d6f168d..f52c713 100644 --- a/rust/src/webserver/webserver.rs +++ b/rust/src/webserver/webserver.rs @@ -214,7 +214,7 @@ fn set_config( let config: PlantControllerConfig = serde_json::from_slice(&all)?; let mut board = BOARD_ACCESS.lock().expect("board access"); - board.board_hal.set_config(config); + let _ = board.board_hal.set_config(config); anyhow::Ok(Some("saved".to_owned())) } -- 2.47.1 From d059b7b1db4c8ff15303449bf22c84ae9836dcf6 Mon Sep 17 00:00:00 2001 From: Empire Date: Fri, 20 Jun 2025 23:59:14 +0200 Subject: [PATCH 7/7] Merge branch 'develop' into housekeeping and clippy and spellcheck --- rust/.idea/dictionaries/project.xml | 7 + rust/src/hal/esp.rs | 6 +- rust/src/hal/initial_hal.rs | 8 +- rust/src/hal/mod.rs | 20 +-- rust/src/hal/v3_hal.rs | 8 +- rust/src/hal/v4_hal.rs | 56 ++++--- rust/src/log/mod.rs | 2 +- rust/src/main.rs | 169 ++++++++++---------- rust/src/webserver/{webserver.rs => mod.rs} | 0 9 files changed, 135 insertions(+), 141 deletions(-) rename rust/src/webserver/{webserver.rs => mod.rs} (100%) diff --git a/rust/.idea/dictionaries/project.xml b/rust/.idea/dictionaries/project.xml index e169be7..929ed9a 100644 --- a/rust/.idea/dictionaries/project.xml +++ b/rust/.idea/dictionaries/project.xml @@ -1,7 +1,14 @@ + buildtime + deepsleep + githash + lightstate + mppt + plantstate sntp + vergen \ No newline at end of file diff --git a/rust/src/hal/esp.rs b/rust/src/hal/esp.rs index 70ff6b8..d1fc437 100644 --- a/rust/src/hal/esp.rs +++ b/rust/src/hal/esp.rs @@ -63,14 +63,14 @@ pub struct MqttClient<'a> { mqtt_client: EspMqttClient<'a>, base_topic: heapless::String<64>, } -pub struct ESP<'a> { +pub struct Esp<'a> { pub(crate) mqtt_client: Option>, pub(crate) wifi_driver: EspWifi<'a>, pub(crate) boot_button: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>, pub(crate) delay: Delay, } -impl ESP<'_> { +impl Esp<'_> { const SPIFFS_PARTITION_NAME: &'static str = "storage"; const CONFIG_FILE: &'static str = "/spiffs/config.cfg"; const BASE_PATH: &'static str = "/spiffs"; @@ -310,7 +310,7 @@ impl ESP<'_> { filename: file.file_name().into_string().unwrap(), size: file .metadata() - .and_then(|it| Ok(it.len())) + .map(|it| it.len()) .unwrap_or_default() as usize, }; diff --git a/rust/src/hal/initial_hal.rs b/rust/src/hal/initial_hal.rs index cda3d10..14238d2 100644 --- a/rust/src/hal/initial_hal.rs +++ b/rust/src/hal/initial_hal.rs @@ -1,4 +1,4 @@ -use crate::hal::esp::ESP; +use crate::hal::esp::Esp; use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor}; use crate::{ config::PlantControllerConfig, @@ -13,7 +13,7 @@ use measurements::{Current, Voltage}; pub struct Initial<'a> { pub(crate) general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, - pub(crate) esp: ESP<'a>, + pub(crate) esp: Esp<'a>, pub(crate) config: PlantControllerConfig, pub(crate) battery: Box, } @@ -22,7 +22,7 @@ pub(crate) fn create_initial_board( free_pins: FreePeripherals, fs_mount_error: bool, config: PlantControllerConfig, - esp: ESP<'static>, + esp: Esp<'static>, ) -> Result + Send>> { let mut general_fault = PinDriver::input_output(free_pins.gpio6.downgrade())?; general_fault.set_pull(Pull::Floating)?; @@ -41,7 +41,7 @@ pub(crate) fn create_initial_board( } impl<'a> BoardInteraction<'a> for Initial<'a> { - fn get_esp(&mut self) -> &mut ESP<'a> { + fn get_esp(&mut self) -> &mut Esp<'a> { &mut self.esp } diff --git a/rust/src/hal/mod.rs b/rust/src/hal/mod.rs index 6d2ddbb..1155ebc 100644 --- a/rust/src/hal/mod.rs +++ b/rust/src/hal/mod.rs @@ -8,7 +8,7 @@ use crate::{ config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig}, hal::{ battery::{print_battery_bq34z100, BatteryInteraction, NoBatteryMonitor}, - esp::ESP, + esp::Esp, }, log::{log, LogMessage}, }; @@ -37,12 +37,12 @@ use esp_idf_sys::{ esp_sleep_ext1_wakeup_mode_t_ESP_EXT1_WAKEUP_ANY_LOW, }; use esp_ota::mark_app_valid; +use measurements::{Current, Voltage}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; use std::result::Result::Ok as OkStd; use std::sync::Mutex; use std::time::Duration; -use measurements::{Current, Voltage}; //Only support for 8 right now! pub const PLANT_COUNT: usize = 8; @@ -84,25 +84,15 @@ pub struct HAL<'a> { pub board_hal: Box + Send>, } -#[derive(Serialize, Deserialize, PartialEq, Debug)] +#[derive(Serialize, Deserialize, PartialEq, Debug, Default)] pub struct BackupHeader { pub timestamp: i64, crc16: u16, pub size: usize, } -impl Default for BackupHeader { - fn default() -> Self { - Self { - timestamp: Default::default(), - crc16: Default::default(), - size: Default::default(), - } - } -} - pub trait BoardInteraction<'a> { - fn get_esp(&mut self) -> &mut ESP<'a>; + fn get_esp(&mut self) -> &mut Esp<'a>; fn get_config(&mut self) -> &PlantControllerConfig; fn get_battery_monitor(&mut self) -> &mut Box; fn set_charge_indicator(&mut self, charging: bool) -> Result<()>; @@ -225,7 +215,7 @@ impl PlantHal { gpio30: peripherals.pins.gpio30, }; - let mut esp = ESP { + let mut esp = Esp { mqtt_client: None, wifi_driver, boot_button, diff --git a/rust/src/hal/v3_hal.rs b/rust/src/hal/v3_hal.rs index 3645905..cbca6f1 100644 --- a/rust/src/hal/v3_hal.rs +++ b/rust/src/hal/v3_hal.rs @@ -5,7 +5,7 @@ use crate::hal::{ use crate::log::{log, LogMessage}; use crate::{ config::PlantControllerConfig, - hal::{battery::BatteryInteraction, esp::ESP}, + hal::{battery::BatteryInteraction, esp::Esp}, }; use anyhow::{anyhow, bail, Ok, Result}; use chrono::{DateTime, Utc}; @@ -79,7 +79,7 @@ const FAULT_2: usize = 23; pub struct V3<'a> { config: PlantControllerConfig, battery_monitor: Box, - esp: ESP<'a>, + esp: Esp<'a>, shift_register: ShiftRegister40< PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, @@ -107,7 +107,7 @@ pub struct V3<'a> { pub(crate) fn create_v3( peripherals: FreePeripherals, - esp: ESP<'static>, + esp: Esp<'static>, config: PlantControllerConfig, battery_monitor: Box, ) -> Result + Send>> { @@ -270,7 +270,7 @@ impl<'a> BoardInteraction<'a> for V3<'a> { fn get_mptt_current(&mut self) -> Result { bail!("Board does not have current sensor") } - fn get_esp(&mut self) -> &mut ESP<'a> { + fn get_esp(&mut self) -> &mut Esp<'a> { &mut self.esp } diff --git a/rust/src/hal/v4_hal.rs b/rust/src/hal/v4_hal.rs index e3b121b..ddaae4a 100644 --- a/rust/src/hal/v4_hal.rs +++ b/rust/src/hal/v4_hal.rs @@ -1,6 +1,6 @@ use crate::config::PlantControllerConfig; use crate::hal::battery::BatteryInteraction; -use crate::hal::esp::ESP; +use crate::hal::esp::Esp; use crate::hal::{ deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT, REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE, X25, @@ -22,15 +22,15 @@ use esp_idf_hal::i2c::I2cDriver; use esp_idf_hal::pcnt::{ PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex, }; -use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError}; +use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, EspError}; use ina219::address::{Address, Pin}; -use ina219::calibration::{Calibration, UnCalibrated}; +use ina219::calibration::UnCalibrated; +use ina219::configuration::{Configuration, OperatingMode}; use ina219::SyncIna219; use measurements::{Current, Resistance, Voltage}; use one_wire_bus::OneWire; use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface}; use std::result::Result::Ok as OkStd; -use ina219::configuration::{Configuration, OperatingMode}; const MS0: u8 = 1_u8; const MS1: u8 = 0_u8; @@ -47,23 +47,27 @@ pub enum Charger<'a> { }, } -impl<'a> Charger<'a> { - pub(crate) fn powersave(&mut self) { - match self { Charger::SolarMpptV1 { mppt_ina, .. } => { - let _ = mppt_ina.set_configuration(Configuration { - reset: Default::default(), - bus_voltage_range: Default::default(), - shunt_voltage_range: Default::default(), - bus_resolution: Default::default(), - shunt_resolution: Default::default(), - operating_mode: OperatingMode::PowerDown, - }).map_err(|e| { - println!( - "Error setting ina mppt configuration during deepsleep preparation{:?}", +impl Charger<'_> { + pub(crate) fn power_save(&mut self) { + match self { + Charger::SolarMpptV1 { mppt_ina, .. } => { + let _ = mppt_ina + .set_configuration(Configuration { + reset: Default::default(), + bus_voltage_range: Default::default(), + shunt_voltage_range: Default::default(), + bus_resolution: Default::default(), + shunt_resolution: Default::default(), + operating_mode: OperatingMode::PowerDown, + }) + .map_err(|e| { + println!( + "Error setting ina mppt configuration during deep sleep preparation{:?}", e ); - }); - } } + }); + } + } } fn set_charge_indicator(&mut self, charging: bool) -> anyhow::Result<()> { match self { @@ -105,7 +109,7 @@ impl<'a> Charger<'a> { } pub struct V4<'a> { - esp: ESP<'a>, + esp: Esp<'a>, charger: Charger<'a>, battery_monitor: Box, config: PlantControllerConfig, @@ -130,10 +134,10 @@ pub struct V4<'a> { pub(crate) fn create_v4( peripherals: FreePeripherals, - esp: ESP<'static>, + esp: Esp<'static>, config: PlantControllerConfig, battery_monitor: Box, -) -> anyhow::Result> { +) -> anyhow::Result + Send + 'static>> { let mut awake = PinDriver::output(peripherals.gpio15.downgrade())?; awake.set_high()?; @@ -244,7 +248,7 @@ pub(crate) fn create_v4( Address::from_pins(Pin::Vcc, Pin::Gnd), )?; - mppt_ina.set_configuration(Configuration{ + mppt_ina.set_configuration(Configuration { reset: Default::default(), bus_voltage_range: Default::default(), shunt_voltage_range: Default::default(), @@ -252,7 +256,7 @@ pub(crate) fn create_v4( shunt_resolution: ina219::configuration::Resolution::Avg128, operating_mode: Default::default(), })?; - //TODO this is probably laready done untill we are ready first time?, maybee add startup time comparison on access? + //TODO this is probably already done until we are ready first time?, maybe add startup time comparison on access? esp.delay.delay_ms( mppt_ina .configuration()? @@ -288,7 +292,7 @@ pub(crate) fn create_v4( } impl<'a> BoardInteraction<'a> for V4<'a> { - fn get_esp(&mut self) -> &mut ESP<'a> { + fn get_esp(&mut self) -> &mut Esp<'a> { &mut self.esp } @@ -306,7 +310,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> { fn deep_sleep(&mut self, duration_in_ms: u64) -> ! { self.awake.set_low().unwrap(); - self.charger.powersave(); + self.charger.power_save(); deep_sleep(duration_in_ms); } diff --git a/rust/src/log/mod.rs b/rust/src/log/mod.rs index 0b3384b..3315333 100644 --- a/rust/src/log/mod.rs +++ b/rust/src/log/mod.rs @@ -65,7 +65,7 @@ pub fn get_log() -> Vec { read_copy.push(copy); } drop(buffer); - return read_copy; + read_copy } pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short: &str, txt_long: &str) { diff --git a/rust/src/main.rs b/rust/src/main.rs index 5a9d60f..9f86aca 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,7 +1,7 @@ use crate::{ config::BoardVersion::INITIAL, hal::{PlantHal, HAL, PLANT_COUNT}, - webserver::webserver::httpd, + webserver::httpd, }; use anyhow::bail; use chrono::{DateTime, Datelike, Timelike, Utc}; @@ -12,7 +12,7 @@ use esp_idf_sys::{ esp_ota_img_states_t, esp_ota_img_states_t_ESP_OTA_IMG_ABORTED, esp_ota_img_states_t_ESP_OTA_IMG_INVALID, esp_ota_img_states_t_ESP_OTA_IMG_NEW, esp_ota_img_states_t_ESP_OTA_IMG_PENDING_VERIFY, esp_ota_img_states_t_ESP_OTA_IMG_UNDEFINED, - esp_ota_img_states_t_ESP_OTA_IMG_VALID, vTaskDelay, + esp_ota_img_states_t_ESP_OTA_IMG_VALID, }; use esp_ota::{mark_app_valid, rollback_and_reboot}; use hal::battery::BatteryState; @@ -28,13 +28,12 @@ mod hal; mod log; mod plant_state; mod tank; +mod webserver; pub static BOARD_ACCESS: Lazy> = Lazy::new(|| PlantHal::create().unwrap()); pub static STAY_ALIVE: Lazy = Lazy::new(|| AtomicBool::new(false)); -mod webserver { - pub mod webserver; -} + #[derive(Serialize, Deserialize, Debug, PartialEq)] enum WaitType { @@ -69,7 +68,7 @@ struct LightState { } #[derive(Serialize, Deserialize, Debug, PartialEq, Default)] -///mqtt stuct to track pump activities +///mqtt struct to track pump activities struct PumpInfo { enabled: bool, pump_ineffective: bool, @@ -175,16 +174,7 @@ fn safe_main() -> anyhow::Result<()> { } println!("cur is {}", cur); - match board - .board_hal - .get_battery_monitor() - .average_current_milli_ampere() - { - Ok(charging) => { - let _ = board.board_hal.set_charge_indicator(charging > 20); - } - Err(_) => {} - } + update_charge_indicator(&mut board); if board.board_hal.get_esp().get_restart_to_conf() { log(LogMessage::ConfigModeSoftwareOverride, 0, 0, "", ""); @@ -265,7 +255,7 @@ fn safe_main() -> anyhow::Result<()> { address, ota_state_string, &mut board, - &ip_address, + ip_address, timezone_time, ); publish_battery_state(&mut board); @@ -426,7 +416,7 @@ fn safe_main() -> anyhow::Result<()> { .board_hal .get_battery_monitor() .get_battery_state() - .unwrap_or(hal::battery::BatteryState::Unknown); + .unwrap_or(BatteryState::Unknown); let mut light_state = LightState { enabled: board.board_hal.get_config().night_lamp.enabled, @@ -556,6 +546,26 @@ fn safe_main() -> anyhow::Result<()> { .deep_sleep(1000 * 1000 * 60 * deep_sleep_duration_minutes as u64); } +fn update_charge_indicator(board: &mut MutexGuard) { + //we have mppt controller, ask it for charging current + if let Ok(current) = board.board_hal.get_mptt_current() { + let _ = board + .board_hal + .set_charge_indicator(current.as_milliamperes() > 20_f64); + } + //fallback to battery controller and ask it instead + else if let Ok(charging) = board + .board_hal + .get_battery_monitor() + .average_current_milli_ampere() + { + let _ = board.board_hal.set_charge_indicator(charging > 20); + } else { + //who knows + let _ = board.board_hal.set_charge_indicator(false); + } +} + fn obtain_tank_temperature(board: &mut MutexGuard) -> anyhow::Result { //multisample should be moved to water_temperature_c let mut attempt = 1; @@ -608,7 +618,7 @@ fn publish_plant_states( .zip(&board.board_hal.get_config().plants.clone()) .enumerate() { - match serde_json::to_string(&plant_state.to_mqtt_info(plant_conf, &timezone_time)) { + match serde_json::to_string(&plant_state.to_mqtt_info(plant_conf, timezone_time)) { Ok(state) => { let plant_topic = format!("/plant{}", plant_id + 1); let _ = board @@ -679,7 +689,7 @@ fn try_connect_wifi_sntp_mqtt(board: &mut MutexGuard) -> NetworkMode { SntpMode::OFFLINE } }; - let mqtt_connected = if let Some(_) = board.board_hal.get_config().network.mqtt_url { + let mqtt_connected = if board.board_hal.get_config().network.mqtt_url.is_some() { let nw_config = &board.board_hal.get_config().network.clone(); match board.board_hal.get_esp().mqtt(nw_config) { Ok(_) => { @@ -750,76 +760,59 @@ fn wait_infinity(wait_type: WaitType, reboot_now: Arc) -> ! { let delay = wait_type.blink_pattern(); let mut led_count = 8; let mut pattern_step = 0; - + let delay_handle = Delay::new_default(); loop { - unsafe { - let mut board = BOARD_ACCESS.lock().unwrap(); + let mut board = BOARD_ACCESS.lock().unwrap(); + update_charge_indicator(&mut board); - //we have mppt controller, ask it for charging current - if let Ok(current) = board.board_hal.get_mptt_current() { - let _ = board.board_hal.set_charge_indicator(current.as_milliamperes() > 20_f64); - } - //fallback to battery controller and ask it instead - else if let Ok(charging) = board - .board_hal - .get_battery_monitor() - .average_current_milli_ampere() - { - let _ = board.board_hal.set_charge_indicator(charging > 20); - } - else { - //who knows - let _ = board.board_hal.set_charge_indicator(false); - } - - match wait_type { - WaitType::MissingConfig => { - // Keep existing behavior: circular filling pattern - led_count %= 8; - led_count += 1; - for i in 0..8 { - let _ = board.board_hal.fault(i, i < led_count); - } - } - WaitType::ConfigButton => { - // Alternating pattern: 1010 1010 -> 0101 0101 - pattern_step = (pattern_step + 1) % 2; - for i in 0..8 { - let _ = board.board_hal.fault(i, (i + pattern_step) % 2 == 0); - } - } - WaitType::MqttConfig => { - // Moving dot pattern - pattern_step = (pattern_step + 1) % 8; - for i in 0..8 { - let _ = board.board_hal.fault(i, i == pattern_step); - } + match wait_type { + WaitType::MissingConfig => { + // Keep existing behavior: circular filling pattern + led_count %= 8; + led_count += 1; + for i in 0..8 { + let _ = board.board_hal.fault(i, i < led_count); } } - - board.board_hal.general_fault(true); - drop(board); - vTaskDelay(delay); - let mut board = BOARD_ACCESS.lock().unwrap(); - board.board_hal.general_fault(false); - - // Clear all LEDs - for i in 0..8 { - let _ = board.board_hal.fault(i, false); + WaitType::ConfigButton => { + // Alternating pattern: 1010 1010 -> 0101 0101 + pattern_step = (pattern_step + 1) % 2; + for i in 0..8 { + let _ = board.board_hal.fault(i, (i + pattern_step) % 2 == 0); + } } - drop(board); - vTaskDelay(delay); + WaitType::MqttConfig => { + // Moving dot pattern + pattern_step = (pattern_step + 1) % 8; + for i in 0..8 { + let _ = board.board_hal.fault(i, i == pattern_step); + } + } + } - if wait_type == WaitType::MqttConfig - && !STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed) - { - reboot_now.store(true, std::sync::atomic::Ordering::Relaxed); - } - if reboot_now.load(std::sync::atomic::Ordering::Relaxed) { - //ensure clean http answer - Delay::new_default().delay_ms(500); - BOARD_ACCESS.lock().unwrap().board_hal.deep_sleep(1); - } + board.board_hal.general_fault(true); + drop(board); + //cannot use shared delay as that is inside the mutex here + delay_handle.delay_ms(delay); + let mut board = BOARD_ACCESS.lock().unwrap(); + board.board_hal.general_fault(false); + + // Clear all LEDs + for i in 0..8 { + let _ = board.board_hal.fault(i, false); + } + drop(board); + delay_handle.delay_ms(delay); + + if wait_type == WaitType::MqttConfig + && !STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed) + { + reboot_now.store(true, std::sync::atomic::Ordering::Relaxed); + } + if reboot_now.load(std::sync::atomic::Ordering::Relaxed) { + //ensure clean http answer + Delay::new_default().delay_ms(500); + BOARD_ACCESS.lock().unwrap().board_hal.deep_sleep(1); } } } @@ -827,7 +820,7 @@ fn wait_infinity(wait_type: WaitType, reboot_now: Arc) -> ! { fn main() { let result = safe_main(); match result { - // this should not get triggered, safe_main should not return but go into deep sleep with sensbile + // this should not get triggered, safe_main should not return but go into deep sleep with sensible // timeout, this is just a fallback Ok(_) => { println!("Main app finished, restarting"); @@ -849,13 +842,13 @@ fn main() { } pub fn in_time_range(cur: &DateTime, start: u8, end: u8) -> bool { - let curhour = cur.hour() as u8; + let current_hour = cur.hour() as u8; //eg 10-14 if start < end { - curhour > start && curhour < end + current_hour > start && current_hour < end } else { //eg 20-05 - curhour > start || curhour < end + current_hour > start || current_hour < end } } diff --git a/rust/src/webserver/webserver.rs b/rust/src/webserver/mod.rs similarity index 100% rename from rust/src/webserver/webserver.rs rename to rust/src/webserver/mod.rs -- 2.47.1