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

Conversation

lila
Copy link

@lila lila commented Nov 22, 2020

the sparkle animations look great on an 8x8 led matrix. But at each iteration, the code picks a random led to sparkle. I wanted to mask the 8x8 matrix so that the sparkles would make an image. for example, here it is with a heart shaped mask:

example

this change adds a optional mask parameter to the Sparkle class. the mask parameter is an array that specifies the narrowed set of values to select from. So if mask=[0, 2, 4, 6, 8, 10], then sparkle would pick randomly among those values.

I also updated the example file for sparkle where it uses the heart shaped mask.

There might be a better way to do this, but this seemed to work. I did compile it locally, and used it in this project:

https://github.com/lila/circuitpythonexpress-matrix

  • k

@FoamyGuy
Copy link
Contributor

@lila thanks for working on this! It looks very neat. The CI actions are showing failed because it wants you to run black code formatting and pylint checker. This page covers the process for Black formatting: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/black the same guide has a section for pylint as well which it may complain about next.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

Thanks for working through those! I know it's a bit of a pain sometimes. There are few more pylint errors. I've provided some suggestions for how to fix the remaining ones.

"""

# 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=[]):
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of [] as the default can we use None instead?

if len(pixel_object) < 2:
raise ValueError("Sparkle needs at least 2 pixels")
if len(mask) >= len(pixel_object):
Copy link
Contributor

Choose a reason for hiding this comment

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

if we go with None as the default parameter, then we can move this length check down below the next thing.

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
Copy link
Contributor

Choose a reason for hiding this comment

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

here we can check for None and set it up accordingly like this:

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")

Copy link
Author

Choose a reason for hiding this comment

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

makes sense, will fix. thanks...

@@ -83,16 +87,20 @@ 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...

@FoamyGuy
Copy link
Contributor

@lila I think it's very close now, sorry for all the little hangups with PyLint and Black.

I think the last error it has now is it's wanting these extra spaces at the end of the heart mask declaration in the example script to be removed:
image

I can see many of the spaces have been used in order to put the code into a shape that matches the physical reality also though. So if it is still complaining about extra spaces after removing the ones I show highlighted above personally I'd be in favor of just adding the pylint disable comment for it so that we can keep the code shape that matches the physical shape.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

These changes to the sparkle animation look good to me. Thanks for working on this @lila this is a really neat new feature!

For the example I think we want to keep the original example intact as well since it's got a comment that specifies what device it's made for (NeoPixel Featherwing) which only has 32 LEDs (8x4 instead of 8x8)

Can you put the original example back into led_animation_sparkle_animations.py and then make a new file for your new one that uses the masks to make a heart on the 8x8 grid. You can name it something like led_animation_sparkle_animations_masked.py

After that I think this is good to go. I tested these changes with a NeoPixel featherwing using this slightly modified version of the example to work with 8x4 and make a "checkerboard" pattern instead of heart:

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, BLUE

# Update to match the pin connected to your NeoPixels
pixel_pin = board.IO38
# Update to match the number of NeoPixels you have connected
pixel_num = 32
# fmt: off
first_mask = [1,   3,  5,  7,
              8,  10, 12, 14,
              17, 19, 21, 23,
              24, 26, 28, 30]


second_mask = [0,   2,  4,  6,
               9,  11, 13, 15,
               16, 18, 20, 22,
               25, 27, 29, 31]


# fmt: on
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False)

animations = AnimationSequence(
    Sparkle(pixels, speed=0.1, color=JADE, num_sparkles=2, mask=first_mask),
    #Sparkle(pixels, speed=0.05, color=AQUA, num_sparkles=1),
    Sparkle(pixels, speed=0.1, color=BLUE, num_sparkles=2, mask=second_mask),
    advance_interval=5,
    auto_clear=False,
)

while True:
    animations.animate()

@kattni can you take a look when you have a moment once you're back in the office

@lila
Copy link
Author

lila commented Dec 1, 2020

that makes sense, i'm happy to make the change, and thank you for your patience.

would you recommend cancelling this pull request and doing a new one where the existing example file has not been edited, that way the file doesn't get unnecessary commits in its history?

or should i just add a new commit to bring it back to what it was?

  • k

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Dec 2, 2020

I think it's fine to stay with this PR, the extra commits to that file won't be an issue I don't think.

@jposada202020
Copy link
Contributor

@FoamyGuy @lila , what do you think about this, is the conflicting merges the only thing left to do here, I think that would be an easy fix and if Foamyguy agrees we could merge. let me know. thanks :)

@lila
Copy link
Author

lila commented May 20, 2021

i apologize, i got busy and this dropped out of my cache :-( let me look at it this evening. i would love to get it merged...

  • k

@kattni
Copy link
Contributor

kattni commented May 20, 2021

The conflicting merge issue will not be present if the changes to the sparkle_animations example are removed and put into a new example as FoamyGuy suggested. I agree that it should be a new example.

@evaherrada evaherrada changed the base branch from master to main June 7, 2021 17:38
@kattni kattni merged commit 6869e4c into adafruit:main Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants