improve performance

This commit is contained in:
2026-03-24 22:09:47 +01:00
parent 942b94498f
commit 37bcfd5951
+7 -15
View File
@@ -11,6 +11,7 @@ from functools import wraps
from typing import Callable from typing import Callable
from dataclasses import dataclass from dataclasses import dataclass
from enum import IntEnum from enum import IntEnum
import select
class Modulation(IntEnum): class Modulation(IntEnum):
FM = 0 FM = 0
@@ -90,12 +91,6 @@ def init_tef():
tef.FM_Set_ChannelEqualizer(INITIAL_EQ) tef.FM_Set_ChannelEqualizer(INITIAL_EQ)
tef.FM_Set_MphSuppression(INITIAL_IMS) tef.FM_Set_MphSuppression(INITIAL_IMS)
tef.FM_Set_LevelStep(-1, -1, -1, -1, -4, -8, 0)
tef.FM_Set_Highcut_Mph(0)
tef.FM_Set_Highcut_Max(False)
tef.FM_Set_Lowcut_Max(True, 60)
tef.FM_Set_Stereo_Time(60, 120, 100, 200)
tef.FM_Set_Stereo_Max(STEREO_BLENDING) 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
@@ -216,9 +211,8 @@ def reset_periodic():
def run_periodic(*args, **kwargs): def run_periodic(*args, **kwargs):
for (func, t, timer) in PERIODIC_FUNCTIONS: for (func, t, timer) in PERIODIC_FUNCTIONS:
max_catchup = 10
count = 0 count = 0
while timer.get_time() > t and count < max_catchup: while timer.get_time() > t and count < 5:
func(*args, **kwargs) func(*args, **kwargs)
timer.subtract(t) timer.subtract(t)
count += 1 count += 1
@@ -321,7 +315,6 @@ def run_server():
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_fm*10}\n".encode()) conn.sendall(f"T{state.last_tune_fm*10}\n".encode())
@@ -332,8 +325,10 @@ def run_server():
conn.sendall(f"M{tef.modulation}\n".encode()) conn.sendall(f"M{tef.modulation}\n".encode())
while True: while True:
try: readable, _, _ = select.select([conn], [], [], 0.03)
if not (data := conn.recv(1024)): break if readable:
data = conn.recv(256)
if not data: break
try: try:
resp = process_command(tef, data, state, conn) resp = process_command(tef, data, state, conn)
if resp: conn.sendall(resp) if resp: conn.sendall(resp)
@@ -342,16 +337,13 @@ def run_server():
print(e) print(e)
conn.sendall(b"!" + str(e).encode() + b"\n") conn.sendall(b"!" + str(e).encode() + b"\n")
except Exception: pass except Exception: pass
except ConnectionResetError: break else:
except ConnectionAbortedError: break
except BlockingIOError:
try: run_periodic(tef, conn, state) try: run_periodic(tef, conn, state)
except Exception as e: except Exception as e:
try: try:
print(e) print(e)
conn.sendall(b"!" + str(e).encode() + b"\n") conn.sendall(b"!" + str(e).encode() + b"\n")
except Exception: pass except Exception: pass
time.sleep(0.03)
import traceback import traceback
if __name__ == "__main__": if __name__ == "__main__":