Files
led2/lightsync/animations/pulse.py
Claude f4da9564d4 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
2026-04-06 21:47:39 +00:00

54 lines
1.8 KiB
Python

"""Pulse (breathing) animation — brightness oscillates via sine wave."""
import math
from lightsync.animations.base import AnimationBase
class PulseAnimation(AnimationBase):
"""Smooth breathing effect — brightness oscillates via sine wave.
speed=1.0 -> 0.1s period (fast), speed=0.0 -> 5.0s period (slow).
"""
def __init__(
self,
color: tuple[int, int, int] = (255, 255, 255),
speed: float = 0.5,
min_brightness: int = 0,
max_brightness: int = 255,
white: int = 0,
) -> None:
self.color = tuple(color) # type: ignore[arg-type]
self.speed = speed
self.min_brightness = min_brightness
self.max_brightness = max_brightness
self.white = white
def render(self, t: float, led_count: int) -> list[tuple]:
# Map speed to period: speed=1.0 -> 0.1s, speed=0.0 -> 5.0s
period = 0.1 + (1.0 - self.speed) * 4.9
# Sine oscillation: 0.0 to 1.0 over one period
brightness = (
self.min_brightness
+ (self.max_brightness - self.min_brightness)
* (0.5 + 0.5 * math.sin(2 * math.pi * t / period))
)
scale = brightness / 255.0
r = min(255, max(0, int(self.color[0] * scale)))
g = min(255, max(0, int(self.color[1] * scale)))
b = min(255, max(0, int(self.color[2] * scale)))
pixel = (r, g, b)
return [pixel] * led_count
@classmethod
def from_params(cls, params: dict) -> "PulseAnimation":
return cls(
color=tuple(params.get("color", [255, 255, 255])), # type: ignore[arg-type]
speed=params.get("speed", 0.5),
min_brightness=params.get("min_brightness", 0),
max_brightness=params.get("max_brightness", 255),
white=params.get("white", 0),
)