Prześlij pliki do "app"
This commit is contained in:
1 parent
5ced728354
commit
26bff5bbcf
1 file changed
+48
@@ -0,0 +1,48 @@
|
|||||||
|
# app/hardware_interface.py
|
||||||
|
import time
|
||||||
|
from collections import deque
|
||||||
|
import threading
|
||||||
|
|
||||||
|
class HardwareInterface:
|
||||||
|
DISPLAY_UPDATE_INTERVAL = 0.5 # sekundy; zmień w razie potrzeby
|
||||||
|
|
||||||
|
def __init__(self, port: str = "/dev/ttyAMA0", baudrate: int = 115200, timeout: float = 0.5):
|
||||||
|
...
|
||||||
|
self._last_display_time = 0.0
|
||||||
|
self._msg_queue = deque()
|
||||||
|
self._sender_thread = threading.Thread(target=self._send_worker, daemon=True)
|
||||||
|
self._sender_thread.start()
|
||||||
|
|
||||||
|
# -------------------------------------------------
|
||||||
|
# Wątek, który po kolei odczytuje kolejkę i wysyła
|
||||||
|
# -------------------------------------------------
|
||||||
|
def _send_worker(self):
|
||||||
|
while True:
|
||||||
|
if not self._msg_queue:
|
||||||
|
time.sleep(0.01)
|
||||||
|
continue
|
||||||
|
payload = self._msg_queue.popleft()
|
||||||
|
self._send(payload)
|
||||||
|
|
||||||
|
# -------------------------------------------------
|
||||||
|
# Publiczne API – dodaje wiadomość do kolejki
|
||||||
|
# -------------------------------------------------
|
||||||
|
def _queue_msg(self, payload: dict):
|
||||||
|
self._msg_queue.append(payload)
|
||||||
|
|
||||||
|
def set_display(self, text: str):
|
||||||
|
"""Wyświetla tekst, ale nie częściej niż DISPLAY_UPDATE_INTERVAL."""
|
||||||
|
now = time.time()
|
||||||
|
if now - self._last_display_time < self._display_update_interval:
|
||||||
|
# Zbyt szybko – ignorujemy (można też nadpisać poprzedni rekord)
|
||||||
|
return
|
||||||
|
self._last_display_time = now
|
||||||
|
self._queue_msg({"cmd": "display", "text": text[:32]})
|
||||||
|
|
||||||
|
def set_led(self, state: bool):
|
||||||
|
self._queue_msg({"cmd": "led", "state": int(state)})
|
||||||
|
|
||||||
|
def update_vu(self, left: float, right: float):
|
||||||
|
"""Wysyła VU co najczęściej, ale nie częściej niż 100 ms (wartość domyślna)."""
|
||||||
|
# Nie robimy throttlingu – VU jest już ograniczone w audio_engine (100 ms)
|
||||||
|
self._queue_msg({"cmd": "vu", "left": int(left), "right": int(right)})
|
||||||
Reference in new issue
Block a user