From 30ec5b6ae07bfbcc97af97ff77ebde4cacdffc8b Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Sat, 21 Mar 2026 15:47:50 +0100 Subject: [PATCH] some stuff --- xrd.py | 64 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/xrd.py b/xrd.py index 7fd0510..d022fb5 100644 --- a/xrd.py +++ b/xrd.py @@ -11,6 +11,8 @@ from functools import wraps from typing import Callable from dataclasses import dataclass +STEREO_BLENDING = False + INITIAL_FREQ = 9500 INITIAL_EQ = True INITIAL_IMS = True @@ -19,8 +21,9 @@ HOST = os.getenv("HOST") or '0.0.0.0' PORT = int(os.getenv("PORT") or 0) or 7373 DEVICE = os.getenv("DEV", "COM6") -PASSWORD = os.getenv("PW", "test") -if PASSWORD == "test": print("Password left at default. Please change if neccesary") +DEFAULT_PW = "test" +PASSWORD = os.getenv("PW", DEFAULT_PW) +if PASSWORD == DEFAULT_PW: print("Password left at default. Please change if neccesary") CLOCK_ENV = os.getenv("CLK", "dp").strip().lower() if CLOCK_ENV == "dp": CLOCK = 12000000 # DP-666 @@ -28,8 +31,8 @@ elif CLOCK_ENV == "x": CLOCK = 9216000 else: CLOCK = int(CLOCK_ENV) SALT_LENGTH = 16 -SS_UPDATE_INTERVAL = 0.11 -RDS_UPDATE_INTERVAL = 0.0856 +SS_UPDATE_INTERVAL = 0.1 +RDS_UPDATE_INTERVAL = 0.086 @dataclass class State: @@ -59,7 +62,7 @@ def init_tef(): tef.FM_Set_Lowcut_Max(True, 60) tef.FM_Set_Stereo_Time(60, 120, 100, 200) - tef.FM_Set_Stereo_Max(False) # Disables stereo blend??!!??!? + tef.FM_Set_Stereo_Max(STEREO_BLENDING) tef.APPL_Set_OperationMode(True) # Turn off return tef @@ -69,17 +72,15 @@ def authenticate(conn: socket.socket): expected_hash = hashlib.sha1((salt + PASSWORD).encode()).hexdigest().encode() while True: - try: - data = conn.recv(len(expected_hash)) - if not data: break + data = conn.recv(len(expected_hash)) + if not data: break - if data.strip() == expected_hash.strip(): - conn.sendall(b"o1,0\nOK\n") - return True - else: - conn.sendall(b"a0\n") # You failed - break - except BlockingIOError: pass + if data.strip() == expected_hash.strip(): + conn.sendall(b"o1,0\nOK\n") + return True + else: + conn.sendall(b"a0\n") # You failed + break conn.close() return False @@ -90,7 +91,6 @@ def process_command(tef: TEF6686, data: bytes, state: State, conn: socket.socket freq = int(cmd.decode().removeprefix("T").strip()) // 10 if freq < 6500 or freq > 10800: continue tef.FM_Tune_To(TEF6686.TuneTo_Mode.FM_Jump, freq) - time.sleep(0.01) state.last_tune = freq out += f"T{freq*10}\n".encode() elif cmd.startswith(b"G"): @@ -108,9 +108,7 @@ def process_command(tef: TEF6686, data: bytes, state: State, conn: socket.socket dtime = 500 if state.deemp == 0 else (750 if state.deemp == 1 else 0) tef.FM_Set_Deemphasis(dtime) out += f"D{state.deemp}\n".encode() - elif cmd.startswith(b"x"): - tef.APPL_Set_OperationMode(False) # Enable - tef.FM_Tune_To(TEF6686.TuneTo_Mode.Preset, state.last_tune) + elif cmd.startswith(b"x"): tef.FM_Tune_To(TEF6686.TuneTo_Mode.Preset, state.last_tune) elif cmd.startswith(b"X"): tef.APPL_Set_OperationMode(True) # turn off elif cmd.startswith(b"W"): state.bw = int(cmd.decode().removeprefix("W").strip()) @@ -172,8 +170,6 @@ def run_periodic(*args, **kwargs): @periodic(SS_UPDATE_INTERVAL) def send_signal_status(tef: TEF6686, conn: socket.socket, state: State): - stereo_pilot, _ = d if (d := tef.FM_Get_Signal_Status()) else (None, None) - res = tef.FM_Get_Quality_Data() if res is None: return @@ -186,9 +182,14 @@ def send_signal_status(tef: TEF6686, conn: socket.socket, state: State): data = b"S" - if state.forced_mono: data += b"M" - elif stereo_pilot: data += b"s" - else: data += b"m" + if STEREO_BLENDING: + stereo_pilot, _ = d if (d := tef.FM_Get_Signal_Status()) else (None, None) + if state.forced_mono: data += b"M" + elif stereo_pilot: data += b"s" + else: data += b"m" + else: + if state.forced_mono: data += b"M" + else: data += b"S" if not state.last_bw_extend and level > 38 and usn < 7: tef.FM_Set_Bandwidth_Options(400) @@ -260,10 +261,10 @@ def run_server(): with conn: reset_periodic() print(f"Connected by {addr}") - conn.setblocking(False) if not authenticate(conn): print("Authentication failed.") continue + conn.setblocking(False) # Send initial state conn.sendall(f"T{state.last_tune*10}\n".encode()) @@ -276,12 +277,19 @@ def run_server(): while True: try: if not (data := conn.recv(1024)): break - resp = process_command(tef, data, state, conn) - if resp: conn.sendall(resp) + try: + resp = process_command(tef, data, state, conn) + if resp: conn.sendall(resp) + except Exception as e: + try: conn.sendall(b"!" + str(e).encode() + b"\n") + except Exception: pass except ConnectionResetError: break except ConnectionAbortedError: break except BlockingIOError: - run_periodic(tef, conn, state) + try: run_periodic(tef, conn, state) + except Exception as e: + try: conn.sendall(b"!" + str(e).encode() + b"\n") + except Exception: pass time.sleep(0.03) import traceback