Created
December 8, 2012 19:46
-
-
Save craftyjon/4241604 to your computer and use it in GitHub Desktop.
xmas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import time | |
import struct | |
import colorsys | |
import sys | |
import math | |
import random | |
def output(strand, data): | |
global ser | |
data = [item for sublist in data for item in sublist] | |
checksum = 0 | |
for member in data: | |
checksum ^= member | |
data += [checksum] | |
packet = [0x99, strand, 0x10, 0xE0, 0x01] + data | |
buf = "".join([struct.pack('B', char) for char in packet]) | |
ser.write(buf) | |
ser.flushInput() | |
if __name__ == "__main__": | |
ser = serial.Serial('/dev/ttyACM0', 2000000, timeout=2) | |
ser.flushInput() | |
buf = "".join([struct.pack('B', char) for char in [0x99, 0x00, 0x15, 0x02, 0x00, 0xa0, 0x00, 0xa0]]) | |
ser.write(buf) | |
#sys.exit(0) | |
time.sleep(0.5) | |
random.seed() | |
hue = 0.0 | |
#sleep_step = 0.0 | |
#delta = 0.0005 | |
sleep_step = 0 | |
delta = 0.005 | |
timestep = 0.1 | |
rainbow_width = 64.0 | |
rd = 1 | |
offset = 0.5 | |
pixels = [(0, 0, 0)] * 320 | |
up = [False] * 320 | |
while True: | |
if random.random() > 0.8: | |
n = 1 + int(random.random() * 3.0) | |
p = int(random.random() * len(pixels)) | |
if pixels[p] > 0: | |
c = random.random() | |
color = (0, 1, 0) | |
if c <= 0.4: | |
color = (0, 0, 1) | |
if c > 0.8: | |
color = (1, 1, 1) | |
for i in range(n): | |
pixels[p] = color | |
up[p] = True | |
for i in range(len(pixels)): | |
r, g, b = pixels[i] | |
if up[i]: | |
if r > 0: | |
if r == 255: | |
up[i] = False | |
else: | |
r += 2 | |
if g > 0: | |
if g == 255: | |
up[i] = False | |
else: | |
g += 2 | |
if b > 0: | |
if b == 255: | |
up[i] = False | |
else: | |
b += 2 | |
else: | |
if r > 0: | |
r -= 1 | |
if g > 0: | |
g -= 1 | |
if b > 0: | |
b -= 1 | |
pixels[i] = (min(255, r), min(255, g), min(255, b)) | |
output(1, pixels[:160]) | |
time.sleep(0.01) | |
output(2, pixels[160:]) | |
time.sleep(sleep_step) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment