Merge pull request #84 from rhooper/sparkle-mask
Move sparkle mask example to own file to make mergeable
This commit is contained in:
commit
8d378fa2ed
3 changed files with 68 additions and 6 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -7,3 +7,4 @@ _build
|
|||
*.pyc
|
||||
.env
|
||||
bundles
|
||||
.idea
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -66,11 +75,13 @@ class Sparkle(Animation):
|
|||
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
|
||||
|
||||
|
|
@ -78,4 +89,5 @@ class Sparkle(Animation):
|
|||
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
examples/led_animation_sparkle_mask.py
Normal file
49
examples/led_animation_sparkle_mask.py
Normal file
|
|
@ -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()
|
||||
Loading…
Reference in a new issue