last attempt

This commit is contained in:
Chris Frankland-Wright 2025-06-24 23:24:33 +01:00
parent 5bd6a822dd
commit 1bf16999b7

View file

@ -1,34 +1,48 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import curses import os
import subprocess import subprocess
import shutil import curses
import time import time
import shutil
import select import select
import signal
def start_ltc_stream(): FIFO_PATH = "/tmp/ltcpipe"
# Launch arecord piped into ltcdump
arecord = subprocess.Popen( def setup_fifo():
["arecord", "-f", "S16_LE", "-c", "1", "-r", "48000", "-D", "hw:2,0"], if os.path.exists(FIFO_PATH):
stdout=subprocess.PIPE, os.remove(FIFO_PATH)
stderr=subprocess.DEVNULL os.mkfifo(FIFO_PATH)
)
ltcdump = subprocess.Popen( def start_arecord():
["ltcdump", "-f", "-"], return subprocess.Popen([
stdin=arecord.stdout, "arecord", "-f", "S16_LE", "-c", "1", "-r", "48000", "-D", "hw:2,0", FIFO_PATH
stdout=subprocess.PIPE, ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
stderr=subprocess.DEVNULL,
text=True, def start_ltcdump():
bufsize=1 return subprocess.Popen([
) "ltcdump", "-f", FIFO_PATH
arecord.stdout.close() # Let ltcdump consume the pipe ], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, bufsize=1)
return arecord, ltcdump
def clean_up_processes(*procs):
for proc in procs:
if proc and proc.poll() is None:
proc.terminate()
try:
proc.wait(timeout=1)
except subprocess.TimeoutExpired:
proc.kill()
if os.path.exists(FIFO_PATH):
os.remove(FIFO_PATH)
def main(stdscr): def main(stdscr):
curses.curs_set(0) curses.curs_set(0)
stdscr.nodelay(True) stdscr.nodelay(True)
arecord_proc, ltcdump_proc = start_ltc_stream() setup_fifo()
arecord_proc = start_arecord()
ltcdump_proc = start_ltcdump()
latest_tc = "⌛ Waiting for LTC..." latest_tc = "⌛ Waiting for LTC..."
last_update = time.time() last_update = time.time()
@ -39,35 +53,32 @@ def main(stdscr):
rlist, _, _ = select.select([ltcdump_proc.stdout], [], [], 0) rlist, _, _ = select.select([ltcdump_proc.stdout], [], [], 0)
if rlist: if rlist:
line = ltcdump_proc.stdout.readline() line = ltcdump_proc.stdout.readline()
if line: if line and line[0].isdigit():
line = line.strip() latest_tc = line.strip()
if line and line[0].isdigit(): last_update = time.time()
latest_tc = line
last_update = time.time()
# Timeout / error detection # If stream stalls or breaks
if time.time() - last_update > 1: if time.time() - last_update > 1:
if ltcdump_proc.poll() is not None or arecord_proc.poll() is not None: if ltcdump_proc.poll() is not None or arecord_proc.poll() is not None:
latest_tc = "💥 Decoder crashed or stream stopped" latest_tc = "💥 Stream stopped or decoder crashed"
else: else:
latest_tc = "⚠️ No LTC signal" latest_tc = "⚠️ No LTC signal detected"
# Draw the curses UI # Draw interface
stdscr.erase() stdscr.erase()
stdscr.addstr(1, 2, "🌀 NTP Timeturner Status") stdscr.addstr(1, 2, "🌀 NTP Timeturner Status")
stdscr.addstr(3, 4, "Streaming LTC from hw:2,0...") stdscr.addstr(3, 4, f"Reading LTC from hw:2,0 via FIFO...")
stdscr.addstr(5, 6, f"🕰️ LTC Timecode: {latest_tc}") stdscr.addstr(5, 6, f"🕰️ LTC Timecode: {latest_tc}")
stdscr.refresh() stdscr.refresh()
time.sleep(0.04) # ~25 FPS time.sleep(0.04) # 25Hz refresh
except KeyboardInterrupt: except KeyboardInterrupt:
stdscr.addstr(8, 6, "🔚 Shutting down...") stdscr.addstr(8, 6, "🔚 Exiting gracefully...")
stdscr.refresh() stdscr.refresh()
time.sleep(1) time.sleep(1)
finally: finally:
arecord_proc.terminate() clean_up_processes(arecord_proc, ltcdump_proc)
ltcdump_proc.terminate()
if __name__ == "__main__": if __name__ == "__main__":
if not shutil.which("ltcdump") or not shutil.which("arecord"): if not shutil.which("ltcdump") or not shutil.which("arecord"):