Writing an Animation - adammhaile/ae45LhX89i GitHub Wiki

BiblioPixel would be useless if new animations couldn't easily be added. While you don't need do use the animation classes to produce an animation effect, doing so will greatly ease the process in most cases.

The BaseStripAnim and BaseMatrixAnim classes work around the concept of generating an animation as a simple state machine, where each frame of the animation is generated in an atomic operation.

For example, the following strip animation class:

class StripTest(BaseStripAnim):
    def __init__(self, led, start=0, end=-1):
        #The base class MUST be initialized by calling super like this
        super(StripTest, self).__init__(led, start, end)
        #Create a color array to use in the animation
        self._colors = [colors.Red, colors.Orange, colors.Yellow, colors.Green, colors.Blue, colors.Indigo]
    
    def step(self, amt = 1):
        #Fill the strip, with each sucessive color 
        for i in range(self._led.numLEDs):
            self._led.set(i, self._colors[(self._step + i) % len(self._colors)])
        #Increment the internal step by the given amount
        self._step += amt

img/strip_test.gif

This animation displays successive colors from self._colors every time step() is called. At the end of the call to step(), self._step is incremented by amt. If the animation does not require keeping track of the current step, this can be omitted, but it must be implemented if needed by the animation. This is all that is required for any animation, refer to BaseStripAnim for more information on other available methods and properties.

Creating a matrix animation is not much different:

class MatrixTest(BaseMatrixAnim):
    def __init__(self, led, start=0, end=-1):
        #The base class MUST be initialized by calling super like this
        super(MatrixTest, self).__init__(led, start, end)
        #Create a color array to use in the animation
        self._colors = [colors.Red, colors.Orange, colors.Yellow, colors.Green, colors.Blue, colors.Indigo]
    
    def step(self, amt = 1):
        #Fill the strip, with each sucessive color 
        for i in range(self._led.numLEDs):
            self._led.drawRect(-1, -1, i+1, i+1, self._colors[(self._step + i) % len(self._colors)])
        #Increment the internal step by the given amount
        self._step += amt

img/matrix_test.gif

As you can see, the basics are no different other than self._led is an [LEDMatrix]] instance instead of [[LEDStrip]], so matrix functions like [drawRect are available for use.

The update() method of [LEDStrip]] and [[LEDMatrix]] must not be called from inside the [step() method as it will automatically be called by run().