mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 10:22:02 +00:00
feat: use HACI_SERIAL_PORT to pick serial port and support mock PTY
Co-authored-by: aider (openai/gpt-5) <aider@aider.chat>
This commit is contained in:
parent
d63a019584
commit
ee4a5a3630
1 changed files with 24 additions and 1 deletions
25
src/main.rs
25
src/main.rs
|
|
@ -18,6 +18,7 @@ use daemonize::Daemonize;
|
|||
use serialport;
|
||||
|
||||
use std::{
|
||||
env,
|
||||
fs,
|
||||
path::Path,
|
||||
sync::{mpsc, Arc, Mutex},
|
||||
|
|
@ -74,6 +75,28 @@ fn ensure_config() {
|
|||
}
|
||||
|
||||
fn find_serial_port() -> Option<String> {
|
||||
// Prefer environment variable if provided (e.g., by entrypoint for mock PTY)
|
||||
if let Ok(path) = env::var("HACI_SERIAL_PORT") {
|
||||
let p = Path::new(&path);
|
||||
if !p.exists() {
|
||||
// Wait up to ~5 seconds for the PTY to appear
|
||||
for _ in 0..50 {
|
||||
if p.exists() {
|
||||
break;
|
||||
}
|
||||
thread::sleep(std::time::Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
if p.exists() {
|
||||
return Some(path);
|
||||
} else {
|
||||
log::warn!(
|
||||
"HACI_SERIAL_PORT='{}' does not exist. Falling back to port scan.",
|
||||
path
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(ports) = serialport::available_ports() {
|
||||
for p in ports {
|
||||
if p.port_name.starts_with("/dev/ttyACM")
|
||||
|
|
@ -172,7 +195,7 @@ async fn main() {
|
|||
let serial_port_path = match find_serial_port() {
|
||||
Some(port) => port,
|
||||
None => {
|
||||
log::error!("❌ No serial port found. Please connect the Teensy device.");
|
||||
log::error!("❌ No serial port found. Set HACI_SERIAL_PORT to a device path (e.g., /dev/ttyACM0) or connect the Teensy device.");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue