Rust ported and tested version

This commit is contained in:
Chris Frankland-Wright 2025-07-09 01:18:49 +01:00
parent 138b1e07a8
commit bd2111d77a
8 changed files with 415 additions and 109 deletions

View file

@ -1,3 +1,5 @@
// src/sync_logic.rs
use chrono::{DateTime, Local, Timelike, Utc};
use regex::Captures;
use std::collections::VecDeque;
@ -10,7 +12,7 @@ pub struct LtcFrame {
pub seconds: u32,
pub frames: u32,
pub frame_rate: f64,
pub timestamp: DateTime<Utc>,
pub timestamp: DateTime<Utc>, // arrival stamp
}
impl LtcFrame {
@ -26,6 +28,7 @@ impl LtcFrame {
})
}
/// Compare just HH:MM:SS against local time.
pub fn matches_system_time(&self) -> bool {
let local = Local::now();
local.hour() == self.hours
@ -38,8 +41,8 @@ pub struct LtcState {
pub latest: Option<LtcFrame>,
pub lock_count: u32,
pub free_count: u32,
/// Stores the last up-to-20 raw offset measurements in ms.
pub offset_history: VecDeque<i64>,
pub hardware_offset_ms: i64,
pub last_match_status: String,
pub last_match_check: i64,
}
@ -51,62 +54,73 @@ impl LtcState {
lock_count: 0,
free_count: 0,
offset_history: VecDeque::with_capacity(20),
hardware_offset_ms: 0,
last_match_status: "UNKNOWN".to_string(),
last_match_status: "UNKNOWN".into(),
last_match_check: 0,
}
}
/// Record one measured offset in ms, maintaining a sliding window of up to 20 samples.
pub fn record_offset(&mut self, offset_ms: i64) {
if self.offset_history.len() == 20 {
self.offset_history.pop_front();
}
self.offset_history.push_back(offset_ms);
}
/// Clear all stored offset measurements (e.g. on FREE-run).
pub fn clear_offsets(&mut self) {
self.offset_history.clear();
}
/// Update LOCK/FREE counts, clear offsets on FREE, and refresh timecode-match every 5 s.
pub fn update(&mut self, frame: LtcFrame) {
match frame.status.as_str() {
"LOCK" => self.lock_count += 1,
"LOCK" => {
self.lock_count += 1;
}
"FREE" => {
self.free_count += 1;
self.offset_history.clear();
self.last_match_status = "UNKNOWN".to_string();
self.clear_offsets();
self.last_match_status = "UNKNOWN".into();
}
_ => {}
}
if frame.status == "LOCK" {
let now = Utc::now();
let offset_ms = (now - frame.timestamp).num_milliseconds() - self.hardware_offset_ms;
if self.offset_history.len() == 20 {
self.offset_history.pop_front();
}
self.offset_history.push_back(offset_ms);
}
// Every 5 seconds, recompute whether HH:MM:SS matches local time
let now_secs = Utc::now().timestamp();
if now_secs - self.last_match_check >= 5 {
self.last_match_status = if frame.matches_system_time() {
"IN SYNC"
"IN SYNC".into()
} else {
"OUT OF SYNC"
}
.to_string();
"OUT OF SYNC".into()
};
self.last_match_check = now_secs;
}
self.latest = Some(frame);
}
/// Average jitter over the stored history, in milliseconds.
pub fn average_jitter(&self) -> i64 {
if self.offset_history.is_empty() {
return 0;
0
} else {
let sum: i64 = self.offset_history.iter().sum();
sum / (self.offset_history.len() as i64)
}
self.offset_history.iter().sum::<i64>() / self.offset_history.len() as i64
}
/// Convert that average jitter into frames (rounded).
pub fn average_frames(&self) -> i64 {
if let Some(frame) = &self.latest {
let frame_time = 1000.0 / frame.frame_rate;
(self.average_jitter() as f64 / frame_time).round() as i64
let ms_per_frame = 1000.0 / frame.frame_rate;
(self.average_jitter() as f64 / ms_per_frame).round() as i64
} else {
0
}
}
/// Percentage of samples seen in LOCK state versus total.
pub fn lock_ratio(&self) -> f64 {
let total = self.lock_count + self.free_count;
if total == 0 {
@ -116,6 +130,7 @@ impl LtcState {
}
}
/// Get the last computed timecodematch status ("IN SYNC", "OUT OF SYNC", or "UNKNOWN").
pub fn timecode_match(&self) -> &str {
&self.last_match_status
}