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:
42
lightsync/animations/strobe.py
Normal file
42
lightsync/animations/strobe.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""Strobe animation — rapid on/off flashing."""
|
||||
from lightsync.animations.base import AnimationBase
|
||||
|
||||
|
||||
class StrobeAnimation(AnimationBase):
|
||||
"""Rapid on/off flashing at configurable frequency and duty cycle.
|
||||
|
||||
speed=0.5 -> ~10.5 Hz. duty_cycle=0.1 means 10% on, 90% off.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
color: tuple[int, int, int] = (255, 255, 255),
|
||||
speed: float = 0.5,
|
||||
duty_cycle: float = 0.1,
|
||||
white: int = 0,
|
||||
) -> None:
|
||||
self.color = tuple(color) # type: ignore[arg-type]
|
||||
self.speed = speed
|
||||
self.duty_cycle = duty_cycle
|
||||
self.white = white
|
||||
|
||||
def render(self, t: float, led_count: int) -> list[tuple]:
|
||||
# Frequency: 1-20 Hz mapped from speed 0.0-1.0
|
||||
freq = 1.0 + self.speed * 19.0
|
||||
phase = (t * freq) % 1.0
|
||||
|
||||
if phase < self.duty_cycle:
|
||||
pixel = self.color
|
||||
else:
|
||||
pixel = (0, 0, 0)
|
||||
|
||||
return [pixel] * led_count
|
||||
|
||||
@classmethod
|
||||
def from_params(cls, params: dict) -> "StrobeAnimation":
|
||||
return cls(
|
||||
color=tuple(params.get("color", [255, 255, 255])), # type: ignore[arg-type]
|
||||
speed=params.get("speed", 0.5),
|
||||
duty_cycle=params.get("duty_cycle", 0.1),
|
||||
white=params.get("white", 0),
|
||||
)
|
||||
Reference in New Issue
Block a user