Skip to content

Instantly share code, notes, and snippets.

@wyojustin
Created April 29, 2017 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyojustin/8d7b713cae2cfb7c6c110c5d5e421b0a to your computer and use it in GitHub Desktop.
Save wyojustin/8d7b713cae2cfb7c6c110c5d5e421b0a to your computer and use it in GitHub Desktop.
import time
from numpy import *
import struct
import serial
PIXEL_DATA = 2
BRIGHTNESS = 3
def msg(s, code, payload):
sz = len(payload)
s.write(chr(code) + struct.pack('H', sz) + payload)
resp = s.read(1)
if resp:
out = ord(resp)
else:
out = -1
return out
def brightness(s, val):
return msg(s, BRIGHTNESS, chr(val % 256))
def write_pixel_data(s, data):
return msg(s, PIXEL_DATA, data.tostring())
baudrate=9600
s1 = serial.Serial('/dev/ttyACM0', timeout=1)
s2 = serial.Serial('/dev/ttyACM1', timeout=1)
s3 = serial.Serial('/dev/ttyACM2', timeout=1)
ss = [s1, s2, s3]
l = 1024
red = zeros((l, 3), uint8) + array([255, 1, 1], uint8)
green = zeros((l, 3), uint8) + array([1, 255, 1], uint8)
blue = zeros((l, 3), uint8) + array([1, 1, 255], uint8)
purple = zeros((l, 3), uint8) + array([255, 1, 255], uint8)
yellow = zeros((l, 3), uint8) + array([255, 255, 1], uint8)
white = ones((l, 3), uint8) * 255
black = zeros((l, 3), uint8)
colors = [red, green, blue, white, black, yellow, purple]
try:
for s in ss:
print s.port, brightness(s, 4)
for i in range(100):
for j, s in enumerate(ss):
color = colors[(j + i) % len(colors)]
print s.port, write_pixel_data(s, color)
time.sleep(.1)
# print s.port, write_pixel_data(s, random.randint(0, 255, (l, 3), uint8))
finally:
for s in ss:
print s.port, write_pixel_data(s, black)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment