Refactor async logging to synchronous; improve error handling consistency across modules.

This commit is contained in:
Kai Börnert
2026-04-13 17:03:47 +02:00
parent 964bdb0454
commit 8ce00c9d95
12 changed files with 104 additions and 121 deletions

View File

@@ -607,16 +607,14 @@ impl Esp<'_> {
0,
"",
"",
)
.await;
);
log(
LogMessage::LowVoltage,
LOW_VOLTAGE_DETECTED as u32,
0,
"",
"",
)
.await;
);
// is executed before main, no other code will alter these values during printing
#[allow(static_mut_refs)]
for (i, time) in LAST_WATERING_TIMESTAMP.iter().enumerate() {
@@ -695,9 +693,9 @@ impl Esp<'_> {
))?;
spawner.spawn(mqtt_runner(task))?;
log(LogMessage::StayAlive, 0, 0, "", &stay_alive_topic).await;
log(LogMessage::StayAlive, 0, 0, "", &stay_alive_topic);
log(LogMessage::MqttInfo, 0, 0, "", mqtt_url).await;
log(LogMessage::MqttInfo, 0, 0, "", mqtt_url);
let mqtt_timeout = 15000;
let res = async {
@@ -839,10 +837,10 @@ async fn mqtt_incoming_task(
true => 1,
false => 0,
};
log(LogMessage::MqttStayAliveRec, a, 0, "", "").await;
log(LogMessage::MqttStayAliveRec, a, 0, "", "");
MQTT_STAY_ALIVE.store(value, Ordering::Relaxed);
} else {
log(LogMessage::UnknownTopic, 0, 0, "", &topic).await;
log(LogMessage::UnknownTopic, 0, 0, "", &topic);
}
}
},

View File

@@ -85,14 +85,13 @@ use esp_alloc as _;
use esp_backtrace as _;
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState};
use esp_hal::delay::Delay;
use esp_hal::i2c::master::{BusTimeout, Config, FsmTimeout, I2c, SoftwareTimeout};
use esp_hal::i2c::master::{BusTimeout, Config, FsmTimeout, I2c};
use esp_hal::interrupt::software::SoftwareInterruptControl;
use esp_hal::pcnt::unit::Unit;
use esp_hal::pcnt::Pcnt;
use esp_hal::rng::Rng;
use esp_hal::rtc_cntl::{Rtc, SocResetReason};
use esp_hal::system::reset_reason;
use esp_hal::time::Rate;
use esp_hal::timer::timg::{MwdtStage, TimerGroup, Wdt};
use esp_hal::uart::Uart;
use esp_hal::Blocking;
@@ -255,7 +254,8 @@ impl PlantHal {
esp_alloc::heap_allocator!(size: 64 * 1024);
esp_alloc::heap_allocator!(#[link_section = ".dram2_uninit"] size: 64000);
let rtc_peripheral: Rtc = Rtc::new(peripherals.LPWR);
let mut rtc_peripheral: Rtc = Rtc::new(peripherals.LPWR);
rtc_peripheral.rwdt.disable();
let timg0 = TimerGroup::new(peripherals.TIMG0);
let sw_int = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
@@ -469,8 +469,7 @@ impl PlantHal {
to_config_mode as u32,
"",
&format!("{reasons:?}"),
)
.await;
);
esp.init_rtc_deepsleep_memory(init_rtc_store, to_config_mode)
.await;
@@ -575,8 +574,7 @@ impl PlantHal {
0,
"",
&err.to_string(),
)
.await;
);
HAL {
board_hal: v4_hal::create_v4(
free_pins,

View File

@@ -1,5 +1,5 @@
use crate::hal::Box;
use crate::fat_error::FatResult;
use crate::hal::Box;
use async_trait::async_trait;
use bincode::{Decode, Encode};
use chrono::{DateTime, Utc};
@@ -26,7 +26,7 @@ pub trait RTCModuleInteraction {
async fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> FatResult<()>;
fn write(&mut self, offset: u32, data: &[u8]) -> FatResult<()>;
fn read(&mut self, offset:u32, data: &mut [u8]) -> FatResult<()>;
fn read(&mut self, offset: u32, data: &mut [u8]) -> FatResult<()>;
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Encode, Decode)]
@@ -67,7 +67,7 @@ impl RTCModuleInteraction for DS3231Module {
Ok(())
}
fn read(&mut self, offset:u32, data: &mut [u8]) -> FatResult<()> {
fn read(&mut self, offset: u32, data: &mut [u8]) -> FatResult<()> {
self.storage.read(offset, data)?;
Ok(())
}

View File

@@ -136,9 +136,7 @@ impl SavegameManager {
let slot = self.storage.scan()?;
match slot {
None => Ok(None),
Some(slot) => {
self.load_slot(slot.idx)
}
Some(slot) => self.load_slot(slot.idx),
}
}

View File

@@ -350,8 +350,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
}
Some(pump_ina) => {
let raw = pump_ina.shunt_voltage()?;
let shunt_voltage =
Voltage::from_microvolts(raw.shunt_voltage_uv().abs() as f64);
let shunt_voltage = Voltage::from_microvolts(raw.shunt_voltage_uv().abs() as f64);
let shut_value = Resistance::from_ohms(0.05_f64);
let current = shunt_voltage.as_volts() / shut_value.as_ohms();
Ok(Current::from_amperes(current))
@@ -516,8 +515,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
for plant in 0..PLANT_COUNT {
let a = moisture.sensor_a_hz[plant].unwrap_or(0.0) as u32;
let b = moisture.sensor_b_hz[plant].unwrap_or(0.0) as u32;
log(LogMessage::TestSensor, a, b, &(plant + 1).to_string(), "")
.await;
log(LogMessage::TestSensor, a, b, &(plant + 1).to_string(), "");
}
Timer::after_millis(10).await;
Ok(())