Compare commits
2 commits
23fd3babfc
...
b5ba28ee15
Author | SHA1 | Date | |
---|---|---|---|
![]() |
b5ba28ee15 | ||
![]() |
9c903b0c13 |
3 changed files with 13 additions and 20 deletions
|
@ -10,7 +10,7 @@ pub(crate) const T_ON: &'static str = "17:00";
|
||||||
pub(crate) const T_OFF: &'static str = "09:00";
|
pub(crate) const T_OFF: &'static str = "09:00";
|
||||||
|
|
||||||
/// Time zone for T_ON and T_OFF.
|
/// 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.
|
/// Access point SSID.
|
||||||
pub(crate) const WIFI_SSID: &'static str = "Example";
|
pub(crate) const WIFI_SSID: &'static str = "Example";
|
||||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -4,11 +4,7 @@ use anyhow::{bail, Result};
|
||||||
use chrono::{NaiveTime, Utc};
|
use chrono::{NaiveTime, Utc};
|
||||||
use esp_idf_svc::{
|
use esp_idf_svc::{
|
||||||
eventloop::EspSystemEventLoop,
|
eventloop::EspSystemEventLoop,
|
||||||
hal::{
|
hal::{delay::FreeRtos, gpio::*, prelude::Peripherals},
|
||||||
delay::FreeRtos,
|
|
||||||
gpio::{self, Gpio7, OutputPin, PinDriver},
|
|
||||||
prelude::Peripherals,
|
|
||||||
},
|
|
||||||
sntp::{EspSntp, SntpConf, SyncMode},
|
sntp::{EspSntp, SntpConf, SyncMode},
|
||||||
wifi::EspWifi,
|
wifi::EspWifi,
|
||||||
};
|
};
|
||||||
|
@ -22,15 +18,15 @@ mod wifi;
|
||||||
const CONTROL_LOOP_INTVL: u32 = 60000;
|
const CONTROL_LOOP_INTVL: u32 = 60000;
|
||||||
|
|
||||||
/// Struct holding resources which should remain in scope throughout execution.
|
/// Struct holding resources which should remain in scope throughout execution.
|
||||||
struct AppState<P: OutputPin> {
|
struct AppState {
|
||||||
ntp: EspSntp<'static>,
|
ntp: EspSntp<'static>,
|
||||||
switch_driver: PinDriver<'static, P, gpio::Output>,
|
switch_driver: PinDriver<'static>,
|
||||||
wifi: EspWifi<'static>,
|
wifi: EspWifi<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let AppState::<_> {
|
let AppState {
|
||||||
ntp: _ntp,
|
ntp: mut_ntp,
|
||||||
mut switch_driver,
|
mut switch_driver,
|
||||||
wifi: _wifi,
|
wifi: _wifi,
|
||||||
} = setup()?;
|
} = setup()?;
|
||||||
|
@ -75,7 +71,7 @@ fn main() -> Result<()> {
|
||||||
/// Runs initial application startup tasks, including connecting WiFi, syncing
|
/// Runs initial application startup tasks, including connecting WiFi, syncing
|
||||||
/// system time over SNTP, and setting the initial switch state. May block for
|
/// system time over SNTP, and setting the initial switch state. May block for
|
||||||
/// several seconds, or indefinitely if initial SNTP sync is not successful.
|
/// 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
|
// It is necessary to call this function once. Otherwise some patches to
|
||||||
// the runtime implemented by esp-idf-sys might not link properly. Refer
|
// the runtime implemented by esp-idf-sys might not link properly. Refer
|
||||||
// to https://github.com/esp-rs/esp-idf-template/issues/71.
|
// 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 peripherals = Peripherals::take()?;
|
||||||
let sysloop = EspSystemEventLoop::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()?;
|
switch_driver.set_low()?;
|
||||||
|
|
||||||
// Dropping EspWifi shuts down the connection, so this variable must remain
|
// 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);
|
crate::ntp::wait_for_sync(&ntp);
|
||||||
|
|
||||||
Ok(Box::new(AppState::<_> {
|
Ok(Box::new(AppState {
|
||||||
ntp,
|
ntp,
|
||||||
switch_driver,
|
switch_driver,
|
||||||
wifi: *wifi,
|
wifi,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use esp_idf_svc::{
|
use esp_idf_svc::sntp::{EspSntp, SyncStatus};
|
||||||
hal::delay::FreRtos,
|
|
||||||
sntp::{EspSntp, SyncStatus},
|
|
||||||
};
|
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
/// Delay between checks of the initial NTP sync check, in milliseconds.
|
/// 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
|
/// this function twice will cause the second invocation to wait for the next
|
||||||
/// synchronization event.
|
/// synchronization event.
|
||||||
pub(crate) fn wait_for_sync(client: &EspSntp) {
|
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...");
|
info!("Waiting for SNTP to sync...");
|
||||||
FreeRtos::delay_ms(SNTP_STATUS_POLL_INTVL);
|
FreeRtos::delay_ms(SNTP_STATUS_POLL_INTVL_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue