pub struct ClockSync { /* private fields */ }wifi and target_os=none only.Expand description
Combines NTP synchronization with a local clock and tick events.
ClockSync does not emit ticks until the first successful sync (or a manual
call to ClockSync::set_utc_time). Each tick includes how long it has been
since the last successful sync.
§Example: WiFi + ClockSync logging
use device_envoy::{
Error,
Result,
button::PressedTo,
clock_sync::{ClockSync, ClockSyncStatic, ONE_SECOND, h12_m_s},
flash_array::FlashArray,
wifi_auto::fields::{TimezoneField, TimezoneFieldStatic},
wifi_auto::{WifiAuto, WifiAutoEvent},
};
use defmt::info;
async fn run(
spawner: embassy_executor::Spawner,
p: embassy_rp::Peripherals,
) -> Result<(), device_envoy::Error> {
let [wifi_credentials_flash_block, timezone_flash_block] = FlashArray::<2>::new(p.FLASH)?;
static TIMEZONE_STATIC: TimezoneFieldStatic = TimezoneField::new_static();
let timezone_field = TimezoneField::new(&TIMEZONE_STATIC, timezone_flash_block);
let wifi_auto = WifiAuto::new(
p.PIN_23,
p.PIN_24,
p.PIN_25,
p.PIN_29,
p.PIO0,
p.DMA_CH0,
wifi_credentials_flash_block,
p.PIN_13,
PressedTo::Ground,
"ClockSync",
[timezone_field],
spawner,
)?;
let (stack, _button) = wifi_auto
.connect(|event| async move {
match event {
WifiAutoEvent::CaptivePortalReady => {
info!("WifiAuto: setup mode ready");
}
WifiAutoEvent::Connecting { .. } => {
info!("WifiAuto: connecting");
}
WifiAutoEvent::ConnectionFailed => {
info!("WifiAuto: connection failed");
}
}
Ok(())
})
.await?;
let offset_minutes = timezone_field
.offset_minutes()?
.ok_or(Error::MissingCustomWifiAutoField)?;
static CLOCK_SYNC_STATIC: ClockSyncStatic = ClockSync::new_static();
let clock_sync = ClockSync::new(
&CLOCK_SYNC_STATIC,
stack,
offset_minutes,
Some(ONE_SECOND),
spawner,
);
loop {
let tick = clock_sync.wait_for_tick().await;
let (hours, minutes, seconds) = h12_m_s(&tick.local_time);
info!(
"Time {:02}:{:02}:{:02}, since sync {}s",
hours,
minutes,
seconds,
tick.since_last_sync.as_secs()
);
}
}Implementations§
Source§impl ClockSync
impl ClockSync
Sourcepub const fn new_static() -> ClockSyncStatic
pub const fn new_static() -> ClockSyncStatic
Create ClockSync resources.
Sourcepub fn new(
clock_sync_static: &'static ClockSyncStatic,
stack: &'static Stack<'static>,
offset_minutes: i32,
tick_interval: Option<Duration>,
spawner: Spawner,
) -> Self
pub fn new( clock_sync_static: &'static ClockSyncStatic, stack: &'static Stack<'static>, offset_minutes: i32, tick_interval: Option<Duration>, spawner: Spawner, ) -> Self
Create a ClockSync using an existing network stack.
See the ClockSync struct example for usage.
Sourcepub async fn wait_for_tick(&self) -> ClockSyncTick
pub async fn wait_for_tick(&self) -> ClockSyncTick
Wait for and return the next tick after sync.
See the ClockSync struct example for usage.
Sourcepub fn now_local(&self) -> OffsetDateTime
pub fn now_local(&self) -> OffsetDateTime
Get the current local time without waiting for a tick.
Sourcepub async fn set_offset_minutes(&self, minutes: i32)
pub async fn set_offset_minutes(&self, minutes: i32)
Update the UTC offset used for local time.
Sourcepub fn offset_minutes(&self) -> i32
pub fn offset_minutes(&self) -> i32
Get the current UTC offset in minutes.
Sourcepub async fn set_tick_interval(&self, interval: Option<Duration>)
pub async fn set_tick_interval(&self, interval: Option<Duration>)
Set the tick interval. Use None to disable periodic ticks.
Sourcepub async fn set_speed(&self, speed_multiplier: f32)
pub async fn set_speed(&self, speed_multiplier: f32)
Update the speed multiplier (1.0 = real time).
Sourcepub async fn set_utc_time(&self, unix_seconds: UnixSeconds)
pub async fn set_utc_time(&self, unix_seconds: UnixSeconds)
Manually set the current UTC time and mark the clock as synced.
Auto Trait Implementations§
impl Freeze for ClockSync
impl !RefUnwindSafe for ClockSync
impl Send for ClockSync
impl Sync for ClockSync
impl Unpin for ClockSync
impl !UnwindSafe for ClockSync
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more