effects.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spezialeffekt-Animationen (CONTINUOUS = True).
  4. Enthaelt visuelle Effekte wie Regenbogen, Welle und Funkeln.
  5. """
  6. from __future__ import annotations
  7. import colorsys
  8. import math
  9. import random
  10. from collections.abc import Iterator
  11. from trixy_core.led.animations import IAnimation, RGB
  12. from trixy_core.led.layouts import LEDLayout
  13. # Schwarz / aus
  14. OFF: RGB = (0, 0, 0)
  15. # Regenbogen: Schritte pro voller Rotation
  16. _RAINBOW_STEPS = 60
  17. # Wellen-Breite (Anzahl LEDs die gleichzeitig leuchten, Gauss-Kern)
  18. _WAVE_WIDTH = 3
  19. # Sparkle: Anzahl zufaellig leuchtender LEDs pro Frame
  20. _SPARKLE_MIN = 1
  21. _SPARKLE_MAX = 2
  22. class RainbowAnimation(IAnimation):
  23. """Regenbogen-Rotation ueber alle LEDs.
  24. Jede LED erhaelt einen anderen Farbton (Hue) aus dem vollen
  25. Spektrum. Der Regenbogen rotiert kontinuierlich. Der ``color``
  26. Parameter wird ignoriert (volles Spektrum).
  27. Bei Jewel: Center-LED leuchtet gedimmt weiss.
  28. """
  29. NAME = "rainbow"
  30. CONTINUOUS = True
  31. def frames(
  32. self,
  33. led_count: int,
  34. color: RGB,
  35. speed: int,
  36. layout: LEDLayout,
  37. **opts: object,
  38. ) -> Iterator[list[RGB]]:
  39. ring = layout.ring_indices
  40. ring_len = len(ring)
  41. while True:
  42. for offset in range(_RAINBOW_STEPS):
  43. frame = [OFF] * led_count
  44. # Jede Ring-LED bekommt eigenen Farbton
  45. for i, ring_idx in enumerate(ring):
  46. # Hue gleichmaessig verteilt + Offset fuer Rotation
  47. hue = (i / ring_len + offset / _RAINBOW_STEPS) % 1.0
  48. frame[ring_idx] = _hsv_to_rgb(hue, 1.0, 1.0)
  49. # Jewel-Center: gedimmt weiss
  50. if layout.center_index is not None:
  51. frame[layout.center_index] = (64, 64, 64)
  52. yield frame
  53. class WaveAnimation(IAnimation):
  54. """Helligkeitswelle wandert hin und her (Knight Rider).
  55. Eine 3 LEDs breite Welle mit Gauss-Abfall bewegt sich
  56. ueber den Ring hin und zurueck.
  57. """
  58. NAME = "wave"
  59. CONTINUOUS = True
  60. def frames(
  61. self,
  62. led_count: int,
  63. color: RGB,
  64. speed: int,
  65. layout: LEDLayout,
  66. **opts: object,
  67. ) -> Iterator[list[RGB]]:
  68. center_color: RGB | None = opts.get("center_color") # type: ignore[assignment]
  69. ring = layout.ring_indices
  70. ring_len = len(ring)
  71. if ring_len <= 1:
  72. # Fallback: einfaches Pulsieren fuer Single-LED
  73. yield from _single_fallback_pulse(led_count, color)
  74. return
  75. # Gauss-Standardabweichung fuer die Wellenbreite
  76. sigma = _WAVE_WIDTH / 2.0
  77. while True:
  78. # Hinweg: Position 0 → ring_len-1
  79. for pos in range(ring_len):
  80. yield _wave_frame(
  81. led_count, color, ring, ring_len, pos, sigma,
  82. layout, center_color,
  83. )
  84. # Rueckweg: Position ring_len-2 → 1 (Endpunkte nicht doppeln)
  85. for pos in range(ring_len - 2, 0, -1):
  86. yield _wave_frame(
  87. led_count, color, ring, ring_len, pos, sigma,
  88. layout, center_color,
  89. )
  90. class SparkleAnimation(IAnimation):
  91. """Zufaelliges Funkeln einzelner LEDs.
  92. Pro Frame leuchten 1-2 zufaellige LEDs in der angegebenen Farbe
  93. auf, der Rest bleibt dunkel. Erzeugt einen Sternen-/Funkel-Effekt.
  94. """
  95. NAME = "sparkle"
  96. CONTINUOUS = True
  97. def frames(
  98. self,
  99. led_count: int,
  100. color: RGB,
  101. speed: int,
  102. layout: LEDLayout,
  103. **opts: object,
  104. ) -> Iterator[list[RGB]]:
  105. center_color: RGB | None = opts.get("center_color") # type: ignore[assignment]
  106. ring = layout.ring_indices
  107. # Dunkler Hintergrund (leichtes Glimmen)
  108. dim: RGB = _scale_color(color, 0.05)
  109. while True:
  110. frame = [dim] * led_count
  111. # Zufaellige Ring-LEDs zum Funkeln auswaehlen
  112. sparkle_count = random.randint(_SPARKLE_MIN, min(_SPARKLE_MAX, len(ring)))
  113. sparkle_indices = random.sample(ring, sparkle_count)
  114. for idx in sparkle_indices:
  115. frame[idx] = color
  116. # Jewel-Center: konstant bei center_color oder dunkel
  117. if layout.center_index is not None:
  118. if center_color is not None:
  119. frame[layout.center_index] = center_color
  120. else:
  121. frame[layout.center_index] = dim
  122. yield frame
  123. # ── Hilfsfunktionen ──────────────────────────────────────────────────────
  124. def _hsv_to_rgb(h: float, s: float, v: float) -> RGB:
  125. """Konvertiert HSV-Farbe (0.0-1.0) nach RGB (0-255).
  126. Args:
  127. h: Farbton (Hue) 0.0 - 1.0.
  128. s: Saettigung 0.0 - 1.0.
  129. v: Helligkeit (Value) 0.0 - 1.0.
  130. Returns:
  131. RGB-Tupel mit Werten 0-255.
  132. """
  133. r, g, b = colorsys.hsv_to_rgb(h, s, v)
  134. return (int(r * 255), int(g * 255), int(b * 255))
  135. def _scale_color(color: RGB, factor: float) -> RGB:
  136. """Skaliert eine RGB-Farbe um einen Faktor (0.0 - 1.0).
  137. Args:
  138. color: Ausgangsfarbe als (R, G, B).
  139. factor: Helligkeitsfaktor.
  140. Returns:
  141. Skalierte RGB-Farbe.
  142. """
  143. return (
  144. min(255, max(0, int(color[0] * factor))),
  145. min(255, max(0, int(color[1] * factor))),
  146. min(255, max(0, int(color[2] * factor))),
  147. )
  148. def _wave_frame(
  149. led_count: int,
  150. color: RGB,
  151. ring: list[int],
  152. ring_len: int,
  153. pos: int,
  154. sigma: float,
  155. layout: LEDLayout,
  156. center_color: RGB | None,
  157. ) -> list[RGB]:
  158. """Erzeugt einen einzelnen Wellen-Frame.
  159. Berechnet fuer jede Ring-LED die Helligkeit anhand einer
  160. Gauss-Verteilung um die aktuelle Position.
  161. Args:
  162. led_count: Gesamtzahl LEDs.
  163. color: Wellenfarbe.
  164. ring: Ring-Indizes.
  165. ring_len: Anzahl Ring-LEDs.
  166. pos: Aktuelle Wellenposition (Index in ring).
  167. sigma: Gauss-Standardabweichung.
  168. layout: LED-Layout.
  169. center_color: Optionale Center-Farbe.
  170. Returns:
  171. Frame als Liste von RGB-Tupeln.
  172. """
  173. frame: list[RGB] = [OFF] * led_count
  174. for i in range(ring_len):
  175. # Gauss-Abfall: Helligkeit nimmt mit Abstand vom Zentrum ab
  176. distance = abs(i - pos)
  177. brightness = math.exp(-(distance ** 2) / (2 * sigma ** 2))
  178. if brightness > 0.01:
  179. frame[ring[i]] = _scale_color(color, brightness)
  180. # Jewel-Center
  181. if layout.center_index is not None and center_color is not None:
  182. frame[layout.center_index] = center_color
  183. return frame
  184. def _single_fallback_pulse(
  185. led_count: int,
  186. color: RGB,
  187. ) -> Iterator[list[RGB]]:
  188. """Fallback fuer Single-LED: einfaches Pulsieren.
  189. Args:
  190. led_count: Anzahl LEDs.
  191. color: Pulsfarbe.
  192. Yields:
  193. Frames mit variierender Helligkeit.
  194. """
  195. steps = 30
  196. while True:
  197. for step in range(steps):
  198. # Dreieckswelle
  199. half = steps // 2
  200. if step < half:
  201. brightness = step / half
  202. else:
  203. brightness = (steps - step) / half
  204. yield [_scale_color(color, brightness)] * led_count
  205. def get_animations() -> list[type[IAnimation]]:
  206. """Gibt alle Spezialeffekt-Animationsklassen zurueck."""
  207. return [
  208. RainbowAnimation,
  209. WaveAnimation,
  210. SparkleAnimation,
  211. ]