improve performance

This commit is contained in:
2026-03-24 22:09:47 +01:00
parent 942b94498f
commit 37bcfd5951
+20 -28
View File
@@ -11,6 +11,7 @@ from functools import wraps
from typing import Callable
from dataclasses import dataclass
from enum import IntEnum
import select
class Modulation(IntEnum):
FM = 0
@@ -90,12 +91,6 @@ def init_tef():
tef.FM_Set_ChannelEqualizer(INITIAL_EQ)
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.APPL_Set_OperationMode(True) # Turn off
return tef
@@ -216,9 +211,8 @@ def reset_periodic():
def run_periodic(*args, **kwargs):
for (func, t, timer) in PERIODIC_FUNCTIONS:
max_catchup = 10
count = 0
while timer.get_time() > t and count < max_catchup:
while timer.get_time() > t and count < 5:
func(*args, **kwargs)
timer.subtract(t)
count += 1
@@ -321,7 +315,6 @@ def run_server():
if not authenticate(conn):
print("Authentication failed.")
continue
conn.setblocking(False)
# Send initial state
conn.sendall(f"T{state.last_tune_fm*10}\n".encode())
@@ -331,27 +324,26 @@ def run_server():
conn.sendall(f"W{state.bw_fm}\n".encode())
conn.sendall(f"M{tef.modulation}\n".encode())
while True:
while True:
readable, _, _ = select.select([conn], [], [], 0.03)
if readable:
data = conn.recv(256)
if not data: break
try:
if not (data := conn.recv(1024)): break
resp = process_command(tef, data, state, conn)
if resp: conn.sendall(resp)
except Exception as e:
try:
resp = process_command(tef, data, state, conn)
if resp: conn.sendall(resp)
except Exception as e:
try:
print(e)
conn.sendall(b"!" + str(e).encode() + b"\n")
except Exception: pass
except ConnectionResetError: break
except ConnectionAbortedError: break
except BlockingIOError:
try: run_periodic(tef, conn, state)
except Exception as e:
try:
print(e)
conn.sendall(b"!" + str(e).encode() + b"\n")
except Exception: pass
time.sleep(0.03)
print(e)
conn.sendall(b"!" + str(e).encode() + b"\n")
except Exception: pass
else:
try: run_periodic(tef, conn, state)
except Exception as e:
try:
print(e)
conn.sendall(b"!" + str(e).encode() + b"\n")
except Exception: pass
import traceback
if __name__ == "__main__":