plate-config-app/app/widgets/sections/led_test_section.py

132 lines
4.9 KiB
Python

"""Встраиваемая секция «Тест цвета» — цветовое чередование диодов.
Позволяет задать порядок цветов (RGB/RBG/…) командой 89
и запустить тест: красный → зелёный → синий.
"""
from __future__ import annotations
from PySide6.QtCore import QTimer
from PySide6.QtWidgets import (
QComboBox,
QGroupBox,
QHBoxLayout,
QLabel,
QMessageBox,
QPushButton,
QVBoxLayout,
QWidget,
)
from ...services.com_service import ComConnection
from ...ui.icons import icon
_COLOR_ORDERS: list[tuple[int, str]] = [
(0, "RGB"),
(1, "RBG"),
(2, "BRG"),
(3, "BGR"),
(4, "GRB"),
(5, "GBR"),
]
class LedTestSection(QWidget):
"""Inline-секция тест цвета (для базового режима)."""
def __init__(self, connection: ComConnection, parent: QWidget | None = None) -> None:
super().__init__(parent)
self._conn = connection
self._setup_ui()
# ── UI ────────────────────────────────────────────────────────────
def _setup_ui(self) -> None:
root = QVBoxLayout(self)
root.setContentsMargins(0, 0, 0, 0)
root.setSpacing(10)
# ── Тест цвета ──
color_group = QGroupBox("Тест цвета (cmd 89)")
color_root = QVBoxLayout(color_group)
color_row1 = QHBoxLayout()
self._color_order_combo = QComboBox()
for code, name in _COLOR_ORDERS:
self._color_order_combo.addItem(f"{code} — {name}", code)
color_row1.addWidget(self._color_order_combo)
self._set_color_btn = QPushButton("Применить")
self._set_color_btn.setProperty("role", "primary")
self._set_color_btn.setIcon(icon("fa5s.palette", color="#ecfdf5"))
self._set_color_btn.clicked.connect(self._send_color_order)
color_row1.addWidget(self._set_color_btn)
color_root.addLayout(color_row1)
color_row2 = QHBoxLayout()
self._color_test_btn = QPushButton("Тест цвета")
self._color_test_btn.setProperty("role", "primary")
self._color_test_btn.setIcon(icon("fa5s.eye", color="#ecfdf5"))
self._color_test_btn.clicked.connect(self._run_color_test)
color_row2.addWidget(self._color_test_btn)
color_hint = QLabel("Должны вывестись: красный → зелёный → синий")
color_hint.setStyleSheet("color: #9ca3af; font-size: 12px;")
color_row2.addWidget(color_hint)
color_row2.addStretch()
color_root.addLayout(color_row2)
root.addWidget(color_group)
# ── Лог ──
self._log = QLabel("")
self._log.setObjectName("LogLabel")
self._log.setWordWrap(True)
root.addWidget(self._log)
# ── Public ────────────────────────────────────────────────────────
def refresh_icons(self) -> None:
"""Нет theme-dependent иконок — ничего не делаем."""
# ── Отправка ──────────────────────────────────────────────────────
def _send_color_order(self) -> None:
code = self._color_order_combo.currentData()
self._send(f"8900{code}")
def _run_color_test(self) -> None:
self._color_test_cmds = ["8003", "8004", "8005", "8002", "8000"]
self._color_test_idx = 0
self._color_test_btn.setEnabled(False)
self._color_test_timer = QTimer(self)
self._color_test_timer.setInterval(300)
self._color_test_timer.timeout.connect(self._color_test_step)
self._color_test_step()
self._color_test_timer.start()
def _color_test_step(self) -> None:
if self._color_test_idx >= len(self._color_test_cmds):
self._color_test_timer.stop()
self._color_test_btn.setEnabled(True)
return
cmd = self._color_test_cmds[self._color_test_idx]
self._color_test_idx += 1
self._send(cmd)
def _send(self, cmd: str) -> None:
if not cmd:
return
if not self._conn.is_connected:
QMessageBox.warning(self, "Ошибка", "Порт не подключён.")
return
try:
self._conn.send_command(cmd)
self._log.setText(f"→ {cmd}")
resp = self._conn.read_response(timeout=0.3)
self._conn.release()
if resp:
self._log.setText(f"→ {cmd}\n← {resp}")
except Exception as exc:
self._conn.release()
self._log.setText(f"Ошибка: {exc}")