Skip to content

Instantly share code, notes, and snippets.

@sidwarkd sidwarkd/medals.py
Created Feb 20, 2014

Embed
What would you like to do?
Tracking Olympic Medals with the Raspberry Pi
import sys, urllib
from time import sleep
import spidev
def spi_send(bus, data):
xfer_list = []
if type(data) == str:
for c in data:
xfer_list.append(ord(c))
elif type(data) == list:
xfer_list += data
elif type(data) == int:
xfer_list.append(data)
else:
print "Unsupported type passed to spi_send. Must be str, int, or list"
bus.xfer2(xfer_list, 250000)
def clear_display(bus):
spi_send(bus, [0x76])
def display_medal_counts(bus, gold, silver, bronze):
total = gold + silver + bronze
totalstr = "to" + str(total)
goldstr = "go" + str(gold)
silverstr = "si" + str(silver)
bronzestr = "br" + str(bronze)
for i in range(3):
clear_display(bus)
spi_send(bus, totalstr)
spi_send(bus, [0x77, 0x10])
sleep(4)
clear_display(bus)
spi_send(bus, goldstr)
spi_send(bus, [0x77, 0x10])
sleep(4)
clear_display(bus)
spi_send(bus, silverstr)
spi_send(bus, [0x77, 0x10])
sleep(4)
clear_display(bus)
spi_send(bus, bronzestr)
spi_send(bus, [0x77, 0x10])
sleep(4)
try:
import lxml.html
except ImportError:
print "You need to install the lxml module first. Use 'sudo apt-get install python-lxml'"
sys.exit(1)
print("Press Ctrl-C to exit")
print("")
# Setup spi module
spi = spidev.SpiDev()
spi.open(0,0)
try:
while True:
sys.stdout.write('Counting Medals.....')
sys.stdout.flush()
# Add a delay for effect
sleep(2)
page = urllib.urlopen("http://www.nbcolympics.com/medals").read()
html = lxml.html.fromstring(page)
result = html.xpath('//a[text()=" United States of America"]/@href')
nameNode = result[0].getparent().getparent()
goldNode = nameNode.getnext()
silverNode = goldNode.getnext()
bronzeNode = silverNode.getnext()
gold_count = int(goldNode.text_content().strip())
silver_count = int(silverNode.text_content().strip())
bronze_count = int(bronzeNode.text_content().strip())
# Display each count 3 times and then check again
display_medal_counts(spi, gold_count, silver_count, bronze_count)
except KeyboardInterrupt:
clear_display(spi)
spi.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.