module: tree

The tree module contains an instance of the tree class, this is the main way to modify the trees pixels

Import the tree instance at the top of your pattern file:

from tree import tree

Then use it to modify the pixel data within your main loop

def main(): for pixel in tree.pixels: pixel.set_rgb(0, 0, 0)

class: Tree

set_light(n: int, color: Color)

set the nth light in the strip to the color

tree.set_light(10, Color(2, 3, 4))
get_light(n: int) -> Pixel

get the nth light in the strip

1 2 l = tree.get_light(10) l.set_rgb(255, 255, 255)
update()

This pushes the pixel buffer to the pixel driver, for pc use this pushes the buffer to the pygame+openGL simulator , when ran on a raspberry pi it will push it to the LED strip

Update also regulates the frame rate; typically the tree will target 45 fps, if your pattern runs faster than 22ms it will sleep the rest of the time until its time to generate a new frame. This shouldn't affect the end developer, as they should just think of the main while loop running every 22ms

1 2 3 4 5 6 7 8 9 from tree import tree name = "Example" author = "Ciaran" def run(): while True: # change tree.pixels here tree.update() # push pixels to tree, regulated to 45fps
set_fps(n: int)

you might want your pattern to run either fast or slower than the default 45 fps, from

1 2 3 4 def run(): tree.set_fps(1) # rest of pattern (now running at 1 fps)
fade(n: float = 1.1)

fade the entire tree, larger values of n will fade faster, values < 1 will brighten the tree

tree.lerp(0, 0, 0) is prefered to fade as it is more performant and gives more control over timing

tree.fade()
black()

switch all the lights off

tree.black()
fill(color: Color)

set all the lights to one color

tree.fill(Color.black())
lerp(color: Color, frames: int, fn: Callable[[float], float] = util.linear)

lerp the entire tree from whatever color each pixel is to the target color over the amount of frames. See Color.lerp

1 2 3 4 # fade the tree to red over 10 frames for i in range(10): tree.lerp(Color.red(), 10) tree.update()
sleep(frames: int, allow_lerp: bool = False)

Call tree.update() frames amount of times. Allow lerps to continue with allow_lerp (default disallow)

1 2 3 # fade the tree to red over 10 frames tree.lerp(Color.red(), 10) tree.sleep(10, allow_lerp=True)
pixels: list[Pixel]

This is the pixel buffer held by the tree, on every update this list gets pushed to the pixel driver. The order of this array is the same as the the LEDs on the strip. The best way I've found to render patterns is to iterate over this list

1 2 3 for pixel in tree.pixels: # figure out what color you want for pixel pixel.set_color(#that color)
coords: list[tuple[float, float, float]]

a list of 3d coordinates (x, y, z) which is in order of pixels on the strip ie. parrallel with tree.pixels

1 2 3 for i, coord in enumerate(tree.coords): # figure out what color you want for the coord tree.set_light(i, #that color)
num_pixels: int

this is the number of pixels... pretty self explanitory. equivelant to len(tree.pixels)

1 2 for i in range(tree.num_pixels): tree.set_light(i, Color.random()) # set each light to a random color
height: float

also pretty explanitory; the height of the tree

center = tree.height / 2
distances: list[list[float]]

A 2d array which holds pre-computed 3D euclidean distances between all pairs of pixels. the index of each array is parrallel with tree.pixels

1 2 3 from = 10 # the 10th pixel in the stip to = 20 # the 20th pixel in the strip distance = tree.distances[from][to] # the distnace between them in 3D space