Replaced ws2801 lib by sk6812 lib

This commit is contained in:
Thomas Kolb 2019-12-01 14:17:42 +01:00
parent e25f0c2f7e
commit 00fb9febfa
2 changed files with 101 additions and 22 deletions

79
sk6812.py Executable file
View file

@ -0,0 +1,79 @@
#!/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)

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
import struct import struct
import socket import socket
@ -6,26 +6,26 @@ import time
from random import random from random import random
class WS2801Command: class SK6812Command:
# command definitions # command definitions
SET_COLOR = 0 SET_COLOR = 0
FADE_COLOR = 1 FADE_COLOR = 1
ADD_COLOR = 2 ADD_COLOR = 2
SET_FADESTEP = 3 SET_FADESTEP = 3
def __init__(self, action = SET_COLOR, module = 0, d0 = 0, d1 = 0, d2 = 0): def __init__(self, action = SET_COLOR, strip = 0, module = 0, d0 = 0, d1 = 0, d2 = 0, d3 = 0):
self.action = action self.action = int(action)
self.module = module self.strip = int(strip)
self.d0 = d0 self.module = int(module)
self.d1 = d1 self.d0 = int(d0)
self.d2 = d2 self.d1 = int(d1)
self.d2 = int(d2)
self.d3 = int(d3)
def serialize(self): def serialize(self):
meta = (self.action << 6) | self.module return struct.pack(">BBBBBBB", self.action, self.strip, self.module, self.d0, self.d1, self.d2, self.d3)
return struct.pack("BBBB", meta, self.d0, self.d1, self.d2) class SK6812:
class WS2801:
def __init__(self, host, port): def __init__(self, host, port):
self.__commands = [] self.__commands = []
@ -37,7 +37,7 @@ class WS2801:
def commit(self): def commit(self):
# send the data # send the data
packet = '' packet = b''
for command in self.__commands: for command in self.__commands:
packet = packet + command.serialize() packet = packet + command.serialize()
@ -48,26 +48,26 @@ class WS2801:
def set_fadestep(self, fadestep): def set_fadestep(self, fadestep):
# add a "set fadestep" command # add a "set fadestep" command
self.__commands.append(WS2801Command(WS2801Command.SET_FADESTEP, d0 = fadestep)) self.__commands.append(SK6812Command(SK6812Command.SET_FADESTEP, d0 = fadestep))
def set_color(self, module, r, g, b): def set_color(self, strip, module, r, g, b, w):
# add a "set color" command # add a "set color" command
self.__commands.append(WS2801Command(WS2801Command.SET_COLOR, module, r, g, b)) self.__commands.append(SK6812Command(SK6812Command.SET_COLOR, strip, module, r, g, b, w))
def fade_color(self, module, r, g, b): def fade_color(self, strip, module, r, g, b, w):
# add a "fade to color" command # add a "fade to color" command
self.__commands.append(WS2801Command(WS2801Command.FADE_COLOR, module, r, g, b)) self.__commands.append(SK6812Command(SK6812Command.FADE_COLOR, strip, module, r, g, b, w))
def add_color(self, module, r, g, b): def add_color(self, strip, module, r, g, b, w):
# add a "add to color" command # add a "add to color" command
self.__commands.append(WS2801Command(WS2801Command.ADD_COLOR, module, r, g, b)) self.__commands.append(SK6812Command(SK6812Command.ADD_COLOR, strip, module, r, g, b, w))
if __name__ == "__main__": if __name__ == "__main__":
w = WS2801("192.168.2.222", 2703) w = SK6812("192.168.2.222", 2703)
w.set_fadestep(1); w.set_fadestep(1);
while True: while True:
w.set_color(10, 255, 255, 255) w.set_color(10, 255, 255, 255, 255)
for i in range(20): for i in range(20):
w.fade_color(i, 0, 0, 0) w.fade_color(i, 0, 0, 0)
w.commit() w.commit()