diff --git a/Cargo.toml b/Cargo.toml index 6947682..2b4302d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,12 +94,12 @@ sachy-config = { git = "https://tangled.org/sachy.dev/sachy-embed-core", package [profile.dev] debug = true lto = true -opt-level = "z" +opt-level = "s" incremental = false codegen-units = 1 [profile.release] debug = true lto = true -opt-level = "z" +opt-level = "s" codegen-units = 1 diff --git a/src/detector.rs b/src/detector.rs index 7b5f9af..85d7968 100644 --- a/src/detector.rs +++ b/src/detector.rs @@ -141,9 +141,11 @@ impl Detector<'_> { data: &mut Sender<'static, NoopRawMutex, (i64, Vec)>, rtc: GlobalRtc<'static>, ) { + let track_time = rtc.track_time().await; + loop { let (timestamp, buf) = data.send().await; - *timestamp = rtc.get_timestamp().await.timestamp_micros(); + *timestamp = track_time.timestamp(); self.adc.sample(buf).await; data.send_done(); } @@ -199,6 +201,7 @@ impl Detector<'_> { async fn tick(&self, rtc: GlobalRtc<'static>) { let mut inactive: Option = None; let mut interval = Ticker::every(Duration::from_secs(1)); + let track_time = rtc.track_time().await; loop { interval.next().await; @@ -224,8 +227,8 @@ impl Detector<'_> { } None => { if let Some(net_data) = UpdateConnection::can_update() { - let now = rtc.get_timestamp().await; - data::transmit_level_update(now.timestamp(), decay, &net_data); + let now = track_time.timestamp(); + data::transmit_level_update(now, decay, &net_data); } } _ => continue, diff --git a/src/rtc.rs b/src/rtc.rs index 75ac672..1837474 100644 --- a/src/rtc.rs +++ b/src/rtc.rs @@ -51,13 +51,20 @@ impl GlobalRtc<'_> { inner.rtc.is_running() && inner.rtc.alarm_scheduled().is_some() } - pub async fn get_timestamp(&self) -> ChronoTime { + async fn get_timestamp(&self) -> ChronoTime { let inner = self.0.read().await; let elapsed = inner.offset.elapsed().as_micros() as i64; inner.timestamp + TimeDelta::microseconds(elapsed) } + pub async fn track_time(&self) -> TimeTracker { + TimeTracker { + timestamp: self.get_timestamp().await, + offset: Instant::now(), + } + } + pub async fn set_rtc_datetime(&self, timestamp: SntpTimestamp) -> Result<(), PicoError> { let timestamp = timestamp.try_to_utc()?; @@ -119,6 +126,19 @@ impl GlobalRtc<'_> { } } +pub struct TimeTracker { + timestamp: ChronoTime, + offset: Instant, +} + +impl TimeTracker { + #[inline] + pub fn timestamp(&self) -> i64 { + let elapsed = self.offset.elapsed().as_micros() as i64; + (self.timestamp + TimeDelta::microseconds(elapsed)).timestamp_micros() + } +} + pub struct GlobalRtcHandler; impl embassy_rp::interrupt::typelevel::Handler