Skip to content

Commit 8d378fa

Browse files
authoredOct 31, 2021
Merge pull request #84 from rhooper/sparkle-mask
Move sparkle mask example to own file to make mergeable
2 parents 76d419a + e09c877 commit 8d378fa

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed
 

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ _build
77
*.pyc
88
.env
99
bundles
10+
.idea

‎adafruit_led_animation/animation/sparkle.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@ class Sparkle(Animation):
4040
:param float speed: Animation speed in seconds, e.g. ``0.1``.
4141
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
4242
:param num_sparkles: Number of sparkles to generate per animation cycle.
43+
:param mask: array to limit sparkles within range of the mask
4344
"""
4445

4546
# pylint: disable=too-many-arguments
46-
def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None):
47+
def __init__(
48+
self, pixel_object, speed, color, num_sparkles=1, name=None, mask=None
49+
):
4750
if len(pixel_object) < 2:
4851
raise ValueError("Sparkle needs at least 2 pixels")
52+
if mask:
53+
self._mask = mask
54+
else:
55+
self._mask = []
56+
if len(self._mask) >= len(pixel_object):
57+
raise ValueError("Sparkle mask should be smaller than number pixel array")
4958
self._half_color = color
5059
self._dim_color = color
5160
self._sparkle_color = color
@@ -66,16 +75,19 @@ def _set_color(self, color):
6675
self._dim_color = dim_color
6776
self._sparkle_color = color
6877

78+
def _random_in_mask(self):
79+
if len(self._mask) == 0:
80+
return random.randint(0, (len(self.pixel_object) - 1))
81+
return self._mask[random.randint(0, (len(self._mask) - 1))]
82+
6983
def draw(self):
70-
self._pixels = [
71-
random.randint(0, (len(self.pixel_object) - 1))
72-
for _ in range(self._num_sparkles)
73-
]
84+
self._pixels = [self._random_in_mask() for _ in range(self._num_sparkles)]
7485
for pixel in self._pixels:
7586
self.pixel_object[pixel] = self._sparkle_color
7687

7788
def after_draw(self):
7889
self.show()
7990
for pixel in self._pixels:
8091
self.pixel_object[pixel % self._num_pixels] = self._half_color
81-
self.pixel_object[(pixel + 1) % self._num_pixels] = self._dim_color
92+
if (pixel + 1) % self._num_pixels in self._mask:
93+
self.pixel_object[(pixel + 1) % self._num_pixels] = self._dim_color
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries, karan bhatia
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
This example uses AnimationsSequence to display multiple animations in sequence, at a five second
5+
interval.
6+
7+
For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
8+
a different form of NeoPixels.
9+
"""
10+
import board
11+
import neopixel
12+
13+
from adafruit_led_animation.animation.sparkle import Sparkle
14+
from adafruit_led_animation.sequence import AnimationSequence
15+
from adafruit_led_animation.color import JADE, AQUA, PINK
16+
17+
# Update to match the pin connected to your NeoPixels
18+
pixel_pin = board.A1
19+
# Update to match the number of NeoPixels you have connected
20+
pixel_num = 64
21+
# fmt: off
22+
heart_mask = [ 1, 2, 5, 6,
23+
8, 9, 10, 11, 12, 13, 14, 15,
24+
16, 17, 18, 19, 20, 21, 22, 23,
25+
24, 25, 26, 27, 28, 29, 30, 31,
26+
33, 34, 35, 36, 37, 38,
27+
42, 43, 44, 45,
28+
51, 52]
29+
unheart_mask = [0, 3, 4, 7,
30+
31+
32+
33+
32, 39,
34+
40, 41, 46, 47,
35+
48, 49, 50, 53, 54, 55,
36+
56, 57, 58, 59, 60, 61, 62, 63]
37+
# fmt: on
38+
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.9, auto_write=False)
39+
40+
animations = AnimationSequence(
41+
Sparkle(pixels, speed=0.05, color=JADE, num_sparkles=1, mask=unheart_mask),
42+
Sparkle(pixels, speed=0.05, color=AQUA, num_sparkles=1),
43+
Sparkle(pixels, speed=0.05, color=PINK, num_sparkles=1, mask=heart_mask),
44+
advance_interval=5,
45+
auto_clear=False,
46+
)
47+
48+
while True:
49+
animations.animate()

0 commit comments

Comments
 (0)
Please sign in to comment.