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,4 +1,6 @@
use std::io::BufRead;
// src/serial_input.rs
use std::io::BufRead;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::Sender;
use chrono::Utc;
@ -10,35 +12,43 @@ pub fn start_serial_thread(
baud_rate: u32,
sender: Sender<LtcFrame>,
state: Arc<Mutex<LtcState>>,
_hardware_offset_ms: i64, // no longer used here
) {
println!("📡 Attempting to open serial port: {} @ {} baud", port_path, baud_rate);
println!("📡 Opening serial port {} @ {} baud", port_path, baud_rate);
let port = serialport::new(port_path, baud_rate)
let port = match serialport::new(port_path, baud_rate)
.timeout(std::time::Duration::from_millis(1000))
.open();
match &port {
Ok(_) => println!("✅ Serial port opened successfully"),
Err(e) => {
eprintln!("❌ Failed to open serial port: {}", e);
return; // Exit early, no point continuing
.open()
{
Ok(p) => {
println!("✅ Serial port opened");
p
}
}
Err(e) => {
eprintln!("❌ Serial open failed: {}", e);
return;
}
};
let reader = std::io::BufReader::new(port.unwrap());
let re = Regex::new(r"\[(LOCK|FREE)\]\s+(\d{2}):(\d{2}):(\d{2})[:;](\d{2})\s+\|\s+([\d.]+)fps")
.unwrap();
println!("🔄 Starting LTC read loop...");
let reader = std::io::BufReader::new(port);
let re = Regex::new(
r"\[(LOCK|FREE)\]\s+(\d{2}):(\d{2}):(\d{2})[:;](\d{2})\s+\|\s+([\d.]+)fps",
)
.unwrap();
println!("🔄 Entering LTC read loop…");
for line in reader.lines() {
if let Ok(line) = line {
if let Some(caps) = re.captures(&line) {
let frame = LtcFrame::from_regex(&caps, Utc::now());
if let Some(frame) = frame {
sender.send(frame.clone()).ok();
let mut state_lock = state.lock().unwrap();
state_lock.update(frame);
if let Ok(text) = line {
if let Some(caps) = re.captures(&text) {
let arrival = Utc::now();
if let Some(frame) = LtcFrame::from_regex(&caps, arrival) {
// update LOCK/FREE counts & timestamp
{
let mut st = state.lock().unwrap();
st.update(frame.clone());
}
// forward raw frame
let _ = sender.send(frame);
}
}
}