fixed lifetime annotations + thread safty still needs work

This commit is contained in:
2025-06-19 18:17:29 +02:00
parent fc1991523a
commit e2cbf9618e
5 changed files with 110 additions and 83 deletions

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 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<MutexDevice<'a, I2cDriver<'a>>>,
}
pub(crate) fn create_v4(peripherals: FreePeripherals, esp:ESP, config:PlantControllerConfig, battery_monitor: Box<dyn BatteryInteraction>) -> anyhow::Result<Box<dyn BoardInteraction + '_>> {
pub(crate) fn create_v4(
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())?;
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)?;