Skip to main content

ClockSync

Struct ClockSync 

Source
pub struct ClockSync { /* private fields */ }
Available on crate feature 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

Source

pub const fn new_static() -> ClockSyncStatic

Create ClockSync resources.

Source

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.

Source

pub async fn wait_for_tick(&self) -> ClockSyncTick

Wait for and return the next tick after sync.

See the ClockSync struct example for usage.

Source

pub fn now_local(&self) -> OffsetDateTime

Get the current local time without waiting for a tick.

Source

pub async fn set_offset_minutes(&self, minutes: i32)

Update the UTC offset used for local time.

Source

pub fn offset_minutes(&self) -> i32

Get the current UTC offset in minutes.

Source

pub async fn set_tick_interval(&self, interval: Option<Duration>)

Set the tick interval. Use None to disable periodic ticks.

Source

pub async fn set_speed(&self, speed_multiplier: f32)

Update the speed multiplier (1.0 = real time).

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.