Repository navigation
Expand file tree
/
Copy pathmuniled.py
More file actions
94 lines (80 loc) · 2.18 KB
/
Copy pathmuniled.py
File metadata and controls
94 lines (80 loc) · 2.18 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Muni LED display
import argparse
import time
import os
import urllib
import urllib2
from xml.dom import minidom
import LPD8806
# Can be used for testing without the LED strip connected
class ConsoleStrip(LPD8806.LEDStrip):
def __init__(self, leds, dev=None):
LPD8806.LEDStrip.__init__(self, leds, '/dev/null')
def update(self):
line = []
for x in range(self.leds):
r = self.buffer[x][self.c_order[0]]
g = self.buffer[x][self.c_order[1]]
b = self.buffer[x][self.c_order[2]]
if r == 255:
line.append('S')
elif r == 134:
line.append('T')
else:
line.append('_')
os.system('clear')
print ''.join(line)
def main(args):
if (args.display == 'led'):
led_strip = LPD8806.LEDStrip(160)
elif args.display == 'console':
led_strip = ConsoleStrip(160)
led_strip.all_off()
try:
while True:
update(led_strip)
time.sleep(5.0)
except:
led_strip.all_off()
raise
def get_predictions(route, stop_id):
base_url = 'http://webservices.nextbus.com/service/publicXMLFeed'
params = {
'command': 'predictions',
'a': 'sf-muni',
'r': route,
's': stop_id,
'useShortTitles': 'true'
}
response = urllib2.urlopen('{url}?{params}'.format(
url=base_url,
params=urllib.urlencode(params)))
dom = minidom.parse(response)
predictions = dom.getElementsByTagName('prediction')
return [int(p.attributes['minutes'].value) for p in predictions]
def update(led_strip=None):
line = [False for i in range(led_strip.leds)]
# inbound
for prediction in get_predictions('N', 3915):
line[80 - prediction - 1] = True
# outbound
for prediction in get_predictions('N', 3914):
line[80 + prediction] = True
# set train leds
for i in range(led_strip.leds):
if line[i]:
led_strip.setHSV(i, 225, .7, 1)
else:
led_strip.setHSV(i, 0, 0, 0)
# set station leds
led_strip.setHSV(79, 320, .7, 1)
led_strip.setHSV(80, 320, .7, 1)
# Update the strip
led_strip.update()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Muni trains on a LED strip.')
parser.add_argument('-d', '--display', choices=['led', 'console'],
default='led', dest='display',
help='Where to display muni stops.')
args = parser.parse_args()
main(args)