Skip to content

added sparkle mask parameter #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions adafruit_led_animation/animation/sparkle.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,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 @@ -83,16 +92,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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the one other pylint issue it has right now is that it wants this if statement to not use the else: like this:

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

Honestly I kind if prefer it written more explicitly like how you did it with the else: personally. But I'll leave it up to you, or we can see if anyone else has thoughts. You can either change it like above to make pylint happy, or add a disable comment in this in this function to have it ignore the check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem, i'll fix...

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
32 changes: 24 additions & 8 deletions examples/led_animation_sparkle_animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,38 @@
import neopixel

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

# Update to match the pin connected to your NeoPixels
pixel_pin = board.D6
pixel_pin = board.A1
# Update to match the number of NeoPixels you have connected
pixel_num = 32
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,

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False)

sparkle = Sparkle(pixels, speed=0.05, color=AMBER, num_sparkles=10)
sparkle_pulse = SparklePulse(pixels, speed=0.05, period=3, color=JADE)

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, sparkle_pulse, advance_interval=5, auto_clear=True,
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:
Expand Down