forked from seanbechhofer/raspberrypi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheerlights.py
More file actions
40 lines (36 loc) · 1.95 KB
/
cheerlights.py
File metadata and controls
40 lines (36 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# coding: Latin-1
# code taken from the cheerlights.com website.
# Load library functions we want
import time
import urllib
# Setup paramters
cheerlightsUrl = "http://api.thingspeak.com/channels/1417/field/1/last.txt"
colourMap = {"red":"200",
"green":"020",
"blue":"002",
"cyan":"022",
"white":"111",
"warmwhite":"222",
"purple":"102",
"magenta":"202",
"yellow":"220",
"orange":"210"}
# Loop indefinitely
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 ...
ledBorgColour = colourMap[colourName] # Get the LedBorg colour to use from the name
else: # Otherwise ...
print "Unexpected colour '" + colourName + "'" # Display the name we did not recognise
ledBorgColour = "000" # Use the colour of black / off
ledBorg = open('/dev/ledborg', 'w') # Open the LedBorg driver
ledBorg.write(ledBorgColour) # Set LedBorg to the new colour
ledBorg.close() # Close the LedBorg driver
except: # If we have an error
pass # Ignore it (do nothing)
finally: # Regardless of errors:
time.sleep(1) # Wait for 1 second