mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
reverted back to working version
This commit is contained in:
parent
1bf16999b7
commit
026373dd77
1 changed files with 38 additions and 59 deletions
|
|
@ -1,88 +1,67 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os
|
|
||||||
import subprocess
|
|
||||||
import curses
|
import curses
|
||||||
import time
|
import subprocess
|
||||||
import shutil
|
import shutil
|
||||||
import select
|
import time
|
||||||
import signal
|
|
||||||
|
|
||||||
FIFO_PATH = "/tmp/ltcpipe"
|
def start_ltc_stream():
|
||||||
|
# Launch ffmpeg piped into ltcdump
|
||||||
def setup_fifo():
|
ffmpeg = subprocess.Popen(
|
||||||
if os.path.exists(FIFO_PATH):
|
["ffmpeg", "-f", "alsa", "-i", "default", "-ac", "1", "-ar", "48000", "-f", "s16le", "-"],
|
||||||
os.remove(FIFO_PATH)
|
stdout=subprocess.PIPE,
|
||||||
os.mkfifo(FIFO_PATH)
|
stderr=subprocess.DEVNULL
|
||||||
|
)
|
||||||
def start_arecord():
|
ltcdump = subprocess.Popen(
|
||||||
return subprocess.Popen([
|
["ltcdump", "-f", "-"],
|
||||||
"arecord", "-f", "S16_LE", "-c", "1", "-r", "48000", "-D", "hw:2,0", FIFO_PATH
|
stdin=ffmpeg.stdout,
|
||||||
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
def start_ltcdump():
|
text=True
|
||||||
return subprocess.Popen([
|
)
|
||||||
"ltcdump", "-f", FIFO_PATH
|
ffmpeg.stdout.close() # Let ltcdump consume the pipe
|
||||||
], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, bufsize=1)
|
return ffmpeg, 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)
|
||||||
|
|
||||||
setup_fifo()
|
stdscr.addstr(1, 2, "🌀 NTP Timeturner Status")
|
||||||
arecord_proc = start_arecord()
|
stdscr.addstr(3, 4, "Streaming LTC from default input...")
|
||||||
ltcdump_proc = start_ltcdump()
|
|
||||||
|
ffmpeg_proc, ltcdump_proc = start_ltc_stream()
|
||||||
|
|
||||||
latest_tc = "⌛ Waiting for LTC..."
|
latest_tc = "⌛ Waiting for LTC..."
|
||||||
last_update = time.time()
|
last_refresh = time.time()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
# Non-blocking read from ltcdump
|
stdscr.clear()
|
||||||
rlist, _, _ = select.select([ltcdump_proc.stdout], [], [], 0)
|
|
||||||
if rlist:
|
|
||||||
line = ltcdump_proc.stdout.readline()
|
|
||||||
if line and line[0].isdigit():
|
|
||||||
latest_tc = line.strip()
|
|
||||||
last_update = time.time()
|
|
||||||
|
|
||||||
# If stream stalls or breaks
|
|
||||||
if time.time() - last_update > 1:
|
|
||||||
if ltcdump_proc.poll() is not None or arecord_proc.poll() is not None:
|
|
||||||
latest_tc = "💥 Stream stopped or decoder crashed"
|
|
||||||
else:
|
|
||||||
latest_tc = "⚠️ No LTC signal detected"
|
|
||||||
|
|
||||||
# Draw interface
|
|
||||||
stdscr.erase()
|
|
||||||
stdscr.addstr(1, 2, "🌀 NTP Timeturner Status")
|
stdscr.addstr(1, 2, "🌀 NTP Timeturner Status")
|
||||||
stdscr.addstr(3, 4, f"Reading LTC from hw:2,0 via FIFO...")
|
stdscr.addstr(3, 4, "Streaming LTC from default input...")
|
||||||
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) # 25Hz refresh
|
# Check if new LTC line available
|
||||||
|
if ltcdump_proc.stdout.readable():
|
||||||
|
line = ltcdump_proc.stdout.readline().strip()
|
||||||
|
if line and line[0].isdigit():
|
||||||
|
latest_tc = line
|
||||||
|
|
||||||
|
# Limit screen redraw to ~10fps
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
stdscr.addstr(8, 6, "🔚 Exiting gracefully...")
|
stdscr.addstr(8, 6, "🔚 Shutting down...")
|
||||||
stdscr.refresh()
|
stdscr.refresh()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
finally:
|
finally:
|
||||||
clean_up_processes(arecord_proc, ltcdump_proc)
|
ffmpeg_proc.terminate()
|
||||||
|
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("ffmpeg"):
|
||||||
print("❌ Required tools not found (ltcdump or arecord). Install and retry.")
|
print("❌ Required tools not found (ltcdump or ffmpeg). Install and retry.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
curses.wrapper(main)
|
curses.wrapper(main)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue