initial push of Rust version

This commit is contained in:
Chris Frankland-Wright 2025-07-08 20:57:15 +01:00
parent fba1dfc2be
commit 138b1e07a8
5 changed files with 262 additions and 0 deletions

46
src/serial_input.rs Normal file
View file

@ -0,0 +1,46 @@
use std::io::BufRead;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::Sender;
use chrono::Utc;
use regex::Regex;
use crate::sync_logic::{LtcFrame, LtcState};
pub fn start_serial_thread(
port_path: &str,
baud_rate: u32,
sender: Sender<LtcFrame>,
state: Arc<Mutex<LtcState>>,
) {
println!("📡 Attempting to open serial port: {} @ {} baud", port_path, baud_rate);
let port = 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
}
}
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...");
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);
}
}
}
}
}