From e2cbf9618e18e7e3b9fc6e8b4c34bf349f133a03 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Thu, 19 Jun 2025 18:17:29 +0200 Subject: [PATCH] fixed lifetime annotations + thread safty still needs work --- rust/src/hal/initial_hal.rs | 45 ++++++++++++++++++++-------- rust/src/hal/mod.rs | 59 ++++++++++++------------------------- rust/src/hal/v3_hal.rs | 31 ++++++++++++------- rust/src/hal/v4_hal.rs | 56 ++++++++++++++++++++++------------- rust/src/main.rs | 2 +- 5 files changed, 110 insertions(+), 83 deletions(-) diff --git a/rust/src/hal/initial_hal.rs b/rust/src/hal/initial_hal.rs index bd7bc6b..d054f10 100644 --- a/rust/src/hal/initial_hal.rs +++ b/rust/src/hal/initial_hal.rs @@ -1,20 +1,44 @@ -use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, Sensor}; +use crate::config::{BoardHardware, PlantControllerConfig}; +use crate::hal::battery::{BatteryInteraction, BatteryMonitor}; +use crate::hal::esp::ESP; +use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, Sensor, FreePeripherals}; +use esp_idf_hal::gpio::{IOPin, Pull}; use anyhow::{bail, Result}; use chrono::{DateTime, Utc}; use embedded_hal::digital::OutputPin; use esp_idf_hal::gpio::{InputOutput, PinDriver}; -use crate::config::{BoardHardware, PlantControllerConfig}; -use crate::hal::battery::{BatteryInteraction, BatteryMonitor}; -use crate::hal::esp::ESP; pub struct Initial<'a> { pub(crate) general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, pub(crate) esp: ESP<'a>, pub(crate) config: PlantControllerConfig, + pub(crate) battery: Box, } -impl BoardInteraction<'_> for Initial<'_> { - fn get_esp<'a>(&mut self) -> &mut ESP<'a> { +pub(crate) fn create_initial_board( + free_pins: FreePeripherals, + fs_mount_error: bool, + config: PlantControllerConfig, + esp: ESP<'static>, +) -> Result> + Send + Sync> { + let mut general_fault = PinDriver::input_output(free_pins.gpio6.downgrade())?; + general_fault.set_pull(Pull::Floating)?; + general_fault.set_low()?; + + if fs_mount_error { + general_fault.set_high()? + } + let v = Initial { + general_fault, + config, + esp, + battery: Box::new(BatteryMonitor::Disabled {}), + }; + Ok(Box::new(v)) +} + +impl<'a> BoardInteraction<'a> for Initial<'a> { + fn get_esp(&mut self) -> &mut ESP<'a> { &mut self.esp } @@ -22,11 +46,8 @@ impl BoardInteraction<'_> for Initial<'_> { &self.config } - fn get_battery_monitor(&mut self) -> Box { - let v = BatteryMonitor::Disabled { - - }; - Box::new(v) as Box + fn get_battery_monitor(&mut self) -> &mut Box { + &mut self.battery } fn set_charge_indicator(&mut self, charging: bool) -> Result<()> { @@ -96,4 +117,4 @@ impl BoardInteraction<'_> for Initial<'_> { fn test(&mut self) -> Result<()> { bail!("Please configure board revision") } -} \ No newline at end of file +} diff --git a/rust/src/hal/mod.rs b/rust/src/hal/mod.rs index 5f17bb8..79ca9e0 100644 --- a/rust/src/hal/mod.rs +++ b/rust/src/hal/mod.rs @@ -42,10 +42,9 @@ 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, + 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; @@ -63,11 +62,7 @@ pub static I2C_DRIVER: Lazy>> = Lazy::new(PlantHal::cre #[non_exhaustive] struct V3Constants; -impl V3Constants { - -} - - +impl V3Constants {} const X25: crc::Crc = crc::Crc::::new(&crc::CRC_16_IBM_SDLC); @@ -98,7 +93,7 @@ pub enum Sensor { pub struct PlantHal {} pub struct HAL<'a> { - pub board_hal: Box>, + pub board_hal: Box + Sync + Send>, } #[derive(Serialize, Deserialize, PartialEq, Debug)] @@ -282,8 +277,6 @@ impl PlantHal { let config = esp.load_config(); - - let hal = match config { Result::Ok(config) => { let battery_monitor: BatteryMonitor = match config.hardware.battery { @@ -313,19 +306,19 @@ impl PlantHal { }; let battery_interaction = Box::new(battery_monitor) as Box; - let board_hal: Box = match config.hardware.board { BoardVersion::INITIAL => { - Self::create_initial_board(free_pins, fs_mount_error, config, esp)? + initial_hal::create_initial_board(free_pins, fs_mount_error, config, esp)? + } + BoardVersion::V3 => { + v3_hal::create_v3(free_pins, esp, config, battery_interaction)? + } + BoardVersion::V4 => { + v4_hal::create_v4(free_pins, esp, config, battery_interaction)? } - BoardVersion::V3 => v3_hal::create_v3(free_pins,esp, config, battery_interaction)?, - BoardVersion::V4 => v4_hal::create_v4(free_pins,esp,config,battery_interaction)?, }; - - HAL { - board_hal - } + HAL { board_hal } } Err(err) => { log( @@ -336,30 +329,16 @@ impl PlantHal { &err.to_string(), ); HAL { - board_hal:Self::create_initial_board(free_pins, fs_mount_error, PlantControllerConfig::default(), esp )? + board_hal: initial_hal::create_initial_board( + free_pins, + fs_mount_error, + PlantControllerConfig::default(), + esp, + )?, } } }; Ok(Mutex::new(hal)) } - - fn create_initial_board(free_pins: FreePeripherals, fs_mount_error: bool, config: PlantControllerConfig, esp: ESP) -> Result>> { - let mut general_fault = - PinDriver::input_output(free_pins.gpio6.downgrade())?; - general_fault.set_pull(Pull::Floating)?; - general_fault.set_low()?; - - if fs_mount_error { - general_fault.set_high()? - } - let v = Initial { - general_fault, - config, - esp - }; - Ok(Box::new(v)) - } - - } diff --git a/rust/src/hal/v3_hal.rs b/rust/src/hal/v3_hal.rs index f4756c7..e8ee742 100644 --- a/rust/src/hal/v3_hal.rs +++ b/rust/src/hal/v3_hal.rs @@ -1,5 +1,10 @@ +use crate::config::PlantControllerConfig; +use crate::hal::battery::{BatteryInteraction, BatteryMonitor}; 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}; +use crate::hal::{ + deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor, V3Constants, I2C_DRIVER, + PLANT_COUNT, REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE, X25, +}; use crate::log::{log, LogMessage}; use anyhow::{anyhow, bail, Ok, Result}; use chrono::{DateTime, Utc}; @@ -8,19 +13,20 @@ 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::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 esp_idf_hal::adc::{attenuation, Resolution}; -use esp_idf_hal::adc::oneshot::config::AdcChannelConfig; -use crate::config::PlantControllerConfig; -use crate::hal::battery::{BatteryInteraction, BatteryMonitor}; +use std::sync::Arc; const PUMP8_BIT: usize = 0; const PUMP1_BIT: usize = 1; @@ -96,7 +102,12 @@ pub struct V3<'a> { >, } -pub(crate) fn create_v3(peripherals: FreePeripherals, esp: ESP, config: PlantControllerConfig, battery_monitor: Box) -> Result> { +pub(crate) fn create_v3( + peripherals: FreePeripherals, + esp: ESP<'static>, + config: PlantControllerConfig, + battery_monitor: Box, +) -> Result> { let mut clock = PinDriver::input_output(peripherals.gpio15.downgrade())?; clock.set_pull(Pull::Floating)?; let mut latch = PinDriver::input_output(peripherals.gpio3.downgrade())?; @@ -235,8 +246,8 @@ pub(crate) fn create_v3(peripherals: FreePeripherals, esp: ESP, config: PlantCon })) } -impl BoardInteraction<'_> for V3<'_> { - fn get_esp(&mut self) -> &mut ESP<'static> { +impl<'a> BoardInteraction<'a> for V3<'a> { + fn get_esp(&mut self) -> &mut ESP<'a> { &mut self.esp } @@ -618,4 +629,4 @@ impl BoardInteraction<'_> for V3<'_> { self.esp.delay.delay_ms(10); Ok(()) } -} \ No newline at end of file +} diff --git a/rust/src/hal/v4_hal.rs b/rust/src/hal/v4_hal.rs index 8c3bac2..ddb1419 100644 --- a/rust/src/hal/v4_hal.rs +++ b/rust/src/hal/v4_hal.rs @@ -1,3 +1,11 @@ +use crate::config::PlantControllerConfig; +use crate::hal::battery::{BatteryInteraction, BatteryMonitor}; +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, +}; +use crate::log::{log, LogMessage}; use anyhow::{anyhow, bail}; use chrono::{DateTime, Utc}; use ds18b20::Ds18b20; @@ -5,22 +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::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 pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface}; -use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT, REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE, X25}; -use crate::hal::esp::ESP; use std::result::Result::Ok as OkStd; -use esp_idf_hal::adc::{attenuation, Resolution}; -use esp_idf_hal::adc::oneshot::config::AdcChannelConfig; -use crate::config::PlantControllerConfig; -use crate::hal::battery::{BatteryInteraction, BatteryMonitor}; -use crate::log::{log, LogMessage}; const MS0: u8 = 1_u8; const MS1: u8 = 0_u8; @@ -54,8 +59,12 @@ pub struct V4<'a> { sensor_expander: Pca9535Immediate>>, } - -pub(crate) fn create_v4(peripherals: FreePeripherals, esp:ESP, config:PlantControllerConfig, battery_monitor: Box) -> anyhow::Result> { +pub(crate) fn create_v4( + peripherals: FreePeripherals, + esp: ESP<'static>, + config: PlantControllerConfig, + battery_monitor: Box, +) -> anyhow::Result> { let mut awake = PinDriver::output(peripherals.gpio15.downgrade())?; awake.set_high()?; @@ -181,8 +190,8 @@ pub(crate) fn create_v4(peripherals: FreePeripherals, esp:ESP, config:PlantContr Ok(Box::new(v)) } -impl BoardInteraction<'_> for V4<'_> { - fn get_esp(&mut self) -> &mut ESP<'_> { +impl<'a> BoardInteraction<'a> for V4<'a> { + fn get_esp(&mut self) -> &mut ESP<'a> { &mut self.esp } @@ -195,7 +204,9 @@ impl BoardInteraction<'_> for V4<'_> { } fn set_charge_indicator(&mut self, charging: bool) -> anyhow::Result<()> { - self.charge_indicator.set_state(charging.into()).expect("cannot fail"); + self.charge_indicator + .set_state(charging.into()) + .expect("cannot fail"); Ok(()) } @@ -358,18 +369,22 @@ impl BoardInteraction<'_> for V4<'_> { fn pump(&mut self, plant: usize, enable: bool) -> anyhow::Result<()> { if enable { - self.pump_expander.pin_set_high(GPIOBank::Bank0, plant.try_into()?)?; + self.pump_expander + .pin_set_high(GPIOBank::Bank0, plant.try_into()?)?; } else { - self.pump_expander.pin_set_low(GPIOBank::Bank0, plant.try_into()?)?; + self.pump_expander + .pin_set_low(GPIOBank::Bank0, plant.try_into()?)?; } anyhow::Ok(()) } fn fault(&mut self, plant: usize, enable: bool) -> anyhow::Result<()> { if enable { - self.pump_expander.pin_set_high(GPIOBank::Bank1, plant.try_into()?)? + self.pump_expander + .pin_set_high(GPIOBank::Bank1, plant.try_into()?)? } else { - self.pump_expander.pin_set_low(GPIOBank::Bank1, plant.try_into()?)? + self.pump_expander + .pin_set_low(GPIOBank::Bank1, plant.try_into()?)? } anyhow::Ok(()) } @@ -380,7 +395,6 @@ impl BoardInteraction<'_> for V4<'_> { self.signal_counter.counter_pause()?; self.signal_counter.counter_clear()?; - //Disable all self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?; @@ -412,7 +426,8 @@ impl BoardInteraction<'_> for V4<'_> { } self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS4)?; - self.sensor_expander.pin_set_high(GPIOBank::Bank0, SENSOR_ON)?; + self.sensor_expander + .pin_set_high(GPIOBank::Bank0, SENSOR_ON)?; let delay = Delay::new_default(); let measurement = 100; // TODO what is this scaling factor? what is its purpose? @@ -424,7 +439,8 @@ impl BoardInteraction<'_> for V4<'_> { delay.delay_ms(measurement); self.signal_counter.counter_pause()?; self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?; - self.sensor_expander.pin_set_low(GPIOBank::Bank0, SENSOR_ON)?; + self.sensor_expander + .pin_set_low(GPIOBank::Bank0, SENSOR_ON)?; self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS0)?; self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS1)?; self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS2)?; diff --git a/rust/src/main.rs b/rust/src/main.rs index 8012341..89ad933 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -31,7 +31,7 @@ use tank::*; use crate::config::BoardVersion::INITIAL; use crate::hal::{BoardInteraction, PlantHal, HAL, PLANT_COUNT}; use crate::hal::battery::BatteryInteraction; -pub static BOARD_ACCESS: Lazy> = Lazy::new(|| PlantHal::create().unwrap()); +pub static BOARD_ACCESS: Lazy>> = Lazy::new(|| Arc::new(PlantHal::create().unwrap())); pub static STAY_ALIVE: Lazy = Lazy::new(|| AtomicBool::new(false)); mod webserver {