Skip to content

Matrix Example

Adam Haile edited this page Jun 27, 2017 · 16 revisions

Now that you have LEDs working in one dimension, let's talk about two dimensions! This is a bit of an advanced topic but needs to be covered. If you do not plan on using the AllPixel to control a matrix arrangement of LEDs, feel free to skip to the next topic.

First, to be clear, the AllPixel will not control "true" LED matrices where each pixel actually does have a real (x,y) coordinate. Instead, BiblioPixel provides the ability to map (x,y) coordinates to the one-dimensional index of an LED strip. As far as the AllPixel is concerned, it's an LED strip like any other. But by informing BiblioPixel of how you have laid out the strip into a matrix, you can display two dimensional animations with ease.

For example, the below matrix is logically a strip, even if its pixels are laid out in a matrix pattern. The top of Column A is the first pixel in the strip, and the bottom of Column H is the last pixel in the strip.

Let's start with something relatively simple. Download this code here (right-click and save as).

import bibliopixel
#causes frame timing information to be output
bibliopixel.log.setLogLevel(bibliopixel.log.DEBUG)

#Load driver for the AllPixel
from bibliopixel.drivers.serial import Serial, LEDTYPE
#set number of pixels & LED type here 
driver = Serial(num = 8*8, ledtype = LEDTYPE.WS2812B)

#load the LEDMatrix class
from bibliopixel import Matrix, Rotation
#change rotation, vert_flip, and serpentine as needed by your display
led = Matrix(driver, 
				rotation = Rotation.ROTATE_0, 
				vert_flip = False,
				serpentine = True)

#load matrix calibration test animation
from bibliopixel.animation import MatrixCalibrationTest
anim = MatrixCalibrationTest(led)

try:
	#run the animation
    anim.run()
except KeyboardInterrupt:
	#Ctrl+C will exit the animation and turn the LEDs offs
    led.all_off()
    led.update()

Be sure to set the number and type of LEDs on line 8, just like in Library Install and Strip Example. As you can see, DriverSerial just takes a single number of LEDs, but is written as 8*8 (8 rows by 8 columns) to make it a little more readable. LEDMatrix can take a width and height value if your matrix is not square, but by default it assumes a square and tries to set the width and height to the square root of the total number of pixels. It will throw a ValueError exception if this is not possible. In this case, you will have to specify the width and height in the LEDMatrix definition (see the LEDMatrix class documentation for more info).

Once you run the animation, you should see something like this:

If your matrix is not laid out the same as the BiblioPixel defaults, that's OK! There are many configuration options. Check out the Display Setup documentation on the BiblioPixel Wiki.

Library Install and Strip Example | Basic Animations