Custom library import issue

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Draculen
 
Posts: 72
Joined: Mon Jan 24, 2022 11:14 pm

Custom library import issue

Post by Draculen »

I've been trying to add a custom version of the Sparkle animation from the adafruit animation library to have the animation support on_cycle_complete.

However after adding what seems to be all that is needed for it to be triggered(with no syntax issues) uploading the library to my feather m4 express has no effect/change whatsoever on my Neopixel animations; it just stays on the sparkle animation and never switches. If it makes a difference I have 1 sparkle animation being drawn with a comet animation drawing over the sparkles. I just want to make sure I didnt run into another bug in the library code

Code: Select all

import random
from adafruit_led_animation.animation import Animation

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"


class Sparkle(Animation):
    """
    Sparkle animation of a single color.

    :param pixel_object: The initialised LED object.
    :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, 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
        self._num_sparkles = num_sparkles
        self._num_pixels = len(pixel_object)
        self._pixels = []
        #self.add_cycle_complete_receiver(animation, callback) #ignore
        super().__init__(pixel_object, speed, color, name=name)
    def on_cycle_complete(self):
        self.cycle_count += 1
        if self.cycle_count % self.notify_cycles == 0:
            for callback in self._also_notify:
                callback(self)
                on_cycle_complete_supported=True
    def _set_color(self, color):
        half_color = tuple(color[rgb] // 4 for rgb in range(len(color)))
        dim_color = tuple(color[rgb] // 10 for rgb in range(len(color)))
        for pixel in range(  # pylint: disable=consider-using-enumerate
            len(self.pixel_object)
        ):
            if self.pixel_object[pixel] == self._half_color:
                self.pixel_object[pixel] = half_color
            elif self.pixel_object[pixel] == self._dim_color:
                self.pixel_object[pixel] = dim_color
        self._half_color = half_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 = [self._random_in_mask() for _ in range(self._num_sparkles)]
        for pixel in self._pixels:
            self.pixel_object[pixel] = self._sparkle_color
            if self.draw_count % len(self.pixel_object) == 0:
                self.cycle_complete = True # no change
                
            


    def after_draw(self):
        self.show()
        self.cycle_complete=True #no change
        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
                


User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Custom library import issue

Post by dastels »

Are you replacing CIRCUITPY/lib/adafruit_led_animation/animation/sparkle.mpy with your new file? Either the simple py or a compiled mpy. If not, it's likely picking up the original sparkle.mpy.

Dave

User avatar
Draculen
 
Posts: 72
Joined: Mon Jan 24, 2022 11:14 pm

Re: Custom library import issue

Post by Draculen »

I'm using a standard .python file. I know for previous patches from adafruits github (group.mpy) for instance I just placed the .python version on the same folder/area and then the microcontroller from there on out utilizes the .py

I've been scratching my head over it all day. Removing the original sparkle.mpy locks the microcontroller up until it's placed back inside the folder. Saving my variant of sparkle.py makes the microcontroller reset as normal. Nothing seems to be coming through *shrug*

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Custom library import issue

Post by dastels »

That removing the sparkle.mpy file causes problems (when there is a .py version there) makes me curious. What happens if you replace the entire animations directory with plain .py versions?

Dave

User avatar
Draculen
 
Posts: 72
Joined: Mon Jan 24, 2022 11:14 pm

Re: Custom library import issue

Post by Draculen »

I will try that tomorrow but going off if the patch/replacement it should prioritize all .py files.

My first thought was maybe if there is an error in the .py version. It double back to the original mpy version but as I said there are no errors coming up. The only other thing I can think of is to find a way to open and create my own mpy files with the changes(or any change) implemented.

And to elaborate further on removing the mpy files from the controller, the board just freezes up, resetting the board (one push of the integrated resetbutton) it will recompile and go back to its frozen state. Absolutely strange

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Custom library import issue

Post by dastels »

The mpycross tool compiles PY files into MPY. See https://learn.adafruit.com/creating-and ... py-3106477.

Dave

User avatar
Draculen
 
Posts: 72
Joined: Mon Jan 24, 2022 11:14 pm

Re: Custom library import issue

Post by Draculen »

I spent all day yesterday trying to get py cross setup but trying to get cookiecutter to work through powershell is extremely confusing and UN-nescessarily convuluted, I got hung up on setting the correct PATH for cookie cutter to be called from the command prompt. It had just become so insanely frustrating trying to have support for this one animation to work with comet.

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Custom library import issue

Post by dastels »

I don't remember doing all that. You just need the mpycross command line tool. See https://learn.adafruit.com/creating-and ... py-3106477.

Dave

User avatar
Draculen
 
Posts: 72
Joined: Mon Jan 24, 2022 11:14 pm

Re: Custom library import issue

Post by Draculen »

Thanks for your patience/help dastels, it turns out i'm just an idiot and had my rendition of sparkle.py as a txt document instead of saving it as a true .py file. I'm finally getting code updates which is great but MAN what an oversight on my part. I gotta laugh at myself about this one

Thanks again

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”