44
44
45
45
"""
46
46
47
- import random
48
- from adafruit_led_animation import NANOS_PER_SECOND , monotonic_ns
49
- from adafruit_led_animation .animation import Animation
47
+ from adafruit_led_animation .animation .sparkle import Sparkle
48
+ from adafruit_led_animation .helper import pulse_generator
50
49
51
50
52
- class SparklePulse (Animation ):
51
+ class SparklePulse (Sparkle ):
53
52
"""
54
- Combination of the Spark and Pulse animations.
53
+ Combination of the Sparkle and Pulse animations.
55
54
56
55
:param pixel_object: The initialised LED object.
57
56
:param int speed: Animation refresh rate in seconds, e.g. ``0.1``.
@@ -63,50 +62,30 @@ class SparklePulse(Animation):
63
62
64
63
# pylint: disable=too-many-arguments
65
64
def __init__ (
66
- self , pixel_object , speed , color , period = 5 , max_intensity = 1 , min_intensity = 0
65
+ self ,
66
+ pixel_object ,
67
+ speed ,
68
+ color ,
69
+ period = 5 ,
70
+ max_intensity = 1 ,
71
+ min_intensity = 0 ,
72
+ name = None ,
67
73
):
68
- if len (pixel_object ) < 2 :
69
- raise ValueError ("Sparkle needs at least 2 pixels" )
70
- self .max_intensity = max_intensity
71
- self .min_intensity = min_intensity
74
+ self ._max_intensity = max_intensity
75
+ self ._min_intensity = min_intensity
72
76
self ._period = period
73
- self ._intensity_delta = max_intensity - min_intensity
74
- self ._half_period = period / 2
75
- self ._position_factor = 1 / self ._half_period
76
- self ._bpp = len (pixel_object [0 ])
77
- # Handle dotstars
78
- if self ._bpp == 4 and isinstance (pixel_object [0 ][3 ], float ):
79
- self ._bpp = 3
80
- self ._last_update = monotonic_ns ()
81
- self ._cycle_position = 0
82
- self ._half_color = None
83
- self ._dim_color = None
84
- super ().__init__ (pixel_object , speed , color )
85
-
86
- def _recompute_color (self , color ):
87
- half_color = tuple (color [rgb ] // 4 for rgb in range (len (color )))
88
- dim_color = tuple (color [rgb ] // 10 for rgb in range (len (color )))
89
- for pixel in range (len (self .pixel_object )):
90
- if self .pixel_object [pixel ] == self ._half_color :
91
- self .pixel_object [pixel ] = half_color
92
- elif self .pixel_object [pixel ] == self ._dim_color :
93
- self .pixel_object [pixel ] = dim_color
94
- self ._half_color = half_color
95
- self ._dim_color = dim_color
77
+ white = len (pixel_object ) == 4 and isinstance (pixel_object [0 ][- 1 ], int )
78
+ dotstar = len (pixel_object ) == 4 and isinstance (pixel_object [0 ][- 1 ], float )
79
+ super ().__init__ (
80
+ pixel_object , speed = speed , color = color , num_sparkles = 1 , name = name
81
+ )
82
+ self ._generator = pulse_generator (
83
+ self ._period , self , white , dotstar_pwm = dotstar
84
+ )
96
85
97
86
def draw (self ):
98
- pixel = random .randint (0 , (len (self .pixel_object ) - 2 ))
99
-
100
- now = monotonic_ns ()
101
- time_since_last_draw = (now - self ._last_update ) / NANOS_PER_SECOND
102
- self ._last_update = now
103
- pos = self ._cycle_position = (
104
- self ._cycle_position + time_since_last_draw
105
- ) % self ._period
106
- if pos > self ._half_period :
107
- pos = self ._period - pos
108
- intensity = self .min_intensity + (
109
- pos * self ._intensity_delta * self ._position_factor
110
- )
111
- color = [int (self .color [n ] * intensity ) for n in range (self ._bpp )]
112
- self .pixel_object [pixel ] = color
87
+ self ._sparkle_color = next (self ._generator )
88
+ super ().draw ()
89
+
90
+ def after_draw (self ):
91
+ self .show ()
0 commit comments