Skip to content

Commit c9c7e14

Browse files
committed
Make comet simpler, faster, able to be longer than the number of pixels, and the length specified.
1 parent 37e9190 commit c9c7e14

File tree

2 files changed

+59
-62
lines changed

2 files changed

+59
-62
lines changed

adafruit_led_animation/animation/comet.py

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"""
4646

4747
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
4949

5050

5151
class Comet(Animation):
@@ -64,86 +64,64 @@ class Comet(Animation):
6464

6565
# pylint: disable=too-many-arguments
6666
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,
7575
):
7676
if tail_length == 0:
7777
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
8678
self.reverse = reverse
8779
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
8884
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()
9091
super().__init__(pixel_object, speed, color, name=name)
9192

9293
on_cycle_complete_supported = True
9394

9495
def _recompute_color(self, color):
95-
pass
96+
self._comet_recompute_color(color)
9697

9798
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))
106102
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)
140104

141105
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
143118

144119
def reset(self):
145120
"""
146121
Resets to the first color.
147122
"""
148-
self._generator = self._comet_generator()
149123
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

adafruit_led_animation/color.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,22 @@ def colorwheel(pos):
6464
return 0, int(255 - pos * 3), int(pos * 3)
6565
pos -= 170
6666
return int(pos * 3), 0, int(255 - (pos * 3))
67+
68+
69+
def calculate_intensity(color, intensity=1.0):
70+
"""
71+
Takes a RGB[W] color tuple and adjusts the intensity.
72+
:param float intensity:
73+
:param color: color value (tuple, list or int)
74+
:return: color
75+
"""
76+
if isinstance(color, int):
77+
return ((int((color & 0xff0000) * intensity) & 0xff0000) |
78+
(int((color & 0xff00) * intensity) & 0xff00) |
79+
(int((color & 0xff) * intensity) & 0xff))
80+
81+
if len(color) == 3:
82+
return int(color[0] * intensity), int(color[1] * intensity), int(color[2] * intensity)
83+
elif len(color) == 4 and isinstance(color[3], float):
84+
return int(color[0] * intensity), int(color[1] * intensity), int(color[2] * intensity), color[3]
85+
return int(color[0] * intensity), int(color[1] * intensity), int(color[2] * intensity), int(color[3] * intensity)

0 commit comments

Comments
 (0)