Skip to content

add ring support - #42 #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion adafruit_led_animation/animation/comet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
6 changes: 5 additions & 1 deletion adafruit_led_animation/animation/rainbowcomet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -74,13 +75,16 @@ 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]
Expand Down