remove: eliminate MoistureSensorState::Disabled, simplify moisture sensor processing, refactor pump logic, and clean up redundant/unnecessary code

This commit is contained in:
2026-03-12 20:36:18 +01:00
parent 5c78495bd5
commit a4d764c4fe
10 changed files with 145 additions and 144 deletions

View File

@@ -11,7 +11,7 @@ use crate::hal::{
};
use crate::log::{LogMessage, LOG_ACCESS};
use alloc::boxed::Box;
use alloc::string::{ToString};
use alloc::string::ToString;
use async_trait::async_trait;
use canapi::id::{classify, plant_id, MessageKind, IDENTIFY_CMD_OFFSET};
use canapi::SensorSlot;
@@ -133,10 +133,9 @@ pub struct V4<'a> {
extra1: Output<'a>,
extra2: Output<'a>,
twai_config: Option<TwaiConfiguration<'static, Blocking>>
twai_config: Option<TwaiConfiguration<'static, Blocking>>,
}
pub(crate) async fn create_v4(
peripherals: FreePeripherals<'static>,
esp: Esp<'static>,
@@ -263,12 +262,11 @@ pub(crate) async fn create_v4(
extra1,
extra2,
can_power,
twai_config
twai_config,
};
Ok(Box::new(v))
}
#[async_trait(?Send)]
impl<'a> BoardInteraction<'a> for V4<'a> {
fn get_tank_sensor(&mut self) -> Result<&mut TankSensor<'a>, FatError> {
@@ -395,11 +393,18 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
if !detect {
continue;
}
let target =
StandardId::new(plant_id(IDENTIFY_CMD_OFFSET, sensor.into(), (plant +1) as u16))
.context(">> Could not create address for sensor! (plant: {}) <<")?;
let target = StandardId::new(plant_id(
IDENTIFY_CMD_OFFSET,
sensor.into(),
(plant + 1) as u16,
))
.context(">> Could not create address for sensor! (plant: {}) <<")?;
let can_buffer = [0_u8; 0];
info!("Sending test message to plant {} sensor {sensor:?} with id {}", plant +1, target.as_raw());
info!(
"Sending test message to plant {} sensor {sensor:?} with id {}",
plant + 1,
target.as_raw()
);
if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) {
// Try a few times; we intentionally ignore rx here and rely on stub logic
let resu = twai
@@ -407,12 +412,12 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
.with_timeout(Duration::from_millis(3000))
.await;
match resu {
Ok(_) => {
}
Ok(_) => {}
Err(err) => {
info!(
"Error sending test message to plant {} sensor {sensor:?}: {err:?}", plant +1
);
"Error sending test message to plant {} sensor {sensor:?}: {err:?}",
plant + 1
);
}
}
} else {
@@ -426,20 +431,17 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
.with_timeout(Duration::from_millis(3000))
.await;
let config = twai.stop().into_blocking();
self.twai_config.replace(config);
self.can_power.set_low();
let result = moistures.into();
info!("Autodetection result: {result:?}");
Ok(result)
}
async fn general_fault(&mut self, enable: bool) {
hold_disable(23);
self.general_fault.set_level(enable.into());
@@ -482,7 +484,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
LOG_ACCESS
.lock()
.await
.log(LogMessage::TestSensor, a, b, &(plant+1).to_string(), "")
.log(LogMessage::TestSensor, a, b, &(plant + 1).to_string(), "")
.await;
}
Timer::after_millis(10).await;
@@ -511,7 +513,6 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
}
}
async fn wait_for_can_measurements(
as_async: &mut Twai<'_, Async>,
moistures: &mut Moistures,
@@ -538,10 +539,12 @@ async fn wait_for_can_measurements(
let frequency = u32::from_be_bytes(bytes);
match sensor {
SensorSlot::A => {
moistures.sensor_a_hz[plant-1] = Some(frequency as f32);
moistures.sensor_a_hz[plant - 1] =
Some(frequency as f32);
}
SensorSlot::B => {
moistures.sensor_b_hz[plant-1] = Some(frequency as f32);
moistures.sensor_b_hz[plant - 1] =
Some(frequency as f32);
}
}
} else {