Skip to content

Commit 2db4899

Browse files
committed
add chase
1 parent 6f522ac commit 2db4899

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

adafruit_led_animation/animation.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def __init__(self, pixel_object, speed, color, period=5, max_intensity=1, min_in
357357
self.max_intensity = max_intensity
358358
self.min_intensity = min_intensity
359359
self._direction = 1.0
360+
# TODO Fix this:
360361
self._intensity_step = 2 / (period / speed)
361362
self._bpp = len(pixel_object[0])
362363
super(Pulse, self).__init__(pixel_object, speed, color)
@@ -374,6 +375,47 @@ def draw(self):
374375
self.show()
375376

376377

378+
class Chase(Animation):
379+
"""
380+
Chase pixels in one direction single color.
381+
382+
:param pixel_object: The initialised LED object.
383+
:param int speed: Animation speed rate in seconds, e.g. ``0.1``.
384+
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
385+
:param size: Number of pixels per chase group.
386+
:param reverse: Reverse direction.
387+
"""
388+
389+
# pylint: disable=too-many-arguments
390+
def __init__(self, pixel_object, speed, color, size=3, reverse=False):
391+
self._size = size * 2
392+
self._direction = 1
393+
self._reverse = reverse
394+
self._n = 0
395+
super(Chase, self).__init__(pixel_object, speed, color)
396+
397+
@property
398+
def reverse(self):
399+
"""
400+
Whether the animation is reversed
401+
"""
402+
return self._reverse
403+
404+
@reverse.setter
405+
def reverse(self, value):
406+
self._reverse = value
407+
self._direction = -1 if self._reverse else 1
408+
409+
def draw(self):
410+
self._n = (self._n + 1) % self._size
411+
n = self._n
412+
size = self._size
413+
half_size = size // 2
414+
for i in range(len(self.pixel_object)):
415+
self.pixel_object[i] = self.color if ((i + n) % size) < half_size else (0, 0, 0)
416+
self.show()
417+
418+
377419
class AnimationSequence:
378420
"""
379421
A sequence of Animations to run in sequence, looping forever.

0 commit comments

Comments
 (0)