Add scanner

This commit is contained in:
2026-02-20 21:53:52 +01:00
parent 6517709ea7
commit eece17b9db
7 changed files with 325 additions and 25 deletions
+45 -8
View File
@@ -13,6 +13,7 @@ from protocol import I2CPCClient
HOST = '0.0.0.0'
PORT = 7373
PASSWORD = "test"
CLOCK = 12000000 # DP-666
SALT_LENGTH = 16
SS_UPDATE_INTERVAL = 0.125
@@ -25,15 +26,16 @@ def compute_hash(salt, password):
return hashlib.sha1((salt + password).encode()).hexdigest().encode()
def init_tef():
p = I2CPCClient("COM17")
p = I2CPCClient("COM6")
tef = TEF6686(p)
tef.init()
tef.init(clock=CLOCK)
tef.AUDIO_Set_Mute(False)
tef.AUDIO_Set_Volume(60)
tef.FM_Tune_To(1, 9500)
tef.FM_Set_RDS(1)
tef.FM_Set_ChannelEqualizer(True)
tef.FM_Set_MphSuppression(True)
tef.APPL_Set_OperationMode(True)
return tef
def authenticate(conn):
@@ -46,7 +48,7 @@ def authenticate(conn):
if not data: return False
if data.strip() == expected_hash.strip(): return True
def process_command(tef: TEF6686, data: bytes, state):
def process_command(tef: TEF6686, data: bytes, state: dict, conn: socket.socket):
out = b""
for cmd in data.splitlines():
if cmd.startswith(b"T"):
@@ -67,22 +69,52 @@ def process_command(tef: TEF6686, data: bytes, state):
out += f"B{int(mono)}\n".encode()
elif cmd.startswith(b"D"):
deemp = int(cmd.decode().removeprefix("D").strip())
time = 500 if deemp == 0 else (750 if deemp == 1 else 0)
dtime = 500 if deemp == 0 else (750 if deemp == 1 else 0)
state['deemp'] = deemp
tef.FM_Set_Deemphasis(time)
tef.FM_Set_Deemphasis(dtime)
out += f"D{deemp}\n".encode()
elif cmd.startswith(b"x"):
out += b"OK\n"
tef.APPL_Set_OperationMode(False)
elif cmd.startswith(b"X"):
tef.APPL_Set_OperationMode(True)
elif cmd.startswith(b"W"):
bw = int(cmd.decode().removeprefix("W").strip())
auto = (bw == 0)
tef.FM_Set_Bandwidth(auto, 2360 if auto else (bw // 100))
state['bw'] = bw
out += f"W{bw}\n".encode()
elif cmd.startswith(b"?"):
out += b"XRD Python driver\n"
elif cmd.startswith(b"S"):
cmd = cmd[1:]
if cmd != b"":
arg = int(cmd.decode()[1:].strip())
match cmd[0]:
case 97: state['scan_start'] = (arg + 5) // 10
case 98: state['scan_stop'] = (arg + 5) // 10
case 99: state['scan_step'] = (arg + 5) // 10
case 119: state['scan_bw'] = arg
else:
tef.FM_Set_Bandwidth((state["scan_bw"] == 0), state["scan_bw"])
conn.sendall(b"U")
for freq in range(state["scan_start"], state["scan_stop"] + state["scan_step"], state["scan_step"]):
tef.FM_Tune_To(2, freq)
time.sleep(0.0055)
conn.sendall(str(freq * 10).encode())
conn.sendall(b" = ")
_, level, *_ = tef.FM_Get_Quality_Data()
if level is None: continue
conn.sendall(str((level // 10) + 10).encode())
conn.sendall(b", ")
conn.sendall(b"\n")
tef.FM_Tune_To(1, state["last_tune"])
tef.FM_Set_Bandwidth((state["bw"] == 0), 2360 if (state["bw"] == 0) else (state["bw"] // 100))
return out
def send_signal_status(tef: TEF6686, conn, state):
def send_signal_status(tef: TEF6686, conn: socket.socket, state):
_, _, stereo, _ = tef.FM_Get_Processing_Status()
stereo = stereo if stereo is not None else 1000
@@ -132,7 +164,11 @@ def run_server():
'last_eqims': 0b11,
'forced_mono': False,
'deemp': 0,
'bw': 0
'bw': 0,
'scan_start': 87500,
'scan_stop': 108000,
'scan_step': 100,
'scan_bw': 0,
}
try:
@@ -164,9 +200,10 @@ def run_server():
try:
data = conn.recv(1024)
if not data: break
resp = process_command(tef, data, state)
resp = process_command(tef, data, state, conn)
if resp: conn.sendall(resp)
except ConnectionResetError: break
except ConnectionAbortedError: break
except BlockingIOError:
if ss_timer.get_time() > SS_UPDATE_INTERVAL:
send_signal_status(tef, conn, state)