Compare commits

..

2 commits

Author SHA1 Message Date
Brent Schroeter
b5ba28ee15 move time zone selection to config.rs 2025-09-06 12:31:29 -07:00
Brent Schroeter
9c903b0c13 refactor setup tasks 2025-09-06 12:31:06 -07:00
3 changed files with 13 additions and 20 deletions

View file

@ -10,7 +10,7 @@ pub(crate) const T_ON: &'static str = "17:00";
pub(crate) const T_OFF: &'static str = "09:00";
/// Time zone for T_ON and T_OFF.
pub(crate) const TZ: chrono_tz::Tz = chrono_tz::US::Pacific;
pub(crate) const TZ: chrono::TimeZone = chrono_tz::US::Pacific;
/// Access point SSID.
pub(crate) const WIFI_SSID: &'static str = "Example";

View file

@ -4,11 +4,7 @@ use anyhow::{bail, Result};
use chrono::{NaiveTime, Utc};
use esp_idf_svc::{
eventloop::EspSystemEventLoop,
hal::{
delay::FreeRtos,
gpio::{self, Gpio7, OutputPin, PinDriver},
prelude::Peripherals,
},
hal::{delay::FreeRtos, gpio::*, prelude::Peripherals},
sntp::{EspSntp, SntpConf, SyncMode},
wifi::EspWifi,
};
@ -22,15 +18,15 @@ mod wifi;
const CONTROL_LOOP_INTVL: u32 = 60000;
/// Struct holding resources which should remain in scope throughout execution.
struct AppState<P: OutputPin> {
struct AppState {
ntp: EspSntp<'static>,
switch_driver: PinDriver<'static, P, gpio::Output>,
switch_driver: PinDriver<'static>,
wifi: EspWifi<'static>,
}
fn main() -> Result<()> {
let AppState::<_> {
ntp: _ntp,
let AppState {
ntp: mut_ntp,
mut switch_driver,
wifi: _wifi,
} = setup()?;
@ -75,7 +71,7 @@ fn main() -> Result<()> {
/// Runs initial application startup tasks, including connecting WiFi, syncing
/// system time over SNTP, and setting the initial switch state. May block for
/// several seconds, or indefinitely if initial SNTP sync is not successful.
fn setup() -> Result<Box<AppState<Gpio7>>> {
fn setup() -> Result<Box<AppState>> {
// It is necessary to call this function once. Otherwise some patches to
// the runtime implemented by esp-idf-sys might not link properly. Refer
// to https://github.com/esp-rs/esp-idf-template/issues/71.
@ -87,7 +83,7 @@ fn setup() -> Result<Box<AppState<Gpio7>>> {
let peripherals = Peripherals::take()?;
let sysloop = EspSystemEventLoop::take()?;
let mut switch_driver = PinDriver::output(peripherals.pins.gpio7)?;
let mut switch_driver = PinDriver::output(app_state.peripherals.pins.gpio7)?;
switch_driver.set_low()?;
// Dropping EspWifi shuts down the connection, so this variable must remain
@ -103,9 +99,9 @@ fn setup() -> Result<Box<AppState<Gpio7>>> {
})?;
crate::ntp::wait_for_sync(&ntp);
Ok(Box::new(AppState::<_> {
Ok(Box::new(AppState {
ntp,
switch_driver,
wifi: *wifi,
wifi,
}))
}

View file

@ -1,7 +1,4 @@
use esp_idf_svc::{
hal::delay::FreRtos,
sntp::{EspSntp, SyncStatus},
};
use esp_idf_svc::sntp::{EspSntp, SyncStatus};
use log::info;
/// Delay between checks of the initial NTP sync check, in milliseconds.
@ -13,8 +10,8 @@ const SNTP_STATUS_POLL_INTVL: u32 = 2000;
/// this function twice will cause the second invocation to wait for the next
/// synchronization event.
pub(crate) fn wait_for_sync(client: &EspSntp) {
while client.get_sync_status() != SyncStatus::Completed {
while ntp.get_sync_status() != SyncStatus::Completed {
info!("Waiting for SNTP to sync...");
FreeRtos::delay_ms(SNTP_STATUS_POLL_INTVL);
FreeRtos::delay_ms(SNTP_STATUS_POLL_INTVL_MS);
}
}