diff --git a/adafruit_led_animation/animation/sparkle.py b/adafruit_led_animation/animation/sparkle.py index 612e374..446bfc9 100644 --- a/adafruit_led_animation/animation/sparkle.py +++ b/adafruit_led_animation/animation/sparkle.py @@ -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 @@ -83,11 +92,13 @@ 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 @@ -95,4 +106,5 @@ 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 diff --git a/examples/led_animation_sparkle_animations.py b/examples/led_animation_sparkle_animations.py index ad90292..b705253 100644 --- a/examples/led_animation_sparkle_animations.py +++ b/examples/led_animation_sparkle_animations.py @@ -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: