Skip to content

Commit 5d99d5f

Browse files
authored
Merge pull request #60 from rhooper/ring_support
add ring support - #42
2 parents dcc4326 + 65376c6 commit 5d99d5f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

adafruit_led_animation/animation/comet.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Comet(Animation):
6060
maximum of the length of the ``pixel_object``.
6161
:param bool reverse: Animates the comet in the reverse order. Defaults to ``False``.
6262
:param bool bounce: Comet will bounce back and forth. Defaults to ``True``.
63+
:param bool ring: Ring mode. Defaults to ``False``.
6364
"""
6465

6566
# pylint: disable=too-many-arguments,too-many-instance-attributes
@@ -72,9 +73,12 @@ def __init__(
7273
reverse=False,
7374
bounce=False,
7475
name=None,
76+
ring=False,
7577
):
7678
if tail_length == 0:
7779
tail_length = len(pixel_object) // 4
80+
if bounce and ring:
81+
raise ValueError("Cannot combine bounce and ring mode")
7882
self.reverse = reverse
7983
self.bounce = bounce
8084
self._initial_reverse = reverse
@@ -87,6 +91,9 @@ def __init__(
8791
self._left_side = -self._tail_length
8892
self._right_side = self._num_pixels
8993
self._tail_start = 0
94+
self._ring = ring
95+
if ring:
96+
self._left_side = 0
9097
self.reset()
9198
super().__init__(pixel_object, speed, color, name=name)
9299

@@ -107,7 +114,10 @@ def draw(self):
107114
for pixel_no, color in enumerate(colors):
108115
draw_at = self._tail_start + pixel_no
109116
if draw_at < 0 or draw_at >= self._num_pixels:
110-
continue
117+
if not self._ring:
118+
continue
119+
draw_at = draw_at % self._num_pixels
120+
111121
self.pixel_object[draw_at] = color
112122

113123
self._tail_start += self._direction
@@ -116,6 +126,8 @@ def draw(self):
116126
if self.bounce:
117127
self.reverse = not self.reverse
118128
self._direction = -self._direction
129+
elif self._ring:
130+
self._tail_start = self._tail_start % self._num_pixels
119131
else:
120132
self.reset()
121133
if self.reverse == self._initial_reverse and self.draw_count > 0:
@@ -130,3 +142,6 @@ def reset(self):
130142
self._tail_start = self._num_pixels + self._tail_length + 1
131143
else:
132144
self._tail_start = -self._tail_length - 1
145+
146+
if self._ring:
147+
self._tail_start = self._tail_start % self._num_pixels

adafruit_led_animation/animation/rainbowcomet.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class RainbowComet(Comet):
6161
:param bool bounce: Comet will bounce back and forth. Defaults to ``True``.
6262
:param int colorwheel_offset: Offset from start of colorwheel (0-255).
6363
:param int step: Colorwheel step (defaults to automatic).
64+
:param bool ring: Ring mode. Defaults to ``False``.
6465
"""
6566

6667
# pylint: disable=too-many-arguments
@@ -74,13 +75,16 @@ def __init__(
7475
colorwheel_offset=0,
7576
step=0,
7677
name=None,
78+
ring=False,
7779
):
7880
if step == 0:
7981
self._colorwheel_step = int(256 / tail_length)
8082
else:
8183
self._colorwheel_step = step
8284
self._colorwheel_offset = colorwheel_offset
83-
super().__init__(pixel_object, speed, 0, tail_length, reverse, bounce, name)
85+
super().__init__(
86+
pixel_object, speed, 0, tail_length, reverse, bounce, name, ring
87+
)
8488

8589
def _set_color(self, color):
8690
self._comet_colors = [BLACK]

0 commit comments

Comments
 (0)