diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 99b8b27..5993509 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -24,7 +24,7 @@ debug = false overflow-checks = true panic = "abort" incremental = true -opt-level = "z" +opt-level = 3 [profile.release] @@ -35,7 +35,7 @@ debug = false overflow-checks = true panic = "abort" incremental = true -opt-level = "z" +opt-level = 3 [package.metadata.espflash] diff --git a/rust/src/hal/esp.rs b/rust/src/hal/esp.rs index 3643755..6d239de 100644 --- a/rust/src/hal/esp.rs +++ b/rust/src/hal/esp.rs @@ -13,7 +13,7 @@ use alloc::{ use core::marker::PhantomData; use core::net::IpAddr; use core::str::FromStr; -use embassy_time::Timer; +use embassy_time::{Instant, Timer}; #[link_section = ".rtc.data"] static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT]; @@ -53,6 +53,7 @@ pub struct MqttClient<'a> { pub struct Esp<'a> { pub(crate) mqtt_client: Option>, pub(crate) dummy: PhantomData<&'a ()>, + pub(crate) wall_clock_offset: u64 //pub(crate) wifi_driver: EspWifi<'a>, //pub(crate) boot_button: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>, } @@ -87,13 +88,9 @@ impl Esp<'_> { //self.time() todo!(); } - pub(crate) fn time(&mut self) -> anyhow::Result> { - bail!("todo"); - // let time = EspSystemTime {}.now().as_millis(); - // let smaller_time = time as i64; - // let local_time = DateTime::from_timestamp_millis(smaller_time) - // .ok_or(anyhow!("could not convert timestamp"))?; - // anyhow::Ok(local_time) + pub(crate) fn time(&mut self) -> DateTime { + let wall_clock = Instant::now().as_millis() + self.wall_clock_offset; + DateTime::from_timestamp_millis(wall_clock as i64).unwrap() } pub(crate) async fn wifi_scan(&mut self) -> anyhow::Result> { diff --git a/rust/src/hal/mod.rs b/rust/src/hal/mod.rs index 22370c8..370f8bd 100644 --- a/rust/src/hal/mod.rs +++ b/rust/src/hal/mod.rs @@ -212,7 +212,8 @@ impl PlantHal { // let mut esp = Esp { mqtt_client: None, - dummy: PhantomData::default() + dummy: PhantomData::default(), + wall_clock_offset : 0 // wifi_driver, // boot_button }; diff --git a/rust/src/main.rs b/rust/src/main.rs index c14e4b5..1b83dc1 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -182,12 +182,8 @@ async fn safe_main() -> anyhow::Result<()> { .or_else(|err| { info!("rtc module error: {:?}", err); board.board_hal.general_fault(true); - board.board_hal.get_esp().time() - }) - .map_err(|err| -> Result<(), _> { - bail!("time error {}", err); - }) - .unwrap(); + anyhow::Ok(board.board_hal.get_esp().time()) + })?; //check if we know the time current > 2020 (plausibility checks, this code is newer than 2020) if cur.year() < 2020 {