45
45
"""
46
46
47
47
from adafruit_led_animation .animation import Animation
48
- from adafruit_led_animation .color import BLACK
48
+ from adafruit_led_animation .color import BLACK , calculate_intensity
49
49
50
50
51
51
class Comet (Animation ):
@@ -62,7 +62,7 @@ class Comet(Animation):
62
62
:param bool bounce: Comet will bounce back and forth. Defaults to ``True``.
63
63
"""
64
64
65
- # pylint: disable=too-many-arguments
65
+ # pylint: disable=too-many-arguments,too-many-instance-attributes
66
66
def __init__ (
67
67
self ,
68
68
pixel_object ,
@@ -75,75 +75,59 @@ def __init__(
75
75
):
76
76
if tail_length == 0 :
77
77
tail_length = len (pixel_object ) // 4
78
- else :
79
- tail_length = max (2 , min (tail_length , len (pixel_object )))
80
- self ._tail_length = tail_length
81
- self ._color_step = 0.9 / tail_length
82
- self ._color_offset = 0.1
83
- self ._comet_colors = None
84
- self ._reverse_comet_colors = None
85
- self ._initial_reverse = reverse
86
78
self .reverse = reverse
87
79
self .bounce = bounce
80
+ self ._initial_reverse = reverse
81
+ self ._tail_length = tail_length
82
+ self ._color_step = 0.95 / tail_length
83
+ self ._comet_colors = None
88
84
self ._computed_color = color
89
- self ._generator = self ._comet_generator ()
85
+ self ._num_pixels = len (pixel_object )
86
+ self ._direction = - 1 if reverse else 1
87
+ self ._left_side = - self ._tail_length
88
+ self ._right_side = self ._num_pixels
89
+ self ._tail_start = 0
90
+ self .reset ()
90
91
super ().__init__ (pixel_object , speed , color , name = name )
91
92
92
93
on_cycle_complete_supported = True
93
94
94
95
def _recompute_color (self , color ):
95
- pass
96
+ self . _comet_recompute_color ( color )
96
97
97
98
def _comet_recompute_color (self , color ):
98
- self ._comet_colors = [BLACK ] + [
99
- [
100
- int (color [rgb ] * ((n * self ._color_step ) + self ._color_offset ))
101
- for rgb in range (len (color ))
102
- ]
103
- for n in range (self ._tail_length - 1 )
104
- ]
105
- self ._reverse_comet_colors = list (reversed (self ._comet_colors ))
99
+ self ._comet_colors = [BLACK ]
100
+ for n in range (self ._tail_length ):
101
+ self ._comet_colors .append (
102
+ calculate_intensity (color , n * self ._color_step + 0.05 )
103
+ )
106
104
self ._computed_color = color
107
105
108
- def _get_range (self , num_pixels ):
106
+ def draw (self ):
107
+ colors = self ._comet_colors
109
108
if self .reverse :
110
- return range (num_pixels , - self ._tail_length - 1 , - 1 )
111
- return range (- self ._tail_length , num_pixels + 1 )
112
-
113
- def _comet_generator (self ):
114
- num_pixels = len (self .pixel_object )
115
- cycle_passes = 0
116
- while True :
117
- if self ._color != self ._computed_color or not self ._comet_colors :
118
- self ._comet_recompute_color (self ._color )
119
- colors = self ._reverse_comet_colors if self .reverse else self ._comet_colors
120
- for start in self ._get_range (num_pixels ):
121
-
122
- if start + self ._tail_length < num_pixels :
123
- end = self ._tail_length
124
- else :
125
- end = num_pixels - start
126
- if start <= 0 :
127
- num_visible = self ._tail_length + start
128
- self .pixel_object [0 :num_visible ] = colors [
129
- self ._tail_length - num_visible :
130
- ]
131
- else :
132
- self .pixel_object [start : start + end ] = colors [0 :end ]
133
- yield
134
- cycle_passes += 1
109
+ colors = reversed (colors )
110
+ for pixel_no , color in enumerate (colors ):
111
+ draw_at = self ._tail_start + pixel_no
112
+ if draw_at < 0 or draw_at >= self ._num_pixels :
113
+ continue
114
+ self .pixel_object [draw_at ] = color
115
+
116
+ self ._tail_start += self ._direction
117
+
118
+ if self ._tail_start < self ._left_side or self ._tail_start >= self ._right_side :
135
119
if self .bounce :
136
120
self .reverse = not self .reverse
137
- if not self .bounce or cycle_passes == 2 :
121
+ self ._direction = - self ._direction
122
+ if self .reverse == self ._initial_reverse :
138
123
self .cycle_complete = True
139
- cycle_passes = 0
140
-
141
- def draw (self ):
142
- next (self ._generator )
143
124
144
125
def reset (self ):
145
126
"""
146
127
Resets to the first color.
147
128
"""
148
- self ._generator = self ._comet_generator ()
149
129
self .reverse = self ._initial_reverse
130
+ if self .reverse :
131
+ self ._tail_start = self ._num_pixels + self ._tail_length + 1
132
+ else :
133
+ self ._tail_start = - self ._tail_length - 1
0 commit comments