Skip to content

Commit 71cc6f1

Browse files
committed
Add Pulse animation.
1 parent b059310 commit 71cc6f1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

adafruit_led_animation/animation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,30 @@ def draw(self):
339339
self.show()
340340

341341

342+
class Pulse(Animation):
343+
344+
def __init__(self, pixel_object, speed, color, period=5, max_intensity=1, min_intensity=0):
345+
self._intensity = min_intensity
346+
self.max_intensity = max_intensity
347+
self.min_intensity = min_intensity
348+
self._direction = 1.0
349+
self._intensity_step = 2 / (period / speed)
350+
self._bpp = len(pixel_object[0])
351+
super(Pulse, self).__init__(pixel_object, speed, color)
352+
353+
def draw(self):
354+
self._intensity += self._intensity_step * self._direction
355+
if self._direction < 0 and self._intensity <= self.min_intensity:
356+
self._direction = -self._direction
357+
self._intensity = self.min_intensity
358+
elif self._direction > 0 and self._intensity >= self.max_intensity:
359+
self._direction = -self._direction
360+
self._intensity = self.max_intensity
361+
color = [int(self._color[n] * self._intensity) for n in range(self._bpp)]
362+
self.fill(color)
363+
self.show()
364+
365+
342366
class AnimationSequence:
343367
"""
344368
A sequence of Animations to run in sequence, looping forever.

0 commit comments

Comments
 (0)