Commit 578e4f3d authored by Fjen Undso's avatar Fjen Undso
Browse files

add lights.py

parent 417f6eb5
#!/usr/bin/env python3
import socket
import time
UDP_IP = "10.0.0.255"
UDP_PORT = 1234
LED_NUM = 42
TIMEOUT = 1 / 120
VERSION = 2
SEQUENCE = 0
SOCKET = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
def send(data):
"""Send data to the esp listening at UDP_IP:UD_PORT"""
global SEQUENCE
header = bytes([VERSION, SEQUENCE, 0, 0])
SOCKET.sendto(header + data, (UDP_IP, UDP_PORT))
SEQUENCE += 1
time.sleep(TIMEOUT)
def rgbw2bytes(pixel):
"""Convert rgbw color values to bytes. Limit values to range(0,255)."""
out = []
for color in pixel:
if color < 0:
color = 0
if color > 255:
color = 255
out.append(color)
return bytes(out)
def make_stripe_data(pixellist):
"""Convert a list of rgbw color values to bytes and make sure it matches the led strip length"""
assert(len(pixellist) % 4 == 0)
if len(pixellist) > LED_NUM * 4:
pixellist = pixellist[:LED_NUM * 4]
if len(pixellist) / 4 < LED_NUM * 4:
print("!!! Filling missing pixels")
missing_pixels = (LED_NUM - int(len(pixellist) / 4)) * 4
pixellist += missing_pixels * [0]
stripe_data = bytes(0)
for i in range(LED_NUM):
stripe_data += rgbw2bytes(pixellist[i * 4:i * 4 + 4])
return stripe_data
def pixelsteps(index):
bytes = b'\x00'
for i in range(LED_NUM):
if i == index:
bytes += rgb2bytes(255, 0, 0, 0)
else:
bytes += rgb2bytes(0, 0, 0, 0)
return bytes
def fill(red, green, blue, white=0):
data = b'\x00'
for i in range(LED_NUM):
data += rgb2bytes(red, green, blue, white)
return data
def fill_rainbow(brightness=1):
while True:
for i in range(255):
send(fill(round((255 - i) * brightness), round(i * brightness), 0))
for i in range(255):
send(fill(0, round((255 - i) * brightness), round(i * brightness)))
for i in range(255):
send(fill(round(i * brightness), 0, round((255 - i) * brightness)))
print(make_stripe_data([1, 2, 3, 4]))
# while True:
# for i in range(LED_NUM):
# send(pixelsteps(i))
# for i in range(LED_NUM):
# send(pixelsteps(LED_NUM-1-i))
# fill_rainbow()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment