Refactor HAL modules: update async support in Water module and reorganize detect_sensors logic

- Replaced `Blocking` with `Async` for ADC operations in `Water` module.
- Improved `detect_sensors` implementation with better structure for sensor messages and autodetection.
- Updated tank ADC sampling to yield between readings, improving efficiency.
This commit is contained in:
2026-04-26 21:01:27 +02:00
parent f1c85d1d74
commit eb276cfa68
2 changed files with 146 additions and 150 deletions

View File

@@ -9,13 +9,15 @@ use esp_hal::pcnt::channel::CtrlMode::Keep;
use esp_hal::pcnt::channel::EdgeMode::{Hold, Increment};
use esp_hal::pcnt::unit::Unit;
use esp_hal::peripherals::GPIO5;
use esp_hal::Blocking;
use esp_hal::Async;
use esp_println::println;
use onewire::{ds18b20, Device, DeviceSearch, OneWire, DS18B20};
unsafe impl Send for TankSensor<'_> {}
pub struct TankSensor<'a> {
one_wire_bus: OneWire<Flex<'a>>,
tank_channel: Adc<'a, ADC1<'a>, Blocking>,
tank_channel: Adc<'a, ADC1<'a>, Async>,
tank_power: Output<'a>,
tank_pin: AdcPin<GPIO5<'a>, ADC1<'a>, AdcCalLine<ADC1<'a>>>,
flow_counter: Unit<'a, 1>,
@@ -35,7 +37,7 @@ impl<'a> TankSensor<'a> {
let mut adc1_config = AdcConfig::new();
let tank_pin =
adc1_config.enable_pin_with_cal::<_, AdcCalLine<_>>(gpio5, Attenuation::_11dB);
let tank_channel = Adc::new(adc1, adc1_config);
let tank_channel = Adc::new(adc1, adc1_config).into_async();
let one_wire_bus = OneWire::new(one_wire_pin, false);
@@ -139,15 +141,9 @@ impl<'a> TankSensor<'a> {
let mut store = [0_u16; TANK_MULTI_SAMPLE];
for sample in store.iter_mut() {
let value = self.tank_channel.read_oneshot(&mut self.tank_pin);
//force yield
*sample = self.tank_channel.read_oneshot(&mut self.tank_pin).await;
//force yield between successful samples
Timer::after_millis(10).await;
match value {
Ok(v) => *sample = v,
Err(e) => {
bail!("ADC Hardware error: {:?}", e);
}
};
}
self.tank_power.set_low();