refactor: unify moisture handling, update config structure, and add peer dependencies

This commit is contained in:
2025-10-23 22:44:44 +02:00
parent 1db3f7af64
commit cafe1b264e
26 changed files with 516 additions and 69 deletions

View File

@@ -1,17 +1,16 @@
use crate::bail;
use crate::fat_error::{ContextExt, FatError, FatResult};
use crate::fat_error::{ContextExt, FatResult};
use crate::hal::Box;
use crate::hal::{DetectionResult, Moistures, Sensor};
use crate::log::{LogMessage, LOG_ACCESS};
use alloc::format;
use alloc::string::ToString;
use async_trait::async_trait;
use bincode::config;
use canapi::id::{classify, plant_id, MessageKind, IDENTIFY_CMD_OFFSET};
use canapi::SensorSlot;
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_time::{Duration, Instant, Timer, WithTimeout};
use embassy_time::{Duration, Timer, WithTimeout};
use embedded_can::{Frame, Id};
use esp_hal::gpio::Output;
use esp_hal::i2c::master::I2c;
@@ -86,7 +85,7 @@ impl SensorInteraction for SensorImpl {
} => {
can_power.set_high();
let config = twai_config.take().expect("twai config not set");
let mut twai = config.start();
let mut twai = config.into_async().start();
loop {
let rec = twai.receive();
@@ -100,15 +99,17 @@ impl SensorInteraction for SensorImpl {
}
Timer::after_millis(10).await;
let can = Self::inner_can(&mut twai).await;
let mut moistures = Moistures::default();
let _ = Self::wait_for_can_measurements(&mut twai, &mut moistures).with_timeout(Duration::from_millis(5000)).await;
can_power.set_low();
let config = twai.stop();
let config = twai.stop().into_blocking();
twai_config.replace(config);
let value = can?;
Ok(value)
Ok(moistures)
}
}
}
@@ -160,9 +161,8 @@ impl SensorImpl {
}
}
let mut result = DetectionResult::default();
// Wait for messages to arrive
let _ = Self::wait_for_can_measurements(&mut as_async, &mut result)
let mut moistures = Moistures::default();
let _ = Self::wait_for_can_measurements(&mut as_async, &mut moistures)
.with_timeout(Duration::from_millis(5000))
.await;
@@ -170,6 +170,8 @@ impl SensorImpl {
can_power.set_low();
twai_config.replace(config);
let result= moistures.into();
info!("Autodetection result: {result:?}");
Ok(result)
}
@@ -178,8 +180,9 @@ impl SensorImpl {
async fn wait_for_can_measurements(
as_async: &mut Twai<'_, Async>,
result: &mut DetectionResult,
) {
moistures: &mut Moistures,
) -> FatResult<()> {
loop {
match as_async.receive_async().await {
Ok(can_frame) => match can_frame.id() {
@@ -196,12 +199,14 @@ impl SensorImpl {
if msg.0 == MessageKind::MoistureData {
let plant = msg.1 as usize;
let sensor = msg.2;
let data = can_frame.data();
match sensor {
SensorSlot::A => {
result.plant[plant].sensor_a = true;
moistures.sensor_a_hz[plant] = data[0] as f32;
}
SensorSlot::B => {
result.plant[plant].sensor_b = true;
moistures.sensor_b_hz[plant] = data[0] as f32;
}
}
}
@@ -214,7 +219,6 @@ impl SensorImpl {
},
Err(err) => {
error!("Error receiving CAN message: {err:?}");
break;
}
}
}
@@ -302,31 +306,17 @@ impl SensorImpl {
let median = results[mid];
Ok(median)
}
async fn inner_can(twai: &mut Twai<'static, Blocking>) -> FatResult<Moistures> {
config::standard();
let timeout = Instant::now()
.checked_add(embassy_time::Duration::from_millis(100))
.context("Timeout")?;
loop {
let answer = twai.receive();
match answer {
Ok(answer) => {
info!("Received CAN message: {answer:?}");
}
Err(error) => match error {
nb::Error::Other(error) => {
return Err(FatError::CanBusError { error });
}
nb::Error::WouldBlock => {
if Instant::now() > timeout {
bail!("Timeout waiting for CAN answer");
}
Timer::after_millis(10).await;
}
},
}
}
}
}
impl From<Moistures> for DetectionResult {
fn from(value: Moistures) -> Self {
let mut result = DetectionResult::default();
for (plant, sensor) in value.sensor_a_hz.iter().enumerate() {
result.plant[plant].sensor_a = *sensor > 1.0_f32;
}
for (plant, sensor) in value.sensor_b_hz.iter().enumerate() {
result.plant[plant].sensor_b = *sensor > 1.0_f32;
}
result
}
}