Created
January 27, 2014 22:38
-
-
Save uwekamper/8658766 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python2.7 | |
# -*- coding: utf-8 -*- | |
import socket | |
import colorsys # for HSV-to-RGB-conversion | |
UDP_IP = "10.0.0.200" | |
UDP_PORT = 1337 | |
ROWS = 16 | |
COLS = 40 | |
def send_array(data): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.sendto(data, (UDP_IP, UDP_PORT)) | |
def prepare_message(data): | |
"""Prepares the pixel data for transmission over UDP | |
""" | |
# 4 bytes for future use as a crc32 checksum in network byte order. | |
checksum = bytearray([0,0,0,0]) | |
data_as_bytes = bytearray(data) | |
message = data_as_bytes + checksum | |
return message | |
def make_gradient(hue): | |
"""This creates a gradient for on hue value. | |
""" | |
array = [] | |
num_pixels = ROWS * COLS | |
step_size = 255.0 / float(num_pixels) | |
for i in range(num_pixels): | |
h = float(hue) / 360.0 | |
s = 1.0 | |
v = (i * step_size) / 255.0 | |
color = int(i * step_size) | |
(r, g, b) = colorsys.hsv_to_rgb(h, s, v) | |
array += [int(r * 255), int(g * 255), int(b * 255)] | |
return array | |
def cycle_hue(): | |
"""Cycles through all possible hue values and send it to the Mate | |
Light | |
""" | |
for hue in range(360): | |
data = make_gradient(hue) | |
message = prepare_message(data) | |
send_array(message) | |
if __name__ == '__main__': | |
print "Transmitting (press Ctrl+C to abort) ..." | |
try: | |
while True: | |
cycle_hue() | |
except KeyboardInterrupt: | |
print " Goodbye!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment