Files
Streamer/app/hardware_interface.py
T
2026-10-05 09:43:58 +02:00

49 lines
2.0 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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)})