mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
last attempt
This commit is contained in:
parent
5bd6a822dd
commit
1bf16999b7
1 changed files with 45 additions and 34 deletions
|
|
@ -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:
|
|
||||||
line = line.strip()
|
|
||||||
if line and line[0].isdigit():
|
if line and line[0].isdigit():
|
||||||
latest_tc = line
|
latest_tc = line.strip()
|
||||||
last_update = time.time()
|
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"):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue