Last active
April 4, 2016 03:53
-
-
Save flyinghyrax/f47640c1c81ce14b16c67cc0a58aa7b2 to your computer and use it in GitHub Desktop.
Demo using Python to talk to badges from MACCDC2016
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
import socket | |
import time | |
import colorsys | |
# address of badge | |
ADDR = "192.168.0.16" | |
# found in src/main.cpp | |
PORT = 11337 | |
# currently unused... | |
# the setColor functions below take the color as tuples like these | |
colors = [ | |
(200,0,0), | |
(0,200,0), | |
(0,0,200) | |
] | |
# can create a socket and pass it to these to change name | |
# or color without keeping an open connection | |
def setColor(sock, color): | |
r, g, b = color | |
sock.sendto(bytes([2, r, g, b]), (ADDR, PORT)) | |
def setName(sock, name): | |
nameBytes = bytes(name, "utf-8") | |
cmdByte = bytes([ 4 ]) | |
payload = cmdByte + nameBytes | |
sock.sendto(payload, (ADDR, PORT)) | |
# these are the same as above except uses a different socket | |
# method which requires the connection to already be opened | |
# (poor code deduplication) | |
def csetColor(sock, color): | |
r, g, b = color | |
sock.send(bytes([2, r, g, b])) | |
def csetName(sock, name): | |
nameBytes = bytes(name, "utf-8") | |
cmdByte = bytes([ 4 ]) | |
payload = cmdByte + nameBytes | |
sock.send(payload) | |
# a utility function with a terrible name | |
def per(flt): | |
return int(round(255 * flt)) | |
# ...loop through some colors | |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: | |
# open socket | |
sock.connect((ADDR, PORT)) | |
# change the text | |
csetName(sock, "Hello") | |
# loop through all the hues in HSV in 1000 steps | |
s = 1.0 | |
v = 1.0 | |
for h in range(0, 1000): | |
# calculate RGB payload | |
fh = float(h) / 1000.0 | |
(fr, fg, fb) = colorsys.hsv_to_rgb(fh, s, v) | |
rgb = (per(fr), per(fg), per(fb)) | |
# set the color | |
csetColor(sock, rgb) | |
# pause so the poor board can breath a little | |
# (to go faster, would probably need to change the firmware) | |
time.sleep(0.05) | |
csetName(sock, "Goodbye") | |
sock.close() | |
# fin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment