-
Notifications
You must be signed in to change notification settings - Fork 56
Writing an Animation
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, layout, start=0, end=-1):
#The base class MUST be initialized by calling super like this
super(StripTest, self).__init__(layout, 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.layout.numLEDs):
self.layout.set(i, self.color_list[(self._step + i) % len(self._colors)])
# Increment the internal step by the given amount
self._step += amt
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, ledlayout:
#The base class MUST be initialized by calling super like this
super(MatrixTest, self).__init__(layout)
#Create a color array to use in the animation
self.color_list = [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.layout.numLEDs):
self.layout.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
As you can see, the basics are no different other than self.layout is an Matrix instance instead of Strip, so matrix functions like drawRect are available for use.
The update() method of Layout must not be called from inside the step() method as it will automatically be called by run().