Skip to content

Instantly share code, notes, and snippets.

@vwillcox
Last active July 29, 2018 12:53
Show Gist options
  • Save vwillcox/490f87de91d06e63e02f77acd12f227b to your computer and use it in GitHub Desktop.
Save vwillcox/490f87de91d06e63e02f77acd12f227b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import time
import unicornhat as unicorn
import urllib
unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.5)
width,height=unicorn.get_shape()
# Setup parameters
cheerlightsUrl = 'http://api.thingspeak.com/channels/1417/field/1/last.txt'
# Name Red Green Blue
colourMap = {'red': (255, 0, 0),
'green': (0, 255, 0),
'blue': (0, 0, 255),
'cyan': (0, 255, 255),
'white': (255, 255, 255),
'warmwhite': (255, 244, 229),
'purple': (128, 0, 128),
'magenta': (255, 0, 255),
'yellow': (255, 255, 0),
'orange': (255, 165, 0),
'pink': (255, 192, 203),
'oldlace': (255, 255, 200)}
while True:
try: # Attempt the following:
cheerlights = urllib.urlopen(cheerlightsUrl) # Open cheerlights file via URL
colourName = cheerlights.read() # Read the last cheerlights colour
cheerlights.close() # Close cheerlights file
if colourMap.has_key(colourName): # If we recognise this colour name then ...
red, green, blue = colourMap[colourName] # Get the UnicornHAT colour to use from the name
else: # Otherwise ...
print 'Unexpected colour "' + colourName + '"' # Display the name we did not recognise
red, green, blue = (0, 0, 0) # Use the colour of black / off
print 'colour= "' + colourName + '"'
unicorn.set_all(red, green, blue)
unicorn.show()
except:
pass
finally:
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment