From e25f0c2f7e83cb7b06443eb3eb1f4c087aaebd3b Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Thu, 27 Dec 2012 02:06:36 +0100 Subject: [PATCH] Repo created, ws2801 lib + some demo scripts --- knightrider.py | 43 ++++++++++++++++++++++++++++ random_fader.py | 23 +++++++++++++++ ws2801.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100755 knightrider.py create mode 100755 random_fader.py create mode 100755 ws2801.py diff --git a/knightrider.py b/knightrider.py new file mode 100755 index 0000000..5c32314 --- /dev/null +++ b/knightrider.py @@ -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) diff --git a/random_fader.py b/random_fader.py new file mode 100755 index 0000000..455a2f1 --- /dev/null +++ b/random_fader.py @@ -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) diff --git a/ws2801.py b/ws2801.py new file mode 100755 index 0000000..0ae9dec --- /dev/null +++ b/ws2801.py @@ -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)