feat(01-01): create package structure, Pydantic models, and device abstractions
- pyproject.toml with FastAPI, uvicorn, Pydantic v2, aiofiles, structlog deps - DeviceConfig Pydantic model with StripType, UUID, validation constraints - ShowModel with AudioRef, TrackModel, CueModel, AnalysisBlock, schema_version=1 - BaseDevice ABC with encode_frame, encode_animation_cmd, bytes_per_pixel - SK6812Device (4 bytes/pixel) and WS2801Device (3 bytes/pixel) implementations
This commit is contained in:
0
lightsync/devices/__init__.py
Normal file
0
lightsync/devices/__init__.py
Normal file
33
lightsync/devices/base.py
Normal file
33
lightsync/devices/base.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from lightsync.models.device import DeviceConfig
|
||||
|
||||
|
||||
class BaseDevice(ABC):
|
||||
def __init__(self, config: DeviceConfig):
|
||||
self.config = config
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self.config.id
|
||||
|
||||
@property
|
||||
def led_count(self) -> int:
|
||||
return self.config.led_count
|
||||
|
||||
@abstractmethod
|
||||
def encode_frame(self, pixels: list[tuple]) -> bytes:
|
||||
"""Encode a full pixel frame to UDP payload bytes.
|
||||
pixels: list of (R, G, B) or (R, G, B, W) tuples.
|
||||
"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def encode_animation_cmd(self, animation: str, params: dict) -> bytes:
|
||||
"""Encode an animation+params command packet."""
|
||||
...
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def bytes_per_pixel(self) -> int:
|
||||
"""Number of bytes per LED in frame mode (3 for RGB, 4 for RGBW)."""
|
||||
...
|
||||
28
lightsync/devices/sk6812.py
Normal file
28
lightsync/devices/sk6812.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from lightsync.devices.base import BaseDevice
|
||||
from lightsync.models.device import DeviceConfig
|
||||
|
||||
|
||||
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:
|
||||
"""Stub — Phase 3 implements real animation command encoding."""
|
||||
return b""
|
||||
27
lightsync/devices/ws2801.py
Normal file
27
lightsync/devices/ws2801.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from lightsync.devices.base import BaseDevice
|
||||
from lightsync.models.device import DeviceConfig
|
||||
|
||||
|
||||
class WS2801Device(BaseDevice):
|
||||
"""WS2801 RGB LED strip — 3 bytes per pixel (R, G, B)."""
|
||||
|
||||
def __init__(self, config: DeviceConfig):
|
||||
super().__init__(config)
|
||||
|
||||
@property
|
||||
def bytes_per_pixel(self) -> int:
|
||||
return 3
|
||||
|
||||
def encode_frame(self, pixels: list[tuple]) -> bytes:
|
||||
"""Encode pixel list to WS2801 frame bytes.
|
||||
Each pixel: (r, g, b) tuple — 3 bytes per LED.
|
||||
"""
|
||||
buf = bytearray()
|
||||
for pixel in pixels:
|
||||
r, g, b = pixel[0], pixel[1], pixel[2]
|
||||
buf.extend([r & 0xFF, g & 0xFF, b & 0xFF])
|
||||
return bytes(buf)
|
||||
|
||||
def encode_animation_cmd(self, animation: str, params: dict) -> bytes:
|
||||
"""Stub — Phase 3 implements real animation command encoding."""
|
||||
return b""
|
||||
Reference in New Issue
Block a user