Custom
Custom animations are just Python functions, but they allow you to do limitless things with your LEDs.
File Location
The default location for custom animation files is /etc/mqttany/led-anim/
,
but you can specify multiple other locations using the anim dir
option in
the config file.
File Header
You will likely want to add the following to the top of each of your files to get the best experience writing your own animations. This will provide type hints and autocompletion while creating animations.
import typing as t
if t.TYPE_CHECKING:
import threading, sys
sys.path.append("/opt/mqttany")
from mqttany.modules.led.anim import parse_color, parse_pixel
from mqttany.modules.led.array.base import baseArray
from mqttany.modules.led.common import Color
from mqttany.logger import mqttanyLogger
log: mqttanyLogger
FRAME_MS: float
If you did not install MQTTany in the default /opt/mqttany
directory you
will need to modify the path in the above header or otherwise ensure that the
imports are available.
Function Naming
Only functions with names starting with anim_
will be imported as
animations. They will be added to the animation dictionary as
filename.func_name
, func_name
will have the anim_
prefix removed.
Ex. An animation function called anim_blink
in a file named
example.py
will be known as example.blink
when triggering via MQTT or
from other animations.
Looping
Animation functions should not loop to repeat the same action themselves, this
is done by the animation manager for the array based on the repeat
parameter. Animation functions should NEVER run an infinite loop, always
use repeat: 0
to accomplish this.
Function Signature
Your animation function must have the following signature for it to work reliably:
def anim_blink(
array, # type: baseArray
cancel, # type: threading.Event
**kwargs, # type: t.Any
) -> None:
Parameter |
Description |
---|---|
|
This is the LED array object subclassed from
|
|
This is a |
|
You should always use Arguments can be pulled from # sets 'value' to None if 'key' is not in 'kwargs'
value = kwargs.get("key", None)
|
array
Object
The array
object provides methods to get information about the array and
set LED colors. The array
object received will be subclassed from
modules.led.array.base.baseArray
.
Method / Property |
Description |
---|---|
|
Outputs the current state of LEDs in memory. This should be called when you are done setting LEDs and want to display those changes on the physical array. |
|
Sets the specified |
|
Returns a |
|
Sets the specified |
|
Returns a tuple of |
|
Sets the specified |
|
Returns a 24-bit |
|
Sets the brightness for the array. Also available
as the property |
|
Returns the current brightness for the array. Also
available as the property |
|
Returns the number of pixels in the array. Also
available as the property |
|
Returns the number of color channels (3 for RGB or
4 for RGBW). Also available as the property
|
|
Can be use to get or set the array brightness. |
|
Returns the number of pixels in the array. |
|
Returns the number of color channels. |
|
Is a dictionary containing all known animations. You can use this to run other animations by name from within your animation function. For example setting the array off after your animation finishes: array.anims["off"](array, cancel)
|
Globals
Some useful values and functions are inserted into the globals for all animations and can be used as if they had been defined or imported in your animation file.
Property |
Description |
---|---|
|
This gives you a logger that logs to
|
|
The configuration value |
class Color(t.NamedTuple):
r: int
g: int
b: int
w: int = 0
def asInt(self) -> int: ...
@classmethod
def fromInt(
cls, color: int
) -> Color: ...
@staticmethod
def getIntFromRGB(
r: int,
g: int,
b: int,
w: int = 0,
) -> int: ...
@staticmethod
def getRGBFromInt(
color: int,
) -> t.Tuple[
int, int, int, int
]: ...
|
The |
parse_color(
array,
color = None,
r = -1,
g = -1,
b = -1,
w = -1,
pixel = None,
)
|
This function can be used to get a |
parse_pixel(
array,
p,
)
|
This can be used to parse various pixel range arguments as
used with the Pixels may be specified in any of the following ways: # single index
"pixel": 2
# range string
"pixel": "4-6"
# list
"pixel": [
2, # pixel 2
"4-6", # pixels 4, 5 and 6
[10, 5] # pixels 10, 11, 12, 13, and 14
]
|