sk6812-client/sk6812.py

80 lines
2.2 KiB
Python
Raw Normal View History

2019-12-01 14:17:42 +01:00
#!/usr/bin/env python3
import struct
import socket
import time
from random import random
class SK6812Command:
# command definitions
SET_COLOR = 0
FADE_COLOR = 1
ADD_COLOR = 2
SET_FADESTEP = 3
SET_NUM_MODS = 4
def __init__(self, action = SET_COLOR, module = 0, d0 = 0, d1 = 0, d2 = 0, d3 = 0):
self.action = int(action)
self.strip = 0 # FIXME
self.module = int(module)
self.d0 = int(d0)
self.d1 = int(d1)
self.d2 = int(d2)
self.d3 = int(d3)
def serialize(self):
return struct.pack(">BBHBBBB", self.action, self.strip, self.module, self.d0, self.d1, self.d2, self.d3)
class SK6812:
def __init__(self, host, port):
self.__commands = []
# create the UDP socket
family, socktype, proto, canonname, sockaddr = socket.getaddrinfo(host, port, 0, socket.SOCK_DGRAM)[0]
self.__socket = socket.socket(family, socktype, proto)
self.__socket.connect(sockaddr)
def commit(self):
# send the data
packet = b''
for command in self.__commands:
packet = packet + command.serialize()
if packet:
self.__socket.send(packet)
self.__commands = []
def set_fadestep(self, fadestep):
# add a "set fadestep" command
self.__commands.append(SK6812Command(SK6812Command.SET_FADESTEP, d0 = fadestep))
def set_color(self, module, r, g, b, w):
# add a "set color" command
self.__commands.append(SK6812Command(SK6812Command.SET_COLOR, module, r, g, b, w))
def fade_color(self, module, r, g, b, w):
# add a "fade to color" command
self.__commands.append(SK6812Command(SK6812Command.FADE_COLOR, module, r, g, b, w))
def add_color(self, module, r, g, b, w):
# add a "add to color" command
self.__commands.append(SK6812Command(SK6812Command.ADD_COLOR, module, r, g, b, w))
def set_num_modules(self, modules):
# add a "set number of modules" command
self.__commands.append(SK6812Command(SK6812Command.SET_NUM_MODS, d0 = (modules >> 24) & 0xFF, d1 = (modules >> 16) & 0xFF, d2 = (modules >> 8) & 0xFF, d3 = modules & 0xFF))
if __name__ == "__main__":
w = SK6812("192.168.2.222", 2703)
w.set_fadestep(1);
while True:
w.set_color(10, 255, 255, 255, 255)
for i in range(20):
w.fade_color(i, 0, 0, 0)
w.commit()
time.sleep(0.2)