Compare commits

..

2 commits

Author SHA1 Message Date
Brent Schroeter
23fd3babfc move time zone selection to config.rs 2025-09-06 12:57:46 -07:00
Brent Schroeter
9a5a1d219f refactor setup tasks 2025-09-06 12:57:46 -07:00
3 changed files with 20 additions and 13 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::TimeZone = chrono_tz::US::Pacific;
pub(crate) const TZ: chrono_tz::Tz = chrono_tz::US::Pacific;
/// Access point SSID.
pub(crate) const WIFI_SSID: &'static str = "Example";

View file

@ -4,7 +4,11 @@ use anyhow::{bail, Result};
use chrono::{NaiveTime, Utc};
use esp_idf_svc::{
eventloop::EspSystemEventLoop,
hal::{delay::FreeRtos, gpio::*, prelude::Peripherals},
hal::{
delay::FreeRtos,
gpio::{self, Gpio7, OutputPin, PinDriver},
prelude::Peripherals,
},
sntp::{EspSntp, SntpConf, SyncMode},
wifi::EspWifi,
};
@ -18,15 +22,15 @@ mod wifi;
const CONTROL_LOOP_INTVL: u32 = 60000;
/// Struct holding resources which should remain in scope throughout execution.
struct AppState {
struct AppState<P: OutputPin> {
ntp: EspSntp<'static>,
switch_driver: PinDriver<'static>,
switch_driver: PinDriver<'static, P, gpio::Output>,
wifi: EspWifi<'static>,
}
fn main() -> Result<()> {
let AppState {
ntp: mut_ntp,
let AppState::<_> {
ntp: _ntp,
mut switch_driver,
wifi: _wifi,
} = setup()?;
@ -71,7 +75,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>> {
fn setup() -> Result<Box<AppState<Gpio7>>> {
// 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.
@ -83,7 +87,7 @@ fn setup() -> Result<Box<AppState>> {
let peripherals = Peripherals::take()?;
let sysloop = EspSystemEventLoop::take()?;
let mut switch_driver = PinDriver::output(app_state.peripherals.pins.gpio7)?;
let mut switch_driver = PinDriver::output(peripherals.pins.gpio7)?;
switch_driver.set_low()?;
// Dropping EspWifi shuts down the connection, so this variable must remain
@ -99,9 +103,9 @@ fn setup() -> Result<Box<AppState>> {
})?;
crate::ntp::wait_for_sync(&ntp);
Ok(Box::new(AppState {
Ok(Box::new(AppState::<_> {
ntp,
switch_driver,
wifi,
wifi: *wifi,
}))
}

View file

@ -1,4 +1,7 @@
use esp_idf_svc::sntp::{EspSntp, SyncStatus};
use esp_idf_svc::{
hal::delay::FreRtos,
sntp::{EspSntp, SyncStatus},
};
use log::info;
/// Delay between checks of the initial NTP sync check, in milliseconds.
@ -10,8 +13,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 ntp.get_sync_status() != SyncStatus::Completed {
while client.get_sync_status() != SyncStatus::Completed {
info!("Waiting for SNTP to sync...");
FreeRtos::delay_ms(SNTP_STATUS_POLL_INTVL_MS);
FreeRtos::delay_ms(SNTP_STATUS_POLL_INTVL);
}
}