From 2af26b7a669145a2260e0a452c35513b4ebb39f2 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Fri, 10 Jul 2020 17:42:21 -0400 Subject: [PATCH 1/2] add ring support - #42 --- adafruit_led_animation/animation/comet.py | 17 ++++++++++++++++- .../animation/rainbowcomet.py | 4 +++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/adafruit_led_animation/animation/comet.py b/adafruit_led_animation/animation/comet.py index 85e36d2..5df7b5f 100644 --- a/adafruit_led_animation/animation/comet.py +++ b/adafruit_led_animation/animation/comet.py @@ -60,6 +60,7 @@ class Comet(Animation): maximum of the length of the ``pixel_object``. :param bool reverse: Animates the comet in the reverse order. Defaults to ``False``. :param bool bounce: Comet will bounce back and forth. Defaults to ``True``. + :param bool ring: Ring mode. Defaults to ``False``. """ # pylint: disable=too-many-arguments,too-many-instance-attributes @@ -72,9 +73,12 @@ def __init__( reverse=False, bounce=False, name=None, + ring=False, ): if tail_length == 0: tail_length = len(pixel_object) // 4 + if bounce and ring: + raise ValueError('Cannot combine bounce and ring mode') self.reverse = reverse self.bounce = bounce self._initial_reverse = reverse @@ -87,6 +91,9 @@ def __init__( self._left_side = -self._tail_length self._right_side = self._num_pixels self._tail_start = 0 + self._ring = ring + if ring: + self._left_side = 0 self.reset() super().__init__(pixel_object, speed, color, name=name) @@ -107,7 +114,10 @@ def draw(self): for pixel_no, color in enumerate(colors): draw_at = self._tail_start + pixel_no if draw_at < 0 or draw_at >= self._num_pixels: - continue + if not self._ring: + continue + draw_at = draw_at % self._num_pixels + self.pixel_object[draw_at] = color self._tail_start += self._direction @@ -116,6 +126,8 @@ def draw(self): if self.bounce: self.reverse = not self.reverse self._direction = -self._direction + elif self._ring: + self._tail_start = self._tail_start % self._num_pixels else: self.reset() if self.reverse == self._initial_reverse and self.draw_count > 0: @@ -130,3 +142,6 @@ def reset(self): self._tail_start = self._num_pixels + self._tail_length + 1 else: self._tail_start = -self._tail_length - 1 + + if self._ring: + self._tail_start = self._tail_start % self._num_pixels diff --git a/adafruit_led_animation/animation/rainbowcomet.py b/adafruit_led_animation/animation/rainbowcomet.py index 3cc84bf..6be25c2 100644 --- a/adafruit_led_animation/animation/rainbowcomet.py +++ b/adafruit_led_animation/animation/rainbowcomet.py @@ -61,6 +61,7 @@ class RainbowComet(Comet): :param bool bounce: Comet will bounce back and forth. Defaults to ``True``. :param int colorwheel_offset: Offset from start of colorwheel (0-255). :param int step: Colorwheel step (defaults to automatic). + :param bool ring: Ring mode. Defaults to ``False``. """ # pylint: disable=too-many-arguments @@ -74,13 +75,14 @@ def __init__( colorwheel_offset=0, step=0, name=None, + ring=False, ): if step == 0: self._colorwheel_step = int(256 / tail_length) else: self._colorwheel_step = step self._colorwheel_offset = colorwheel_offset - super().__init__(pixel_object, speed, 0, tail_length, reverse, bounce, name) + super().__init__(pixel_object, speed, 0, tail_length, reverse, bounce, name, ring) def _set_color(self, color): self._comet_colors = [BLACK] From 65376c68e496cbe183a781ffda0723f8fd2f1e70 Mon Sep 17 00:00:00 2001 From: Roy Hooper Date: Fri, 10 Jul 2020 17:45:26 -0400 Subject: [PATCH 2/2] blackness --- adafruit_led_animation/animation/comet.py | 2 +- adafruit_led_animation/animation/rainbowcomet.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/adafruit_led_animation/animation/comet.py b/adafruit_led_animation/animation/comet.py index 5df7b5f..7e77c9d 100644 --- a/adafruit_led_animation/animation/comet.py +++ b/adafruit_led_animation/animation/comet.py @@ -78,7 +78,7 @@ def __init__( if tail_length == 0: tail_length = len(pixel_object) // 4 if bounce and ring: - raise ValueError('Cannot combine bounce and ring mode') + raise ValueError("Cannot combine bounce and ring mode") self.reverse = reverse self.bounce = bounce self._initial_reverse = reverse diff --git a/adafruit_led_animation/animation/rainbowcomet.py b/adafruit_led_animation/animation/rainbowcomet.py index 6be25c2..6fa22f4 100644 --- a/adafruit_led_animation/animation/rainbowcomet.py +++ b/adafruit_led_animation/animation/rainbowcomet.py @@ -82,7 +82,9 @@ def __init__( else: self._colorwheel_step = step self._colorwheel_offset = colorwheel_offset - super().__init__(pixel_object, speed, 0, tail_length, reverse, bounce, name, ring) + super().__init__( + pixel_object, speed, 0, tail_length, reverse, bounce, name, ring + ) def _set_color(self, color): self._comet_colors = [BLACK]