From 26bff5bbcf260ad593eacff5eabc35d6532e666c Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 5 Oct 2026 09:43:58 +0200 Subject: [PATCH] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20"app"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/hardware_interface.py | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/hardware_interface.py diff --git a/app/hardware_interface.py b/app/hardware_interface.py new file mode 100644 index 0000000..67c5533 --- /dev/null +++ b/app/hardware_interface.py @@ -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)})