Skip to content

Commit 563f608

Browse files
authored
Merge pull request #103 from adafruit/optimizations
Optimizations
2 parents 16179ea + 73d5f61 commit 563f608

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

adafruit_led_animation/animation/chase.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def bar_colors():
9696
bar_no += 1
9797

9898
colorgen = bar_colors()
99-
self.pixel_object[:] = [next(colorgen) for _ in self.pixel_object]
99+
self.pixel_object[:] = [next(colorgen) for _ in range(len(self.pixel_object))]
100100

101101
if self.draw_count % len(self.pixel_object) == 0:
102102
self.cycle_complete = True

adafruit_led_animation/animation/comet.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,24 @@ def draw(self):
120120
colors = self._comet_colors
121121
if self.reverse:
122122
colors = reversed(colors)
123-
for pixel_no, color in enumerate(colors):
124-
draw_at = self._tail_start + pixel_no
125-
if draw_at < 0 or draw_at >= self._num_pixels:
126-
if not self._ring:
127-
continue
128-
draw_at = draw_at % self._num_pixels
129-
130-
self.pixel_object[draw_at] = color
123+
124+
pixels = self.pixel_object
125+
start = self._tail_start
126+
npixels = len(pixels)
127+
if self._ring:
128+
start %= npixels
129+
for color in colors:
130+
pixels[start] = color
131+
start += 1
132+
if start == npixels:
133+
start = 0
134+
else:
135+
for color in colors:
136+
if start >= npixels:
137+
break
138+
if start >= 0:
139+
pixels[start] = color
140+
start += 1
131141

132142
self._tail_start += self._direction
133143

0 commit comments

Comments
 (0)