-
Notifications
You must be signed in to change notification settings - Fork 56
Animations
The animation module provides base classes upon which to build custom animation routines. Animations built from these classes can be run with only two lines of code and continue running based on a wide array of options. For example:
anim = StripChannelTest(led)
anim.run()
This will run StripChannelTest continuously and as fast as possible.
For more information on using the animation classes, see Writing an Animation.
BaseAnimation serves as the basis for BaseStripAnim and BaseMatrixAnim and should not be inherited from directly. It simply provides any code that is shared between BaseStripAnim and BaseMatrixAnim.
(layout)
By default, preRun does nothing but can be overridden by any class that inherits from BaseStripAnim and BaseMatrixAnim. If overridden, if will always be called at the beginning of each call to run(). This is mainly provided as a way to always initialize to a proper state without the user of your animation class having to call anything except run().
By default, preStep does nothing but can be overridden by any class that inherits from BaseStripAnim and BaseMatrixAnim. If overridden, if will always be called at the beginning of each call to step(). This is mainly provided as a way to always initialize to a proper state without the user of your animation class having to call anything except step().
(amt = 1)
- amt - Amount to increment _step by after each call to step(). Increments by 1 by default. As step() is generally called by run(), this value will be set to whatever is passed into run(). This parameter is required by can be ignored by your step() implementation. Actually incrementing _step is the responsibility of the animation author.
step() MUST be overridden for an animation class otherwise it will throw an exception. This is where all of the work is actually done to generate the animation frame. The frame should only be generated here. Updating of the display will occur in run().
def step(self, amt = 1):
self.layout.fill((self._step, 0, 0))
self._step += amt
if self._step > 255:
self._step = 0
The above step() implementation will fill the entire display with increasing brightness values of red, wrapping around to 0% once it reaches maximum brightness.
(amt = 1, fps=None, sleep=None, max_steps = 0, untilComplete = False, max_cycles = 0, threaded = False, joinThread = False)
- amt - Amount to increment _step after each frame is generated. Can be used to run the animation faster without changing the framerate by skipping frames.
- fps - Target frames per second to run the animation at. Guaranteed to not run faster but may not run as fast as set if the frame generation and display update takes too long. Ignored if sleep is set.
- sleep - Amount of time, in milliseconds, between each frame, including the time it takes to generate the frame and update the display. fps is ignored if sleep is set.
- max_steps - How many total steps to run the animation for before automatically quitting. This is incremented on its own and is not effected by amt.
- untilComplete - True/False: Run the animation until animComplete flag is set by the animation itself.
- max_cycles - Only used with untilComplete. If set above 0, the animation will run until animComplete flag has been set that many times.
- threaded - Setting to True will run the animation on a separate thread and return immediately, unless joinThread is set. The script or application must not close while the animation is threaded. NOTE: You cannot run multiple animations at the same time! They will not superimpose on themselves. Instead it will cause a failure when trying to update the display. This is intended for doing other, non-animation things while the animation is running.
- joinThread - If threaded is True, setting this to True will run the animation on another thread but run() will not return until the animation has completed or is stopped.
Instance of Layout that was passed into the animation.
The current frame step which can be used for keeping track of the animation state and is incremented in step()
This can be set to a number of milliseconds by the animation itself to override any fps or sleep settings. Mainly it is to be used for animations that require variable timing between each frame.
Set to True to flag the animation sequence as complete and use with the completed
option when running the animation.
BaseStripAnim is the basis for any animation that runs with an instance of Strip and provides all the same methods and properties as BaseAnimation plus those below.
(layout, start=0, end=-1)
- layout - An instance of Strip
- start - Pixel index to start the animation at, defaults to 0
- end - Pixel index to end the animation at. It left at -1, it will default to the last pixel on the strip.
Note: The start and end parameters must be respected by the inheriting animation for them to do anything.
In addition to the properties provided by BaseAnimation, the following properties are available to classes that inherit from BaseStripAnim.
Index of the first pixel to start the animation at. This does not have to be used by an animation class but it is recommended that it be respected.
Index of the last pixel to end the animation at. This does not have to be used by an animation class but it is recommended that it be respected.
The calculated size of the total animation space, (_end - _start)
BaseStripAnimation is the basis for any animation that runs with an instance of Strip and provides all the same methods and properties as BaseAnimation plus those below.
(layout, width=0, height=0, startX=0, startY=0)
- layout - An instance of Matrix
- width - Width of the pixel matrix. If 0, width will be retrieved from the led instance.
- height - Height of the pixel matrix. If 0, height will be retrieved from the led instance.
- startX - X coordinate to start the animation at.
- startY - Y coordinate to start the animation at.
Note: The startX and startY parameters must be respected by the inheriting animation for them to do anything.
In addition to the properties provided by BaseAnimation, the following properties are available from BaseMatrixAnim.
Width of the pixel matrix to use with the animation.
Height of the pixel matrix to use with the animation.
X coordinate to start the animation at. This does not have to be used by an animation class but it is recommended that it be respected.
Y coordinate to start the animation at. This does not have to be used by an animation class but it is recommended that it be respected.
BaseGameAnim is the basis for interactive animations and inherits from BaseMatrixAnim, providing all the same methods and properties. However, it also provides automatic handling for inputs and speed timing. It is highly recommended that game animations be designed to run at a specific frame-rate as the timing is based off of frames, not actual clock time. Max speed will depending on the performance of your display but 30fps has been found to work well in most cases.
(layout, inputDev)
- layout - An instance of Matrix
- inputDev - An input device class object that inherits from BaseGamePad. This is required to get control input for the animation.
(name)
- name - Name of the timing object to check as setup by setSpeed
checkSpeed can be used in your animation inside of the step() method to determine if something should change on that frame, such as moving a sprite.
(name, speed)
- name - Name for this timing object. Used to check via checkSpeed.
-
speed - Int value >= 1. checkSpeed will return true when
(frameCount % speed)
equals 0, making this an inverse value. Lower is faster. 1 means checkSpeed will return True every frame. 4 means checkSpeed returns True every fourth frame and is 1/4th the speed.
(key, func, speed=1, hold=True)
- key - Name of the key used by your GamePad instance, such as "A" or "START"
- func - Function to call when key is pressed or held. May be a lambda.
- speed - How often to respond to the key being pressed. Same logic as checkSpeed.
- hold - Continue to call func as long as the key is pressed.
This MUST be called inside of the animation step() method. It is kept as a manual call so that the developer can decide where in their step() process to handle inputs.
BaseCircleAnim is the basis for any animation that runs with an instance of Circle and provides all the same methods and properties as BaseAnimation plus those below.
(layout)
- layout - An instance of Circle
Note: The start and end parameters must be respected by the inheriting animation for them to do anything.
In addition to the properties provided by BaseAnimation, the following properties are available to classes that inherit from BaseCircleAnim.
Array of the ring mapping provided to the Circle instance.
Total number of rings, as passed to the Circle instance.
Index of the outer most ring on the circle.
Array of degrees between each pixel for the given ring index.
Sequence
automatically handles stepping through queues of animations while being an animation itself, making it very easy to integrate into existing code. It provides all the same methods available on BaseAnimation.
(layout, animations=None)
- layout - An instance of Layout
- animations - A list of animation class objects to run, can be of any type. If none are given they must be added with addAnim before calling run().
(anim, amt = 1, fps=None, max_steps = 0, untilComplete = False, max_cycles = 0)
- anim - Animation class object to add to internal queue
- amt - Same as amt parameter on the step() method.
- fps - Framerate to run animation at. If left as None, the framerate given when calling Sequence.run() will be used, allowing for a default framerate across all animations in the queue.
All other parameters are the same as on the base animation run() method but you must use either max_steps or untilComplete and max_cycles, otherwise the animation with run forever and later animations will never be run!