From 914bb8060b7f2ae91fe22adcd73d9258f69e0ce1 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 10 Nov 2022 14:53:25 -0600 Subject: [PATCH 1/2] speed bar generation by not querying pixels In a test this improved speed substantially, nearly doubling the speed of the following test program ```python pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False, pixel_order="RGB") evens = helper.PixelMap(pixels, [(i,) for i in range(0, pixel_num, 2)], individual_pixels=True) animation = RainbowChase(evens, 0, spacing=8) t0 = adafruit_ticks.ticks_ms() while True: for i in range(10): animation.animate(show=False) t1 = adafruit_ticks.ticks_ms() print(f"{10000/(t1-t0):.0f}fps") t0 = t1 ``` Performance on Raspberry Pi Pico W: Before: ~85fps After: ~140fps This also happens to make it compatible with an in-process PR that adds a fast PixelMap-like class to the core, but which doesn't support getitem. --- adafruit_led_animation/animation/chase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_led_animation/animation/chase.py b/adafruit_led_animation/animation/chase.py index f5168d7..a46a16a 100644 --- a/adafruit_led_animation/animation/chase.py +++ b/adafruit_led_animation/animation/chase.py @@ -96,7 +96,7 @@ def bar_colors(): bar_no += 1 colorgen = bar_colors() - self.pixel_object[:] = [next(colorgen) for _ in self.pixel_object] + self.pixel_object[:] = [next(colorgen) for _ in range(len(self.pixel_object))] if self.draw_count % len(self.pixel_object) == 0: self.cycle_complete = True From 73d5f61c6e059a289e7ff548fb90197559683f86 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 10 Nov 2022 15:50:26 -0600 Subject: [PATCH 2/2] Optimize comet draw The big pay-off is avoiding enumerate(). Removing redundant comparisons of _ring() and avoiding modulo operations help too. --- adafruit_led_animation/animation/comet.py | 26 ++++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/adafruit_led_animation/animation/comet.py b/adafruit_led_animation/animation/comet.py index 7252c98..e562e49 100644 --- a/adafruit_led_animation/animation/comet.py +++ b/adafruit_led_animation/animation/comet.py @@ -120,14 +120,24 @@ def draw(self): colors = self._comet_colors if self.reverse: colors = reversed(colors) - for pixel_no, color in enumerate(colors): - draw_at = self._tail_start + pixel_no - if draw_at < 0 or draw_at >= self._num_pixels: - if not self._ring: - continue - draw_at = draw_at % self._num_pixels - - self.pixel_object[draw_at] = color + + pixels = self.pixel_object + start = self._tail_start + npixels = len(pixels) + if self._ring: + start %= npixels + for color in colors: + pixels[start] = color + start += 1 + if start == npixels: + start = 0 + else: + for color in colors: + if start >= npixels: + break + if start >= 0: + pixels[start] = color + start += 1 self._tail_start += self._direction