fixed lifetime annotations + thread safty still needs work

This commit is contained in:
ju6ge 2025-06-19 18:17:29 +02:00
parent fc1991523a
commit e2cbf9618e
Signed by: judge
GPG Key ID: 6512C30DD8E017B5
5 changed files with 110 additions and 83 deletions

View File

@ -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 anyhow::{bail, Result};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use embedded_hal::digital::OutputPin; use embedded_hal::digital::OutputPin;
use esp_idf_hal::gpio::{InputOutput, PinDriver}; 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 struct Initial<'a> {
pub(crate) general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, 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) config: PlantControllerConfig,
pub(crate) battery: Box<dyn BatteryInteraction>,
} }
impl BoardInteraction<'_> for Initial<'_> { pub(crate) fn create_initial_board(
fn get_esp<'a>(&mut self) -> &mut ESP<'a> { free_pins: FreePeripherals,
fs_mount_error: bool,
config: PlantControllerConfig,
esp: ESP<'static>,
) -> Result<Box<dyn BoardInteraction<'static>> + 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 &mut self.esp
} }
@ -22,11 +46,8 @@ impl BoardInteraction<'_> for Initial<'_> {
&self.config &self.config
} }
fn get_battery_monitor(&mut self) -> Box<dyn BatteryInteraction> { fn get_battery_monitor(&mut self) -> &mut Box<dyn BatteryInteraction> {
let v = BatteryMonitor::Disabled { &mut self.battery
};
Box::new(v) as Box<dyn BatteryInteraction>
} }
fn set_charge_indicator(&mut self, charging: bool) -> Result<()> { fn set_charge_indicator(&mut self, charging: bool) -> Result<()> {
@ -96,4 +117,4 @@ impl BoardInteraction<'_> for Initial<'_> {
fn test(&mut self) -> Result<()> { fn test(&mut self) -> Result<()> {
bail!("Please configure board revision") bail!("Please configure board revision")
} }
} }

View File

@ -42,10 +42,9 @@ use crate::log::log;
use embedded_hal::digital::OutputPin; use embedded_hal::digital::OutputPin;
use esp_idf_hal::delay::Delay; use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{ use esp_idf_hal::gpio::{
Gpio0, Gpio1, Gpio10, Gpio11, Gpio12, Gpio13, Gpio14, Gpio15, Gpio16, Gpio17, Gpio0, Gpio1, Gpio10, Gpio11, Gpio12, Gpio13, Gpio14, Gpio15, Gpio16, Gpio17, Gpio18, Gpio2,
Gpio18, Gpio2, Gpio21, Gpio22, Gpio23, Gpio24, Gpio25, Gpio26, Gpio27, Gpio28, Gpio21, Gpio22, Gpio23, Gpio24, Gpio25, Gpio26, Gpio27, Gpio28, Gpio29, Gpio3, Gpio30, Gpio4,
Gpio29, Gpio3, Gpio30, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, IOPin, Gpio5, Gpio6, Gpio7, Gpio8, IOPin, PinDriver, Pull,
PinDriver, Pull,
}; };
use esp_idf_hal::pcnt::PCNT0; use esp_idf_hal::pcnt::PCNT0;
use esp_idf_hal::prelude::Peripherals; use esp_idf_hal::prelude::Peripherals;
@ -63,11 +62,7 @@ pub static I2C_DRIVER: Lazy<Mutex<I2cDriver<'static>>> = Lazy::new(PlantHal::cre
#[non_exhaustive] #[non_exhaustive]
struct V3Constants; struct V3Constants;
impl V3Constants { impl V3Constants {}
}
const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC); const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC);
@ -98,7 +93,7 @@ pub enum Sensor {
pub struct PlantHal {} pub struct PlantHal {}
pub struct HAL<'a> { pub struct HAL<'a> {
pub board_hal: Box<dyn BoardInteraction<'a>>, pub board_hal: Box<dyn BoardInteraction<'a> + Sync + Send>,
} }
#[derive(Serialize, Deserialize, PartialEq, Debug)] #[derive(Serialize, Deserialize, PartialEq, Debug)]
@ -282,8 +277,6 @@ impl PlantHal {
let config = esp.load_config(); let config = esp.load_config();
let hal = match config { let hal = match config {
Result::Ok(config) => { Result::Ok(config) => {
let battery_monitor: BatteryMonitor = match config.hardware.battery { let battery_monitor: BatteryMonitor = match config.hardware.battery {
@ -313,19 +306,19 @@ impl PlantHal {
}; };
let battery_interaction = Box::new(battery_monitor) as Box<dyn BatteryInteraction>; let battery_interaction = Box::new(battery_monitor) as Box<dyn BatteryInteraction>;
let board_hal: Box<dyn BoardInteraction> = match config.hardware.board { let board_hal: Box<dyn BoardInteraction> = match config.hardware.board {
BoardVersion::INITIAL => { 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) => { Err(err) => {
log( log(
@ -336,30 +329,16 @@ impl PlantHal {
&err.to_string(), &err.to_string(),
); );
HAL { 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)) Ok(Mutex::new(hal))
} }
fn create_initial_board(free_pins: FreePeripherals, fs_mount_error: bool, config: PlantControllerConfig, esp: ESP) -> Result<Box<dyn BoardInteraction<'static>>> {
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))
}
} }

View File

@ -1,5 +1,10 @@
use crate::config::PlantControllerConfig;
use crate::hal::battery::{BatteryInteraction, BatteryMonitor};
use crate::hal::esp::ESP; 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 crate::log::{log, LogMessage};
use anyhow::{anyhow, bail, Ok, Result}; use anyhow::{anyhow, bail, Ok, Result};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
@ -8,19 +13,20 @@ use ds323x::{DateTimeAccess, Ds323x};
use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr}; use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr};
use embedded_hal::digital::OutputPin; use embedded_hal::digital::OutputPin;
use embedded_hal_bus::i2c::MutexDevice; 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::oneshot::{AdcChannelDriver, AdcDriver};
use esp_idf_hal::adc::{attenuation, Resolution};
use esp_idf_hal::delay::Delay; use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, PinDriver, Pull}; use esp_idf_hal::gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, PinDriver, Pull};
use esp_idf_hal::i2c::I2cDriver; 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 esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use one_wire_bus::OneWire; use one_wire_bus::OneWire;
use plant_ctrl2::sipo::ShiftRegister40; use plant_ctrl2::sipo::ShiftRegister40;
use std::result::Result::Ok as OkStd; use std::result::Result::Ok as OkStd;
use esp_idf_hal::adc::{attenuation, Resolution}; use std::sync::Arc;
use esp_idf_hal::adc::oneshot::config::AdcChannelConfig;
use crate::config::PlantControllerConfig;
use crate::hal::battery::{BatteryInteraction, BatteryMonitor};
const PUMP8_BIT: usize = 0; const PUMP8_BIT: usize = 0;
const PUMP1_BIT: usize = 1; 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<dyn BatteryInteraction>) -> Result<Box<dyn BoardInteraction + '_>> { pub(crate) fn create_v3(
peripherals: FreePeripherals,
esp: ESP<'static>,
config: PlantControllerConfig,
battery_monitor: Box<dyn BatteryInteraction>,
) -> Result<Box<dyn BoardInteraction + '_ + Send + Sync>> {
let mut clock = PinDriver::input_output(peripherals.gpio15.downgrade())?; let mut clock = PinDriver::input_output(peripherals.gpio15.downgrade())?;
clock.set_pull(Pull::Floating)?; clock.set_pull(Pull::Floating)?;
let mut latch = PinDriver::input_output(peripherals.gpio3.downgrade())?; 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<'_> { impl<'a> BoardInteraction<'a> for V3<'a> {
fn get_esp(&mut self) -> &mut ESP<'static> { fn get_esp(&mut self) -> &mut ESP<'a> {
&mut self.esp &mut self.esp
} }
@ -618,4 +629,4 @@ impl BoardInteraction<'_> for V3<'_> {
self.esp.delay.delay_ms(10); self.esp.delay.delay_ms(10);
Ok(()) Ok(())
} }
} }

View File

@ -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 anyhow::{anyhow, bail};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use ds18b20::Ds18b20; use ds18b20::Ds18b20;
@ -5,22 +13,19 @@ use ds323x::{DateTimeAccess, Ds323x};
use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr}; use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr};
use embedded_hal::digital::OutputPin; use embedded_hal::digital::OutputPin;
use embedded_hal_bus::i2c::MutexDevice; 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::oneshot::{AdcChannelDriver, AdcDriver};
use esp_idf_hal::adc::{attenuation, Resolution};
use esp_idf_hal::delay::Delay; use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, Output, PinDriver, Pull}; use esp_idf_hal::gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, Output, PinDriver, Pull};
use esp_idf_hal::i2c::I2cDriver; 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 esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use one_wire_bus::OneWire; use one_wire_bus::OneWire;
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface}; 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 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 MS0: u8 = 1_u8;
const MS1: u8 = 0_u8; const MS1: u8 = 0_u8;
@ -54,8 +59,12 @@ pub struct V4<'a> {
sensor_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>, sensor_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
} }
pub(crate) fn create_v4(
pub(crate) fn create_v4(peripherals: FreePeripherals, esp:ESP, config:PlantControllerConfig, battery_monitor: Box<dyn BatteryInteraction>) -> anyhow::Result<Box<dyn BoardInteraction + '_>> { peripherals: FreePeripherals,
esp: ESP<'static>,
config: PlantControllerConfig,
battery_monitor: Box<dyn BatteryInteraction>,
) -> anyhow::Result<Box<dyn BoardInteraction + '_ + Send + Sync>> {
let mut awake = PinDriver::output(peripherals.gpio15.downgrade())?; let mut awake = PinDriver::output(peripherals.gpio15.downgrade())?;
awake.set_high()?; awake.set_high()?;
@ -181,8 +190,8 @@ pub(crate) fn create_v4(peripherals: FreePeripherals, esp:ESP, config:PlantContr
Ok(Box::new(v)) Ok(Box::new(v))
} }
impl BoardInteraction<'_> for V4<'_> { impl<'a> BoardInteraction<'a> for V4<'a> {
fn get_esp(&mut self) -> &mut ESP<'_> { fn get_esp(&mut self) -> &mut ESP<'a> {
&mut self.esp &mut self.esp
} }
@ -195,7 +204,9 @@ impl BoardInteraction<'_> for V4<'_> {
} }
fn set_charge_indicator(&mut self, charging: bool) -> anyhow::Result<()> { 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(()) Ok(())
} }
@ -358,18 +369,22 @@ impl BoardInteraction<'_> for V4<'_> {
fn pump(&mut self, plant: usize, enable: bool) -> anyhow::Result<()> { fn pump(&mut self, plant: usize, enable: bool) -> anyhow::Result<()> {
if enable { 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 { } 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(()) anyhow::Ok(())
} }
fn fault(&mut self, plant: usize, enable: bool) -> anyhow::Result<()> { fn fault(&mut self, plant: usize, enable: bool) -> anyhow::Result<()> {
if enable { 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 { } 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(()) anyhow::Ok(())
} }
@ -380,7 +395,6 @@ impl BoardInteraction<'_> for V4<'_> {
self.signal_counter.counter_pause()?; self.signal_counter.counter_pause()?;
self.signal_counter.counter_clear()?; self.signal_counter.counter_clear()?;
//Disable all //Disable all
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?; 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_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 delay = Delay::new_default();
let measurement = 100; // TODO what is this scaling factor? what is its purpose? 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); delay.delay_ms(measurement);
self.signal_counter.counter_pause()?; self.signal_counter.counter_pause()?;
self.sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?; 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, MS0)?;
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS1)?; self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS1)?;
self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS2)?; self.sensor_expander.pin_set_low(GPIOBank::Bank0, MS2)?;

View File

@ -31,7 +31,7 @@ use tank::*;
use crate::config::BoardVersion::INITIAL; use crate::config::BoardVersion::INITIAL;
use crate::hal::{BoardInteraction, PlantHal, HAL, PLANT_COUNT}; use crate::hal::{BoardInteraction, PlantHal, HAL, PLANT_COUNT};
use crate::hal::battery::BatteryInteraction; use crate::hal::battery::BatteryInteraction;
pub static BOARD_ACCESS: Lazy<Mutex<HAL>> = Lazy::new(|| PlantHal::create().unwrap()); pub static BOARD_ACCESS: Lazy<Arc<Mutex<HAL>>> = Lazy::new(|| Arc::new(PlantHal::create().unwrap()));
pub static STAY_ALIVE: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false)); pub static STAY_ALIVE: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
mod webserver { mod webserver {