Skip to content

Instantly share code, notes, and snippets.

@kmcallister
Last active December 23, 2015 05:49
Show Gist options
  • Save kmcallister/6589774 to your computer and use it in GitHub Desktop.
Save kmcallister/6589774 to your computer and use it in GitHub Desktop.
LED strip example v2
#!/usr/bin/env python
import time
import socket
import struct
import colorsys
class LEDs(object):
def __init__(self):
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def _conv(self, x):
if x<0: return 0
if x>1: return 31
return int(round(x*31))
# colors is a list of 198 (r,g,b) tuples where each
# color component is a float between 0 and 1.
def send(self, colors):
dat = ''
for cx in colors:
r,g,b = map(self._conv, cx)
dat += struct.pack('<H',
(b << 10) | (r << 5) | g)
self._sock.sendto(dat, ('10.5.0.6', 2772))
leds = LEDs()
PER = 10
while True:
t = (time.time() % PER) / PER
color = colorsys.hsv_to_rgb(t, 1, 1)
leds.send([color] * 198)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment