Files
Streamer/modules/led_manager.py
T

99 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
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.
"""
LED Manager - Obsługa asynchronicznego oddychania turkusowego
Każda z 8 diod oddycha niezależnie z randomowym offsetem
"""
import time
import threading
import json
import logging
log = logging.getLogger(__name__)
class LEDManager:
def __init__(self, uart_manager=None):
"""
Inicjalizuj LED manager
uart_manager - opcjonalnie, jeśli masz dostęp do UART/USB
"""
self.uart = uart_manager
self.running = False
self.thread = None
def start(self):
"""Uruchom pętlę LED"""
if self.running:
return
self.running = True
self.thread = threading.Thread(target=self._loop, daemon=True)
self.thread.start()
log.info("✅ LED manager uruchomiony")
def stop(self):
"""Zatrzymaj pętlę LED"""
self.running = False
if self.thread:
self.thread.join(timeout=1)
def _loop(self):
"""
GŁÓWNA PĘTLA LED
Oddychanie turkusowe - każda dioda asynchronicznie
"""
while self.running:
now = time.time() * 1000 # milliseconds
# Tablica jasności dla 8 diod
brightness = [0] * 8
for led_idx in range(8):
# RANDOMOWY OFFSET dla każdej diody
# 875ms delay między diodami = 8 diod × ~1s świecenia
offset = (led_idx * 875)
# Pełny cykl: 7 sekund
cycle_time = (now + offset) % 7000
# FAZY ODDYCHANIA:
if cycle_time < 2000:
# FAZA 1: 0-2s PAUSE (ciemno)
brightness[led_idx] = 0
elif cycle_time < 4000:
# FAZA 2: 2-4s NARASTANIE (0% → 20%)
progress = (cycle_time - 2000) / 2000.0
brightness[led_idx] = int(20 * progress)
elif cycle_time < 5000:
# FAZA 3: 4-5s ŚWIECENIE (20% jasności)
brightness[led_idx] = 20
else:
# FAZA 4: 5-7s WYGASZANIE (20% → 0%)
progress = (7000 - cycle_time) / 2000.0
brightness[led_idx] = int(20 * progress)
# Wyślij do firmware
self._send_led_command(brightness)
# 50ms refresh (20 Hz)
time.sleep(0.05)
def _send_led_command(self, brightness_values):
"""Wyślij komendy LED do firmware"""
if not self.uart or not self.uart._fp:
return
try:
# Format: {"cmd":"leds","brightness":[b0,b1,b2,b3,b4,b5,b6,b7]}
# Gdzie każde b = 0-20 (procent jasności)
cmd = {
"cmd": "leds",
"brightness": brightness_values
}
self.uart._fp._send(cmd)
except Exception as e:
log.debug(f"LED send error: {e}")