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 ):
@@ -64,86 +64,64 @@ class Comet(Animation):
64
64
65
65
# pylint: disable=too-many-arguments
66
66
def __init__ (
67
- self ,
68
- pixel_object ,
69
- speed ,
70
- color ,
71
- tail_length = 0 ,
72
- reverse = False ,
73
- bounce = False ,
74
- name = None ,
67
+ self ,
68
+ pixel_object ,
69
+ speed ,
70
+ color ,
71
+ tail_length = 0 ,
72
+ reverse = False ,
73
+ bounce = False ,
74
+ name = None ,
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 - 1
88
+ self ._right_side = self ._num_pixels + self ._tail_length + 1
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 (calculate_intensity (color , n * self ._color_step + 0.05 ))
106
102
self ._computed_color = color
107
-
108
- def _get_range (self , num_pixels ):
109
- 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
135
- if self .bounce :
136
- self .reverse = not self .reverse
137
- if not self .bounce or cycle_passes == 2 :
138
- self .cycle_complete = True
139
- cycle_passes = 0
103
+ print (self ._comet_colors )
140
104
141
105
def draw (self ):
142
- next (self ._generator )
106
+ for pixel_no in range (self ._tail_length + 1 ):
107
+ draw_at = self ._tail_start + (self ._direction * pixel_no )
108
+ if draw_at < 0 or draw_at >= self ._num_pixels :
109
+ continue
110
+ self .pixel_object [draw_at ] = self ._comet_colors [pixel_no ]
111
+
112
+ self ._tail_start += self ._direction
113
+
114
+ if self ._tail_start < self ._left_side or self ._tail_start > self ._right_side :
115
+ self .reverse = not self .reverse
116
+ self ._direction = - self ._direction
117
+ self ._tail_start += self ._direction
143
118
144
119
def reset (self ):
145
120
"""
146
121
Resets to the first color.
147
122
"""
148
- self ._generator = self ._comet_generator ()
149
123
self .reverse = self ._initial_reverse
124
+ if self .reverse :
125
+ self ._tail_start = self ._num_pixels + self ._tail_length + 1
126
+ else :
127
+ self ._tail_start = - self ._tail_length - 1
0 commit comments