From f4a25f5a4f3f7bf5b110e9bbd5a9dfa3c73c4a93 Mon Sep 17 00:00:00 2001 From: karan bhatia Date: Fri, 20 Nov 2020 14:44:07 -0500 Subject: [PATCH 1/6] added mask array for sparkle animation --- adafruit_led_animation/animation/sparkle.py | 14 ++++++++++++-- examples/led_animation_sparkle_animations.py | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/adafruit_led_animation/animation/sparkle.py b/adafruit_led_animation/animation/sparkle.py index 612e374..6a6b84f 100644 --- a/adafruit_led_animation/animation/sparkle.py +++ b/adafruit_led_animation/animation/sparkle.py @@ -57,18 +57,22 @@ 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 len(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 self._num_sparkles = num_sparkles self._num_pixels = len(pixel_object) self._pixels = [] + self._mask = mask super().__init__(pixel_object, speed, color, name=name) def _set_color(self, color): @@ -83,9 +87,15 @@ def _set_color(self, color): self._dim_color = dim_color self._sparkle_color = color + def _random_in_mask(self): + if self.mask == None: + return random.randint(0, (len(self.pixel_object) - 1)) + else: + return self.mask[random.randint(0, (len(self.mask)-1))] + def draw(self): self._pixels = [ - random.randint(0, (len(self.pixel_object) - 1)) + self._random_in_mask() for _ in range(self._num_sparkles) ] for pixel in self._pixels: diff --git a/examples/led_animation_sparkle_animations.py b/examples/led_animation_sparkle_animations.py index ad90292..0eb76cf 100644 --- a/examples/led_animation_sparkle_animations.py +++ b/examples/led_animation_sparkle_animations.py @@ -17,8 +17,9 @@ pixel_pin = board.D6 # Update to match the number of NeoPixels you have connected pixel_num = 32 +first_eight_mask = [ 1, 2, 3, 4, 5, 6, 7, 8] -pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False) +pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False, mask=first_eight_mask ) sparkle = Sparkle(pixels, speed=0.05, color=AMBER, num_sparkles=10) sparkle_pulse = SparklePulse(pixels, speed=0.05, period=3, color=JADE) From 5f56db76aac1d124a7795c0a7bf24dc037e01840 Mon Sep 17 00:00:00 2001 From: karan bhatia Date: Sun, 22 Nov 2020 09:47:23 -0500 Subject: [PATCH 2/6] fixed a few issues, added heart example for sparkle animation --- adafruit_led_animation/animation/sparkle.py | 9 ++--- examples/led_animation_sparkle_animations.py | 38 +++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/adafruit_led_animation/animation/sparkle.py b/adafruit_led_animation/animation/sparkle.py index 6a6b84f..07eea6a 100644 --- a/adafruit_led_animation/animation/sparkle.py +++ b/adafruit_led_animation/animation/sparkle.py @@ -61,7 +61,7 @@ class Sparkle(Animation): """ # pylint: disable=too-many-arguments - def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None, mask = None): + def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None, mask=[]): if len(pixel_object) < 2: raise ValueError("Sparkle needs at least 2 pixels") if len(mask) >= len(pixel_object): @@ -88,10 +88,10 @@ def _set_color(self, color): self._sparkle_color = color def _random_in_mask(self): - if self.mask == None: + if len(self._mask) == 0: return random.randint(0, (len(self.pixel_object) - 1)) else: - return self.mask[random.randint(0, (len(self.mask)-1))] + return self._mask[random.randint(0, (len(self._mask)-1))] def draw(self): self._pixels = [ @@ -105,4 +105,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 0eb76cf..d62e1b9 100644 --- a/examples/led_animation_sparkle_animations.py +++ b/examples/led_animation_sparkle_animations.py @@ -9,24 +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 AMBER, JADE, AQUA, PINK -# Update to match the pin connected to your NeoPixels -pixel_pin = board.D6 +# 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 = 32 -first_eight_mask = [ 1, 2, 3, 4, 5, 6, 7, 8] +pixel_num = 64 +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, mask=first_eight_mask ) -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] + +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: - animations.animate() + animations.animate() \ No newline at end of file From c3b868b880303345f0a872b22b283e618985139d Mon Sep 17 00:00:00 2001 From: karan bhatia Date: Mon, 23 Nov 2020 14:35:05 -0500 Subject: [PATCH 3/6] fixed formating issue identified by black and pylint --- adafruit_led_animation/animation/sparkle.py | 13 +++++-------- examples/led_animation_sparkle_animations.py | 13 ++++++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/adafruit_led_animation/animation/sparkle.py b/adafruit_led_animation/animation/sparkle.py index 07eea6a..64cd549 100644 --- a/adafruit_led_animation/animation/sparkle.py +++ b/adafruit_led_animation/animation/sparkle.py @@ -90,14 +90,11 @@ def _set_color(self, color): def _random_in_mask(self): if len(self._mask) == 0: return random.randint(0, (len(self.pixel_object) - 1)) - else: - return self._mask[random.randint(0, (len(self._mask)-1))] + else: + return self._mask[random.randint(0, (len(self._mask) - 1))] def draw(self): - self._pixels = [ - self._random_in_mask() - 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 @@ -105,5 +102,5 @@ def after_draw(self): self.show() for pixel in self._pixels: self.pixel_object[pixel % self._num_pixels] = self._half_color - if (pixel+1) % self._num_pixels in self._mask: 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 d62e1b9..0e5a53f 100644 --- a/examples/led_animation_sparkle_animations.py +++ b/examples/led_animation_sparkle_animations.py @@ -10,12 +10,13 @@ from adafruit_led_animation.animation.sparkle import Sparkle from adafruit_led_animation.sequence import AnimationSequence -from adafruit_led_animation.color import AMBER, JADE, AQUA, PINK +from adafruit_led_animation.color import JADE, AQUA, PINK -# Update to match the pin connected to your NeoPixels +# 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, @@ -32,6 +33,7 @@ 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) @@ -39,8 +41,9 @@ 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, + advance_interval=5, + auto_clear=False, ) - + while True: - animations.animate() \ No newline at end of file + animations.animate() From b5e93fd414bebb8429a13d4db36aebc75b5d74c5 Mon Sep 17 00:00:00 2001 From: karan bhatia Date: Tue, 24 Nov 2020 13:29:37 -0500 Subject: [PATCH 4/6] updated code based on pylint and PR feedback. --- adafruit_led_animation/animation/sparkle.py | 12 +++++++----- examples/led_animation_sparkle_animations.py | 4 +--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/adafruit_led_animation/animation/sparkle.py b/adafruit_led_animation/animation/sparkle.py index 64cd549..bf634cc 100644 --- a/adafruit_led_animation/animation/sparkle.py +++ b/adafruit_led_animation/animation/sparkle.py @@ -61,10 +61,14 @@ class Sparkle(Animation): """ # pylint: disable=too-many-arguments - def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None, mask=[]): + 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 len(mask) >= len(pixel_object): + 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 @@ -72,7 +76,6 @@ def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None, mask=[ self._num_sparkles = num_sparkles self._num_pixels = len(pixel_object) self._pixels = [] - self._mask = mask super().__init__(pixel_object, speed, color, name=name) def _set_color(self, color): @@ -90,8 +93,7 @@ def _set_color(self, color): def _random_in_mask(self): if len(self._mask) == 0: return random.randint(0, (len(self.pixel_object) - 1)) - else: - return self._mask[random.randint(0, (len(self._mask) - 1))] + return self._mask[random.randint(0, (len(self._mask) - 1))] def draw(self): self._pixels = [self._random_in_mask() for _ in range(self._num_sparkles)] diff --git a/examples/led_animation_sparkle_animations.py b/examples/led_animation_sparkle_animations.py index 0e5a53f..8003dd3 100644 --- a/examples/led_animation_sparkle_animations.py +++ b/examples/led_animation_sparkle_animations.py @@ -23,8 +23,7 @@ 24, 25, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36, 37, 38, 42, 43, 44, 45, - 51, 52] - + 51, 52] unheart_mask = [0, 3, 4, 7, @@ -34,7 +33,6 @@ 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( From 0fea0a48f193f05d9b7ab86b82f7379fd39870e3 Mon Sep 17 00:00:00 2001 From: Karan Bhatia Date: Wed, 25 Nov 2020 00:29:59 -0500 Subject: [PATCH 5/6] added black recommended change --- adafruit_led_animation/animation/sparkle.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_led_animation/animation/sparkle.py b/adafruit_led_animation/animation/sparkle.py index bf634cc..446bfc9 100644 --- a/adafruit_led_animation/animation/sparkle.py +++ b/adafruit_led_animation/animation/sparkle.py @@ -61,7 +61,9 @@ class Sparkle(Animation): """ # pylint: disable=too-many-arguments - def __init__(self, pixel_object, speed, color, num_sparkles=1, name=None, mask=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: From 6869e4c128503ac0fa4edc0402f108374716447e Mon Sep 17 00:00:00 2001 From: karan bhatia Date: Wed, 25 Nov 2020 11:19:16 -0500 Subject: [PATCH 6/6] more pylint changes --- examples/led_animation_sparkle_animations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/led_animation_sparkle_animations.py b/examples/led_animation_sparkle_animations.py index 8003dd3..b705253 100644 --- a/examples/led_animation_sparkle_animations.py +++ b/examples/led_animation_sparkle_animations.py @@ -23,7 +23,7 @@ 24, 25, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36, 37, 38, 42, 43, 44, 45, - 51, 52] + 51, 52] unheart_mask = [0, 3, 4, 7,