Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hacklab LED example
#!/usr/bin/env python
import six
import socket
import time
host = "ledpi"
port = 2812
pixels = 776
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def list_to_bytes(data):
if six.PY2:
return "".join(map(chr, data))
else:
return bytes(data)
def send_frame(frame, limit=486, offset=0):
if offset != 0:
frame = frame[offset*3:] + frame[0:offset*3]
pos = 0
while pos < len(frame):
if len(frame) - pos <= limit * 3:
pixel = int(pos / 3)
message = [0x04, pixel >> 8, (pixel & 255)] + frame[pos:]
sock.sendto(list_to_bytes(message), (host, port))
pos = pos + len(frame)
else:
pixel = int(pos / 3)
message = [0x05, (pixel >> 8), (pixel & 255)] + frame[pos:pos+(limit*3)]
sock.sendto(list_to_bytes(message), (host, port))
pos = pos + (limit * 3)
def zap():
while True:
for pixel in range(0, pixels):
frame = []
for p in range(0, pixels):
if pixel == p:
frame.append(255)
frame.append(255)
frame.append(255)
else:
frame.append(0)
frame.append(0)
frame.append(0)
send_frame(frame)
time.sleep(0.035)
def chase(n=5, t=0.05):
while True:
for i in range(0, n):
message = []
for p in range(0, pixels):
if p % n == i:
message.append(255)
message.append(255)
message.append(255)
elif (p+1) % n == i:
message.append(15)
message.append(15)
message.append(15)
else:
message.append(0)
message.append(0)
message.append(0)
send_frame(message)
time.sleep(t)
#zap()
chase()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.