Repo created, ws2801 lib + some demo scripts

This commit is contained in:
Thomas Kolb 2012-12-27 02:06:36 +01:00
commit e25f0c2f7e
3 changed files with 140 additions and 0 deletions

43
knightrider.py Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python
import ws2801
import time
num_modules = 20
interval = 0.1
w = ws2801.WS2801("192.168.23.222", 2703)
w.set_fadestep(1.0/interval)
# dictionary which maps {module: [r, g, b]}
set_colors = {}
curModule = [0, 0, 0]
speed = [0.49, 0.65, 1]
#countUp = [True, True, False]
while True:
set_colors = {}
set_colors[int(curModule[0])] = [0, 0, 0]
set_colors[int(curModule[1])] = [0, 0, 0]
set_colors[num_modules-1-int(curModule[2])] = [0, 0, 0]
set_colors[int(curModule[0])][0] = 255
set_colors[int(curModule[1])][1] = 255
set_colors[num_modules-1-int(curModule[2])][2] = 255
for k in set_colors.keys():
w.add_color(k, set_colors[k][0], set_colors[k][1], set_colors[k][2])
for i in range(num_modules):
w.fade_color(i, 0, 0, 0)
w.commit()
for i in range(3):
curModule[i] += speed[i]
if curModule[i] >= num_modules:
curModule[i] -= num_modules
time.sleep(interval)

23
random_fader.py Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env python
import ws2801
import time
import random
w = ws2801.WS2801("192.168.23.222", 2703)
w.set_fadestep(2)
# dictionary which maps {module: [r, g, b]}
set_colors = {}
while True:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
for i in range(20):
w.fade_color(i, r, g, b)
w.commit()
time.sleep(0.1)
time.sleep(120)

74
ws2801.py Executable file
View File

@ -0,0 +1,74 @@
#!/usr/bin/env python
import struct
import socket
import time
from random import random
class WS2801Command:
# command definitions
SET_COLOR = 0
FADE_COLOR = 1
ADD_COLOR = 2
SET_FADESTEP = 3
def __init__(self, action = SET_COLOR, module = 0, d0 = 0, d1 = 0, d2 = 0):
self.action = action
self.module = module
self.d0 = d0
self.d1 = d1
self.d2 = d2
def serialize(self):
meta = (self.action << 6) | self.module
return struct.pack("BBBB", meta, self.d0, self.d1, self.d2)
class WS2801:
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 = ''
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(WS2801Command(WS2801Command.SET_FADESTEP, d0 = fadestep))
def set_color(self, module, r, g, b):
# add a "set color" command
self.__commands.append(WS2801Command(WS2801Command.SET_COLOR, module, r, g, b))
def fade_color(self, module, r, g, b):
# add a "fade to color" command
self.__commands.append(WS2801Command(WS2801Command.FADE_COLOR, module, r, g, b))
def add_color(self, module, r, g, b):
# add a "add to color" command
self.__commands.append(WS2801Command(WS2801Command.ADD_COLOR, module, r, g, b))
if __name__ == "__main__":
w = WS2801("192.168.2.222", 2703)
w.set_fadestep(1);
while True:
w.set_color(10, 255, 255, 255)
for i in range(20):
w.fade_color(i, 0, 0, 0)
w.commit()
time.sleep(0.2)