pub struct TimeSync { /* private fields */ }Expand description
Maintain a linear mapping between device ticks and host time.
This struct collects timestamp measurement pairs and uses linear regression to compute a mapping from device ticks to host time. It supports:
- Configurable window size (number of samples to retain)
- Optional outlier trimming for robustness against jitter
- Both auto-fit (recompute on every update) and manual fit modes
Implementations§
Source§impl TimeSync
impl TimeSync
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Create a synchroniser with custom capacity.
Sourcepub fn set_auto_fit(&mut self, enabled: bool) -> &mut Self
pub fn set_auto_fit(&mut self, enabled: bool) -> &mut Self
Enable or disable automatic refitting on every update.
When disabled, you must call fit manually to update coefficients.
Sourcepub fn set_trim_outliers(&mut self, enabled: bool) -> &mut Self
pub fn set_trim_outliers(&mut self, enabled: bool) -> &mut Self
Enable or disable outlier trimming during fit.
When enabled, the 10% most extreme residuals are excluded from the regression when at least 10 samples are available.
Sourcepub fn coefficients(&self) -> (f64, f64)
pub fn coefficients(&self) -> (f64, f64)
Return the current slope and intercept of the time mapping.
Sourcepub fn samples(&self) -> impl Iterator<Item = (u64, Instant)> + '_
pub fn samples(&self) -> impl Iterator<Item = (u64, Instant)> + '_
Iterator over the samples contained in the sliding window.
Sourcepub fn origin_instant(&self) -> Option<Instant>
pub fn origin_instant(&self) -> Option<Instant>
Access the origin instant if at least one sample has been recorded.
Sourcepub fn origin_system(&self) -> Option<SystemTime>
pub fn origin_system(&self) -> Option<SystemTime>
Access the origin system time if available.
Sourcepub fn sample_bounds(&self) -> Option<((u64, Instant), (u64, Instant))>
pub fn sample_bounds(&self) -> Option<((u64, Instant), (u64, Instant))>
Return the first and last sample retained in the window.
Sourcepub fn set_freq_hz(&mut self, freq: f64)
pub fn set_freq_hz(&mut self, freq: f64)
Set the device tick frequency.
Sourcepub fn update(&mut self, dev_ts: u64, host_instant: Instant)
pub fn update(&mut self, dev_ts: u64, host_instant: Instant)
Add a new measurement pair to the regression window.
If auto-fit is enabled (default), the linear model is recomputed immediately.
Sourcepub fn fit(&mut self, freq_hz: Option<f64>) -> Option<(f64, f64)>
pub fn fit(&mut self, freq_hz: Option<f64>) -> Option<(f64, f64)>
Fit the linear model, optionally updating the frequency.
Returns the updated (slope, intercept) coefficients when enough samples
are available, or None if fewer than 2 samples exist.
Sourcepub fn to_host_time(&self, dev_ts: u64) -> SystemTime
pub fn to_host_time(&self, dev_ts: u64) -> SystemTime
Convert a device timestamp into a host SystemTime.