feat(03-02): implement animation library — 7 types with registry and factory

- AnimationBase ABC with render(t, led_count) and from_params() contract
- SolidColorAnimation: time-independent uniform fill
- ChaseAnimation: moving lit segment with forward/reverse support
- PulseAnimation: sine-wave breathing effect (speed maps to period)
- RainbowAnimation: HSV hue cycle across strip via colorsys.hsv_to_rgb
- StrobeAnimation: on/off flashing at 1-20 Hz with configurable duty cycle
- ColorWipeAnimation: progressive fill from start or end
- FireAnimation: Fire2012 algorithm with cooling/sparking/heat-to-color mapping
- ANIMATION_REGISTRY + create_animation() factory function
- All 24 tests passing
This commit is contained in:
Claude
2026-04-06 21:47:39 +00:00
parent 920243fcae
commit f4da9564d4
10 changed files with 422 additions and 3 deletions

View File

@@ -0,0 +1,58 @@
"""Chase animation — a lit segment moves along the strip."""
from lightsync.animations.base import AnimationBase
class ChaseAnimation(AnimationBase):
"""Moving lit segment(s) chasing along the strip.
At speed=1.0, the segment moves at 60 pixels/second.
Pattern repeats every (size + spacing) pixels.
"""
def __init__(
self,
color: tuple[int, int, int] = (255, 255, 255),
bg_color: tuple[int, int, int] = (0, 0, 0),
speed: float = 0.5,
size: int = 3,
spacing: int = 7,
reverse: bool = False,
white: int = 0,
) -> None:
self.color = tuple(color) # type: ignore[arg-type]
self.bg_color = tuple(bg_color) # type: ignore[arg-type]
self.speed = speed
self.size = size
self.spacing = spacing
self.reverse = reverse
self.white = white
def render(self, t: float, led_count: int) -> list[tuple]:
period = self.size + self.spacing
offset = int(t * self.speed * 60) # pixels/sec at speed=1.0 is 60
pixels = []
for i in range(led_count):
if self.reverse:
pos = (i + offset) % period
else:
pos = (i - offset) % period
if pos < self.size:
pixels.append(self.color)
else:
pixels.append(self.bg_color)
return pixels
@classmethod
def from_params(cls, params: dict) -> "ChaseAnimation":
return cls(
color=tuple(params.get("color", [255, 255, 255])), # type: ignore[arg-type]
bg_color=tuple(params.get("bg_color", [0, 0, 0])), # type: ignore[arg-type]
speed=params.get("speed", 0.5),
size=params.get("size", 3),
spacing=params.get("spacing", 7),
reverse=params.get("reverse", False),
white=params.get("white", 0),
)