2019-12-01 14:17:42 +01:00
|
|
|
#!/usr/bin/env python3
|
2012-12-27 02:06:36 +01:00
|
|
|
|
|
|
|
import struct
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
|
|
|
|
from random import random
|
|
|
|
|
2019-12-01 14:17:42 +01:00
|
|
|
class SK6812Command:
|
2012-12-27 02:06:36 +01:00
|
|
|
# command definitions
|
|
|
|
SET_COLOR = 0
|
|
|
|
FADE_COLOR = 1
|
|
|
|
ADD_COLOR = 2
|
|
|
|
SET_FADESTEP = 3
|
|
|
|
|
2019-12-01 14:17:42 +01:00
|
|
|
def __init__(self, action = SET_COLOR, strip = 0, module = 0, d0 = 0, d1 = 0, d2 = 0, d3 = 0):
|
|
|
|
self.action = int(action)
|
|
|
|
self.strip = int(strip)
|
|
|
|
self.module = int(module)
|
|
|
|
self.d0 = int(d0)
|
|
|
|
self.d1 = int(d1)
|
|
|
|
self.d2 = int(d2)
|
|
|
|
self.d3 = int(d3)
|
2012-12-27 02:06:36 +01:00
|
|
|
|
|
|
|
def serialize(self):
|
2019-12-01 14:17:42 +01:00
|
|
|
return struct.pack(">BBBBBBB", self.action, self.strip, self.module, self.d0, self.d1, self.d2, self.d3)
|
2012-12-27 02:06:36 +01:00
|
|
|
|
2019-12-01 14:17:42 +01:00
|
|
|
class SK6812:
|
2012-12-27 02:06:36 +01:00
|
|
|
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
|
2019-12-01 14:17:42 +01:00
|
|
|
packet = b''
|
2012-12-27 02:06:36 +01:00
|
|
|
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
|
2019-12-01 14:17:42 +01:00
|
|
|
self.__commands.append(SK6812Command(SK6812Command.SET_FADESTEP, d0 = fadestep))
|
2012-12-27 02:06:36 +01:00
|
|
|
|
2019-12-01 14:17:42 +01:00
|
|
|
def set_color(self, strip, module, r, g, b, w):
|
2012-12-27 02:06:36 +01:00
|
|
|
# add a "set color" command
|
2019-12-01 14:17:42 +01:00
|
|
|
self.__commands.append(SK6812Command(SK6812Command.SET_COLOR, strip, module, r, g, b, w))
|
2012-12-27 02:06:36 +01:00
|
|
|
|
2019-12-01 14:17:42 +01:00
|
|
|
def fade_color(self, strip, module, r, g, b, w):
|
2012-12-27 02:06:36 +01:00
|
|
|
# add a "fade to color" command
|
2019-12-01 14:17:42 +01:00
|
|
|
self.__commands.append(SK6812Command(SK6812Command.FADE_COLOR, strip, module, r, g, b, w))
|
2012-12-27 02:06:36 +01:00
|
|
|
|
2019-12-01 14:17:42 +01:00
|
|
|
def add_color(self, strip, module, r, g, b, w):
|
2012-12-27 02:06:36 +01:00
|
|
|
# add a "add to color" command
|
2019-12-01 14:17:42 +01:00
|
|
|
self.__commands.append(SK6812Command(SK6812Command.ADD_COLOR, strip, module, r, g, b, w))
|
2012-12-27 02:06:36 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-12-01 14:17:42 +01:00
|
|
|
w = SK6812("192.168.2.222", 2703)
|
2012-12-27 02:06:36 +01:00
|
|
|
w.set_fadestep(1);
|
|
|
|
|
|
|
|
while True:
|
2019-12-01 14:17:42 +01:00
|
|
|
w.set_color(10, 255, 255, 255, 255)
|
2012-12-27 02:06:36 +01:00
|
|
|
for i in range(20):
|
|
|
|
w.fade_color(i, 0, 0, 0)
|
|
|
|
w.commit()
|
|
|
|
time.sleep(0.2)
|