mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
stashed some initial files, removed the redundant hermione modules
This commit is contained in:
parent
9fe70f4688
commit
e36a4bfd87
4 changed files with 126 additions and 117 deletions
10
config.json
Normal file
10
config.json
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"ltc_device": "/dev/audio",
|
||||
"offset": {
|
||||
"hours": 0,
|
||||
"minutes": 0,
|
||||
"seconds": 0,
|
||||
"milliseconds": 0
|
||||
},
|
||||
"frame_rate": 25
|
||||
}
|
||||
109
hermione.py
109
hermione.py
|
|
@ -1,109 +0,0 @@
|
|||
import json
|
||||
import subprocess
|
||||
import time
|
||||
import threading
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
CONFIG_FILE = "hermione_config.json"
|
||||
|
||||
# Load config or create default
|
||||
def load_config():
|
||||
if not os.path.exists(CONFIG_FILE):
|
||||
default_config = {
|
||||
"framerate": 25,
|
||||
"start_mode": "system",
|
||||
"manual_time": "12:00:00",
|
||||
"duration_seconds": 3600,
|
||||
"ltc_gen_path": "ltc-gen.exe",
|
||||
"autostart_timeout": 5
|
||||
}
|
||||
with open(CONFIG_FILE, "w") as f:
|
||||
json.dump(default_config, f, indent=4)
|
||||
return default_config
|
||||
else:
|
||||
with open(CONFIG_FILE, "r") as f:
|
||||
return json.load(f)
|
||||
|
||||
# Save updated config
|
||||
def save_config(config):
|
||||
with open(CONFIG_FILE, "w") as f:
|
||||
json.dump(config, f, indent=4)
|
||||
|
||||
# Prompt with timeout
|
||||
def prompt_with_timeout(prompt, timeout):
|
||||
print(prompt, end='', flush=True)
|
||||
input_data = []
|
||||
|
||||
def get_input():
|
||||
try:
|
||||
input_data.append(input())
|
||||
except EOFError:
|
||||
pass
|
||||
|
||||
thread = threading.Thread(target=get_input)
|
||||
thread.daemon = True
|
||||
thread.start()
|
||||
thread.join(timeout)
|
||||
return input_data[0] if input_data else ""
|
||||
|
||||
# Get timecode based on config
|
||||
def get_start_time(config):
|
||||
if config["start_mode"] == "system":
|
||||
now = datetime.now()
|
||||
return now.strftime("%H:%M:%S")
|
||||
else:
|
||||
return config["manual_time"]
|
||||
|
||||
# Run ltc-gen
|
||||
def run_ltc_gen(config):
|
||||
start_time = get_start_time(config)
|
||||
framerate = str(config["framerate"])
|
||||
duration = str(config["duration_seconds"])
|
||||
ltc_gen_path = config["ltc_gen_path"]
|
||||
|
||||
cmd = [
|
||||
ltc_gen_path,
|
||||
"-f", framerate,
|
||||
"-l", duration,
|
||||
"-t", start_time
|
||||
]
|
||||
|
||||
print(f"\n🎬 Running Hermione with:")
|
||||
print(f" Start Time: {start_time}")
|
||||
print(f" Framerate: {framerate} fps")
|
||||
print(f" Duration: {duration} seconds")
|
||||
print(f" Executable: {ltc_gen_path}\n")
|
||||
|
||||
try:
|
||||
subprocess.run(cmd)
|
||||
except FileNotFoundError:
|
||||
print(f"❌ Error: {ltc_gen_path} not found!")
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to run Hermione: {e}")
|
||||
|
||||
# Main logic
|
||||
def main():
|
||||
config = load_config()
|
||||
user_input = prompt_with_timeout(
|
||||
"\nPress [Enter] to run with config or type 'm' to modify (auto-starts in 5s): ",
|
||||
config.get("autostart_timeout", 5)
|
||||
)
|
||||
|
||||
if user_input.lower() == 'm':
|
||||
try:
|
||||
config["framerate"] = int(input("Enter framerate (e.g. 25): "))
|
||||
config["start_mode"] = input("Start from system time or manual? (system/manual): ").strip().lower()
|
||||
if config["start_mode"] == "manual":
|
||||
config["manual_time"] = input("Enter manual start time (HH:MM:SS): ")
|
||||
config["duration_seconds"] = int(input("Enter duration in seconds: "))
|
||||
config["ltc_gen_path"] = input("Enter path to ltc-gen.exe (or leave blank for default): ") or config["ltc_gen_path"]
|
||||
save_config(config)
|
||||
except Exception as e:
|
||||
print(f"⚠️ Error updating config: {e}")
|
||||
return
|
||||
|
||||
run_ltc_gen(config)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
{
|
||||
"framerate": 25,
|
||||
"start_mode": "system",
|
||||
"manual_time": "12:00:00",
|
||||
"duration_seconds": 3600,
|
||||
"ltc_gen_path": "ltc-gen.exe",
|
||||
"autostart_timeout": 5
|
||||
}
|
||||
116
timeturner.py
Normal file
116
timeturner.py
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
timeturner.py
|
||||
NTP Timeturner — LTC-to-NTP time server for Raspberry Pi
|
||||
Now with a colourful LTC status light 🌈🟢
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import logging
|
||||
import json
|
||||
import curses
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
CONFIG_PATH = "config.json"
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
"ltc_device": "/dev/audio",
|
||||
"offset": {
|
||||
"hours": 0,
|
||||
"minutes": 0,
|
||||
"seconds": 0,
|
||||
"milliseconds": 0
|
||||
},
|
||||
"frame_rate": 25
|
||||
}
|
||||
|
||||
TIMETURNER_SPINNER = ['⧗', '⧖', '⧗', '⧖', '🕰']
|
||||
HEARTBEAT_PULSE = ['●', '○']
|
||||
|
||||
def load_config(path=CONFIG_PATH):
|
||||
if not os.path.exists(path):
|
||||
logging.warning("Config file not found, using defaults.")
|
||||
return DEFAULT_CONFIG
|
||||
with open(path, "r") as f:
|
||||
return json.load(f)
|
||||
|
||||
def read_ltc_time():
|
||||
return datetime.utcnow()
|
||||
|
||||
def apply_offset(base_time, offset):
|
||||
delta = timedelta(
|
||||
hours=offset.get("hours", 0),
|
||||
minutes=offset.get("minutes", 0),
|
||||
seconds=offset.get("seconds", 0),
|
||||
milliseconds=offset.get("milliseconds", 0)
|
||||
)
|
||||
return base_time + delta
|
||||
|
||||
def set_system_time(new_time):
|
||||
formatted = new_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
logging.info(f"Setting system time to: {formatted}")
|
||||
# os.system(f"sudo timedatectl set-time \"{formatted}\"")
|
||||
|
||||
def draw_dashboard(stdscr, ltc_time, adjusted_time, config, frame):
|
||||
stdscr.clear()
|
||||
|
||||
# Setup strings
|
||||
offset = config["offset"]
|
||||
offset_str = f"{offset['hours']:02}:{offset['minutes']:02}:{offset['seconds']:02}.{offset['milliseconds']:03}"
|
||||
spinner = TIMETURNER_SPINNER[frame % len(TIMETURNER_SPINNER)]
|
||||
heartbeat = HEARTBEAT_PULSE[frame % len(HEARTBEAT_PULSE)]
|
||||
|
||||
# Hardcoded LTC status
|
||||
ltc_status = "LOCKED"
|
||||
ltc_colour = curses.color_pair(2) # Green
|
||||
|
||||
# Draw
|
||||
stdscr.addstr(0, 0, f"┌───────────────────────────────┐")
|
||||
stdscr.addstr(1, 0, f"│ {spinner} NTP Timeturner {spinner} │")
|
||||
stdscr.addstr(2, 0, f"├───────────────────────────────┤")
|
||||
stdscr.addstr(3, 0, f"│ LTC Time: {ltc_time.strftime('%H:%M:%S:%f')[:-3]} │")
|
||||
stdscr.addstr(4, 0, f"│ Offset Applied: +{offset_str:<17}│")
|
||||
stdscr.addstr(5, 0, f"│ System Time: {adjusted_time.strftime('%H:%M:%S')} │")
|
||||
stdscr.addstr(6, 0, f"│ Frame Rate: {config['frame_rate']} fps │")
|
||||
stdscr.addstr(7, 0, f"│ LTC Status: ")
|
||||
stdscr.addstr("● ", ltc_colour)
|
||||
stdscr.addstr(f"{ltc_status:<14}│")
|
||||
stdscr.addstr(8, 0, f"│ NTP Broadcast: PENDING │")
|
||||
stdscr.addstr(9, 0, f"├───────────────────────────────┤")
|
||||
stdscr.addstr(10, 0, f"│ System Status: {heartbeat} │")
|
||||
stdscr.addstr(11, 0, f"│ [Ctrl+C to exit] │")
|
||||
stdscr.addstr(12, 0, f"└───────────────────────────────┘")
|
||||
|
||||
stdscr.refresh()
|
||||
|
||||
def start_timeturner(stdscr):
|
||||
curses.curs_set(0)
|
||||
curses.start_color()
|
||||
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) # Not used yet
|
||||
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK) # LOCKED
|
||||
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) # UNSTABLE
|
||||
|
||||
stdscr.nodelay(True)
|
||||
stdscr.timeout(1000)
|
||||
|
||||
config = load_config()
|
||||
frame = 0
|
||||
|
||||
while True:
|
||||
try:
|
||||
ltc_time = read_ltc_time()
|
||||
adjusted_time = apply_offset(ltc_time, config["offset"])
|
||||
set_system_time(adjusted_time)
|
||||
draw_dashboard(stdscr, ltc_time, adjusted_time, config, frame)
|
||||
frame += 1
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format="[%(asctime)s] %(levelname)s: %(message)s")
|
||||
logging.info("✨ Timeturner console mode started.")
|
||||
curses.wrapper(start_timeturner)
|
||||
Loading…
Add table
Add a link
Reference in a new issue