some stuff

This commit is contained in:
2026-03-21 15:47:50 +01:00
parent 90435b477a
commit 30ec5b6ae0
+36 -28
View File
@@ -11,6 +11,8 @@ from functools import wraps
from typing import Callable from typing import Callable
from dataclasses import dataclass from dataclasses import dataclass
STEREO_BLENDING = False
INITIAL_FREQ = 9500 INITIAL_FREQ = 9500
INITIAL_EQ = True INITIAL_EQ = True
INITIAL_IMS = 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 PORT = int(os.getenv("PORT") or 0) or 7373
DEVICE = os.getenv("DEV", "COM6") DEVICE = os.getenv("DEV", "COM6")
PASSWORD = os.getenv("PW", "test") DEFAULT_PW = "test"
if PASSWORD == "test": print("Password left at default. Please change if neccesary") 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() CLOCK_ENV = os.getenv("CLK", "dp").strip().lower()
if CLOCK_ENV == "dp": CLOCK = 12000000 # DP-666 if CLOCK_ENV == "dp": CLOCK = 12000000 # DP-666
@@ -28,8 +31,8 @@ elif CLOCK_ENV == "x": CLOCK = 9216000
else: CLOCK = int(CLOCK_ENV) else: CLOCK = int(CLOCK_ENV)
SALT_LENGTH = 16 SALT_LENGTH = 16
SS_UPDATE_INTERVAL = 0.11 SS_UPDATE_INTERVAL = 0.1
RDS_UPDATE_INTERVAL = 0.0856 RDS_UPDATE_INTERVAL = 0.086
@dataclass @dataclass
class State: class State:
@@ -59,7 +62,7 @@ def init_tef():
tef.FM_Set_Lowcut_Max(True, 60) tef.FM_Set_Lowcut_Max(True, 60)
tef.FM_Set_Stereo_Time(60, 120, 100, 200) 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 tef.APPL_Set_OperationMode(True) # Turn off
return tef return tef
@@ -69,17 +72,15 @@ def authenticate(conn: socket.socket):
expected_hash = hashlib.sha1((salt + PASSWORD).encode()).hexdigest().encode() expected_hash = hashlib.sha1((salt + PASSWORD).encode()).hexdigest().encode()
while True: while True:
try: data = conn.recv(len(expected_hash))
data = conn.recv(len(expected_hash)) if not data: break
if not data: break
if data.strip() == expected_hash.strip(): if data.strip() == expected_hash.strip():
conn.sendall(b"o1,0\nOK\n") conn.sendall(b"o1,0\nOK\n")
return True return True
else: else:
conn.sendall(b"a0\n") # You failed conn.sendall(b"a0\n") # You failed
break break
except BlockingIOError: pass
conn.close() conn.close()
return False 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 freq = int(cmd.decode().removeprefix("T").strip()) // 10
if freq < 6500 or freq > 10800: continue if freq < 6500 or freq > 10800: continue
tef.FM_Tune_To(TEF6686.TuneTo_Mode.FM_Jump, freq) tef.FM_Tune_To(TEF6686.TuneTo_Mode.FM_Jump, freq)
time.sleep(0.01)
state.last_tune = freq state.last_tune = freq
out += f"T{freq*10}\n".encode() out += f"T{freq*10}\n".encode()
elif cmd.startswith(b"G"): 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) dtime = 500 if state.deemp == 0 else (750 if state.deemp == 1 else 0)
tef.FM_Set_Deemphasis(dtime) tef.FM_Set_Deemphasis(dtime)
out += f"D{state.deemp}\n".encode() out += f"D{state.deemp}\n".encode()
elif cmd.startswith(b"x"): elif cmd.startswith(b"x"): tef.FM_Tune_To(TEF6686.TuneTo_Mode.Preset, state.last_tune)
tef.APPL_Set_OperationMode(False) # Enable
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"X"): tef.APPL_Set_OperationMode(True) # turn off
elif cmd.startswith(b"W"): elif cmd.startswith(b"W"):
state.bw = int(cmd.decode().removeprefix("W").strip()) state.bw = int(cmd.decode().removeprefix("W").strip())
@@ -172,8 +170,6 @@ def run_periodic(*args, **kwargs):
@periodic(SS_UPDATE_INTERVAL) @periodic(SS_UPDATE_INTERVAL)
def send_signal_status(tef: TEF6686, conn: socket.socket, state: State): 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() res = tef.FM_Get_Quality_Data()
if res is None: return if res is None: return
@@ -186,9 +182,14 @@ def send_signal_status(tef: TEF6686, conn: socket.socket, state: State):
data = b"S" data = b"S"
if state.forced_mono: data += b"M" if STEREO_BLENDING:
elif stereo_pilot: data += b"s" stereo_pilot, _ = d if (d := tef.FM_Get_Signal_Status()) else (None, None)
else: data += b"m" 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: if not state.last_bw_extend and level > 38 and usn < 7:
tef.FM_Set_Bandwidth_Options(400) tef.FM_Set_Bandwidth_Options(400)
@@ -260,10 +261,10 @@ def run_server():
with conn: with conn:
reset_periodic() reset_periodic()
print(f"Connected by {addr}") print(f"Connected by {addr}")
conn.setblocking(False)
if not authenticate(conn): if not authenticate(conn):
print("Authentication failed.") print("Authentication failed.")
continue continue
conn.setblocking(False)
# Send initial state # Send initial state
conn.sendall(f"T{state.last_tune*10}\n".encode()) conn.sendall(f"T{state.last_tune*10}\n".encode())
@@ -276,12 +277,19 @@ def run_server():
while True: while True:
try: try:
if not (data := conn.recv(1024)): break if not (data := conn.recv(1024)): break
resp = process_command(tef, data, state, conn) try:
if resp: conn.sendall(resp) 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 ConnectionResetError: break
except ConnectionAbortedError: break except ConnectionAbortedError: break
except BlockingIOError: 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) time.sleep(0.03)
import traceback import traceback