ups-esp32c3-rust/src/time.rs

34 lines
801 B
Rust

use embedded_svc::sys_time::SystemTime;
use esp_idf_svc::sntp::{self, EspSntp};
use esp_idf_svc::systime::EspSystemTime;
use log::{info};
use std::time::{Duration};
pub struct Time {
sntp: Option<Box<EspSntp>>,
}
impl Time {
pub fn new () -> Time {
return Time {
sntp: None
}
}
pub fn sync(&mut self) -> anyhow::Result<()> {
let sntp = sntp::EspSntp::new_default().map_err(|err| {
anyhow::anyhow!("ESP SNTP Failed: {:?}", err)
})?;
self.sntp = Some(Box::new(sntp));
return anyhow::Ok(())
}
pub fn get_time(&mut self) -> Duration {
if let Some(ref mut sntp) = self.sntp {
info!("ESP SNTP sync status {:?}", sntp.get_sync_status());
}
EspSystemTime {}.now()
}
}