Skip to content
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ _build
*.pyc
.env
bundles
.idea
24 changes: 18 additions & 6 deletions adafruit_led_animation/animation/sparkle.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@ class Sparkle(Animation):
:param float speed: Animation speed in seconds, e.g. ``0.1``.
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
:param num_sparkles: Number of sparkles to generate per animation cycle.
:param mask: array to limit sparkles within range of the mask
"""

# pylint: disable=too-many-arguments
def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None):
def __init__(
self, pixel_object, speed, color, num_sparkles=1, name=None, mask=None
):
if len(pixel_object) < 2:
raise ValueError("Sparkle needs at least 2 pixels")
if mask:
self._mask = mask
else:
self._mask = []
if len(self._mask) >= len(pixel_object):
raise ValueError("Sparkle mask should be smaller than number pixel array")
self._half_color = color
self._dim_color = color
self._sparkle_color = color
Expand All @@ -66,16 +75,19 @@ def _set_color(self, color):
self._dim_color = dim_color
self._sparkle_color = color

def _random_in_mask(self):
if len(self._mask) == 0:
return random.randint(0, (len(self.pixel_object) - 1))
return self._mask[random.randint(0, (len(self._mask) - 1))]

def draw(self):
self._pixels = [
random.randint(0, (len(self.pixel_object) - 1))
for _ in range(self._num_sparkles)
]
self._pixels = [self._random_in_mask() for _ in range(self._num_sparkles)]
for pixel in self._pixels:
self.pixel_object[pixel] = self._sparkle_color

def after_draw(self):
self.show()
for pixel in self._pixels:
self.pixel_object[pixel % self._num_pixels] = self._half_color
self.pixel_object[(pixel + 1) % self._num_pixels] = self._dim_color
if (pixel + 1) % self._num_pixels in self._mask:
self.pixel_object[(pixel + 1) % self._num_pixels] = self._dim_color
49 changes: 49 additions & 0 deletions examples/led_animation_sparkle_mask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries, karan bhatia
# SPDX-License-Identifier: MIT
"""
This example uses AnimationsSequence to display multiple animations in sequence, at a five second
interval.

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.
"""
import board
import neopixel

from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import JADE, AQUA, PINK

# Update to match the pin connected to your NeoPixels
pixel_pin = board.A1
# Update to match the number of NeoPixels you have connected
pixel_num = 64
# fmt: off
heart_mask = [ 1, 2, 5, 6,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
33, 34, 35, 36, 37, 38,
42, 43, 44, 45,
51, 52]
unheart_mask = [0, 3, 4, 7,



32, 39,
40, 41, 46, 47,
48, 49, 50, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63]
# fmt: on
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.9, auto_write=False)

animations = AnimationSequence(
Sparkle(pixels, speed=0.05, color=JADE, num_sparkles=1, mask=unheart_mask),
Sparkle(pixels, speed=0.05, color=AQUA, num_sparkles=1),
Sparkle(pixels, speed=0.05, color=PINK, num_sparkles=1, mask=heart_mask),
advance_interval=5,
auto_clear=False,
)

while True:
animations.animate()