- UDPSender: single shared asyncio DatagramTransport, start/stop lifecycle, fire-and-forget send() - _NullProtocol: swallows OS send errors (UDP delivery is best-effort for LEDs) - SK6812Device.encode_animation_cmd: replaces b'' stub with real 16-byte 0xAC packet - WS2801Device.encode_animation_cmd: replaces b'' stub with real 16-byte 0xAC packet - Both devices delegate to encode_animation_cmd from lightsync.protocol.animation_cmd
30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
from lightsync.devices.base import BaseDevice
|
|
from lightsync.models.device import DeviceConfig
|
|
from lightsync.protocol.animation_cmd import encode_animation_cmd as _encode_cmd
|
|
|
|
|
|
class SK6812Device(BaseDevice):
|
|
"""SK6812 RGBW LED strip — 4 bytes per pixel (R, G, B, W)."""
|
|
|
|
def __init__(self, config: DeviceConfig):
|
|
super().__init__(config)
|
|
|
|
@property
|
|
def bytes_per_pixel(self) -> int:
|
|
return 4
|
|
|
|
def encode_frame(self, pixels: list[tuple]) -> bytes:
|
|
"""Encode pixel list to SK6812 frame bytes.
|
|
Each pixel: (r, g, b, w) tuple — 4 bytes per LED.
|
|
"""
|
|
buf = bytearray()
|
|
for pixel in pixels:
|
|
r, g, b = pixel[0], pixel[1], pixel[2]
|
|
w = pixel[3] if len(pixel) > 3 else 0
|
|
buf.extend([r & 0xFF, g & 0xFF, b & 0xFF, w & 0xFF])
|
|
return bytes(buf)
|
|
|
|
def encode_animation_cmd(self, animation: str, params: dict) -> bytes:
|
|
"""Encode animation command as 16-byte 0xAC LightSync packet."""
|
|
return _encode_cmd(animation, params)
|