some changes

This commit is contained in:
2026-03-21 08:20:44 +01:00
parent 7b0f74a1e5
commit 42f428305b
2 changed files with 21 additions and 9 deletions
+4 -4
View File
@@ -3,7 +3,7 @@ from driver.base_tef import BaseTEF668X
from typing import overload, ParamSpec, TypeVar, Concatenate, Callable
from functools import wraps
import struct
from enum import Enum
from enum import IntEnum
P = ParamSpec("P")
T = TypeVar("T")
@@ -36,7 +36,7 @@ def pack(*values: int | SignedInt):
class TEF6686(BaseTEF668X):
def _pre_command_(self, wrap: Callable, data: bytes, read_bytes: int | None, out_parser: Callable | None): return data, read_bytes, out_parser
class TuneTo_Mode(Enum):
class TuneTo_Mode(IntEnum):
Preset = 1
Search = 2
FM_AF_Update = 3
@@ -44,9 +44,9 @@ class TEF6686(BaseTEF668X):
FM_Check = 5
End = 7
@_command_wrapper
def FM_Tune_To(self, mode: TuneTo_Mode, frequency: int | None): return b"\x20\x01\x01" + (pack(mode.value, frequency) if frequency is not None else pack(mode.value)), None, None
def FM_Tune_To(self, mode: TuneTo_Mode, frequency: int | None): return b"\x20\x01\x01" + (pack(mode, frequency) if frequency is not None else pack(mode)), None, None
@_command_wrapper
def AM_Tune_To(self, mode: TuneTo_Mode, frequency: int | None): return b"\x21\x01\x01" + (pack(mode.value, frequency) if frequency is not None else pack(mode.value)), None, None
def AM_Tune_To(self, mode: TuneTo_Mode, frequency: int | None): return b"\x21\x01\x01" + (pack(mode, frequency) if frequency is not None else pack(mode)), None, None
@_command_wrapper
def FM_Set_Tune_Options(self, afu_bw_mode: bool = False, afu_bandwidth: int = 2360, afu_mute_time: int = 1000, afu_sample_time: int = 2000):
+17 -5
View File
@@ -42,6 +42,7 @@ class State:
scan_stop: int = 10800
scan_step: int = 10
scan_bw: int = 0
last_bw_extend: bool = False
def init_tef():
tef = TEF6686(I2CPCClient(DEVICE, int(os.getenv("BAUD") or 0) or 115200))
@@ -91,8 +92,8 @@ def process_command(tef: TEF6686, data: bytes, state: State, conn: socket.socket
if cmd.startswith(b"T"):
freq = int(cmd.decode().removeprefix("T").strip()) // 10
if freq < 6500 or freq > 10800: continue
tef.FM_Tune_To(TEF6686.TuneTo_Mode.Preset, freq)
time.sleep(0.05)
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"):
@@ -180,7 +181,11 @@ def send_signal_status(tef: TEF6686, conn: socket.socket, state: State):
res = tef.FM_Get_Quality_Data()
if res is None: return
_, level, usn, wam, _, bandwidth, *_ = res
status, level, usn, wam, _, bandwidth, *_ = res
ms_since_tune = (status & 0x1ff) / 10
if ms_since_tune < 33: return # Give only "quality data"
level = level / 10
@@ -190,6 +195,13 @@ def send_signal_status(tef: TEF6686, conn: socket.socket, state: State):
elif stereo_pilot: data += b"s"
else: data += b"m"
if not state.last_bw_extend and level > 38 and usn < 7:
tef.FM_Set_Bandwidth_Options(400)
state.last_bw_extend = True
if state.last_bw_extend and (level < 33 or usn > 10):
tef.FM_Set_Bandwidth_Options()
state.last_bw_extend = False
conn.sendall(data + f"{level + 11.25},{wam//10},{usn//10},{bandwidth}\n\n".encode())
@periodic(RDS_UPDATE_INTERVAL)
@@ -224,6 +236,8 @@ def send_rds_data(tef: TEF6686, conn: socket.socket, state: State):
def run_server():
with init_tef() as tef:
state = State()
device, hw_version, sw_version = d if (d := tef.APPL_Get_Identification()) else (None, None, None)
if device and hw_version and sw_version:
variant = device & 127
@@ -240,8 +254,6 @@ def run_server():
print(f"{variant_str} (V{hw_major}{hw_minor}{sw_major}.{sw_minor})")
else: print("Could not fetch variant")
state = State()
with socket.socket() as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))