4. Example Code¶
Example code has been supplied to illustrate the methods available.
Note
Remember to change the Strip definition to suit the LEDs connected. Each of the four terminals may use a different LED type and shape.
Run the code using:
sudo python3 example.py
4.1. Rainbow¶
Displays a moving rainbow on your LEDs.
#!/usr/bin/env python3
import colorsys
import time
from pixelpi import Strip
# Change the terminal type to the type you have
strip1 = Strip(1, 180, ledtype='WS2811_GRB', brightness=50)
strip2 = Strip(2, 180, ledtype='WS2811_GRB', brightness=50)
strip3 = Strip(3, 180, ledtype='WS2811_GRB', brightness=50)
strip4 = Strip(4, 180, ledtype='WS2811_GRB', brightness=50)
spacing = 360.0 / 16.0
hue = 0
try:
while True:
hue = int(time.time() * 100) % 360
for x in range(256):
offset = x * spacing
h = ((hue + offset) % 360) / 360.0
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
for strip in [strip1, strip2, strip3, strip4]:
strip.setLEDs(rgb=(r, g, b), led=x)
for strip in [strip1, strip2, strip3, strip4]:
strip.showLEDs()
time.sleep(0.001)
except KeyboardInterrupt:
for strip in [strip1, strip2, strip3, strip4]:
strip.clearLEDs()
strip.showLEDs()
del strip
4.2. Shifting LEDs¶
The LED colours can be shifted by a number of LEDs and direction:
4.2.1. Shifting LED Strings¶
#!/usr/bin/env python3
from pixelpi import Strip
strip1 = Strip(1, 180, ledtype='WS2811_GRB', brightness=50)
strip2 = Strip(2, 180, ledtype='WS2811_GRB', brightness=50)
strip3 = Strip(3, 180, ledtype='WS2811_GRB', brightness=50)
strip4 = Strip(4, 180, ledtype='WS2811_GRB', brightness=50)
strip1.clearLEDs()
pattern = strip1.getLEDs()
for pixel in range(len(pattern)):
pattern[pixel] = [0, pixel, 0, pattern[pixel][3]]
for strip in [strip1, strip2, strip3, strip4]:
strip.setLEDs(pattern=pattern)
try:
while True:
for strip in [strip1, strip2, strip3, strip4]:
strip.showLEDs()
strip.shift("down", 1)
except KeyboardInterrupt:
for strip in [strip1, strip2, strip3, strip4]:
strip.clearLEDs()
strip.showLEDs()
del strip
4.2.2. Shifting LED Matrices¶
#!/usr/bin/env python3
from pixelpi import Strip
import time
strip = Strip(4, (8, 32), shape="zmatrix", ledtype='WS2812', brightness=30)
for x in range(strip.getWidth):
strip.setLEDs(led=(x, 0), rgb=(128, 0, 0))
strip.showLEDs()
try:
while True:
strip.shift("up", 2)
strip.showLEDs()
time.sleep(0.2)
except KeyboardInterrupt:
strip.clearLEDs()
strip.showLEDs()
del strip
4.3. Mirroring in an Axis¶
4.3.1. Mirroring LED Strings¶
#!/usr/bin/env python3
import time
from pixelpi import Strip
strip1 = Strip(1, 180, ledtype='WS2811_GRB', brightness=50)
strip2 = Strip(2, 180, ledtype='WS2811_GRB', brightness=50)
strip3 = Strip(3, 180, ledtype='WS2811_GRB', brightness=50)
strip4 = Strip(4, 180, ledtype='WS2811_GRB', brightness=50)
strip1.clearLEDs()
pattern = strip1.getLEDs()
for pixel in range(int(len(pattern) / 2)):
pattern[pixel] = [255, 0, 0, pattern[pixel][3]]
pattern[strip1.getLength - pixel - 1] = [0, 0, 255, pattern[strip1.getLength - pixel - 1][3]]
for strip in [strip1, strip2, strip3, strip4]:
strip.setLEDs(pattern=pattern)
try:
while True:
for strip in [strip1, strip2, strip3, strip4]:
strip.mirror()
strip.showLEDs()
time.sleep(1)
except KeyboardInterrupt:
for strip in [strip1, strip2, strip3, strip4]:
strip.clearLEDs()
strip.showLEDs()
del strip
4.3.2. Mirroring LED Matrices¶
#!/usr/bin/env python3
import time
from pixelpi import Strip
strip = Strip(4, (8, 32), shape="zmatrix", ledtype='WS2812', brightness=30)
for x in range(int(strip.getWidth / 2)):
for y in range(strip.getHeight):
strip.setLEDs(led=(x, y), rgb=(128, 0, 0))
strip.setLEDs(led=(strip.getWidth - 1 - x, y), rgb=(0, 0, 128))
strip.showLEDs()
try:
while True:
strip.mirror("horizontal")
strip.showLEDs()
time.sleep(0.2)
except KeyboardInterrupt:
strip.clearLEDs()
strip.showLEDs()
del strip
4.4. Displaying an Image¶
4.4.1. Displaying Images of LED Matrices¶
from time import sleep
from PIL import Image
from pixelpi import Strip
im = Image.open("image.png").convert(mode='RGB', colors=256)
# strip1 = Strip(2, (8, 8), ledtype='SK6812_GRBW', shape="matrix", brightness=0.2)
strip = Strip(terminal=4, size=(8, 32), ledtype='WS2812', shape="zmatrix", brightness=30)
try:
i = 0
while True:
i = i + 1
if i >= 32:
i = 0
for r in range(4):
im = im.transpose(Image.ROTATE_90)
strip.setLEDs(led=(0, i), image=im)
strip.showLEDs()
sleep(0.5)
strip.clearLEDs()
strip.showLEDs()
except KeyboardInterrupt:
strip.clearLEDs()
strip.showLEDs()
del strip
Note: | You can of course display an image on an LED string by supplying a 1 by x image. |
---|