Created
October 16, 2016 22:02
-
-
Save kevana/32bfa486d9fb0aa20a19694d1b69d783 to your computer and use it in GitHub Desktop.
This file contains 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
from __future__ import division | |
from xml.etree import ElementTree | |
from datetime import datetime | |
import requests | |
import Adafruit_WS2801 | |
import dateutil.parser | |
import sys | |
NEXTRIP_URL = "http://svc.metrotransit.org/NexTrip/56039" | |
PIXEL_COUNT = 25 | |
# Specify a software SPI connection for Raspberry Pi on the following pins: | |
PIXEL_CLOCK = 18 | |
PIXEL_DOUT = 23 | |
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, clk=PIXEL_CLOCK, do=PIXEL_DOUT) | |
# Alternatively specify a hardware SPI connection on /dev/spidev0.0: | |
#SPI_PORT = 0 | |
#SPI_DEVICE = 0 | |
#pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) | |
def time_to_next_train(): | |
"""Return the time to next train in seconds.""" | |
xml_resp = requests.get(NEXTRIP_URL) | |
if xml_resp.status_code != 200: | |
pass | |
tree = ElementTree.fromstring(xml_resp.content) | |
date_str = tree[0][3].text | |
next_dep_date = dateutil.parser.parse(date_str) | |
now_date = datetime.now() | |
diff = next_dep_date - now_date | |
diff_seconds = diff.total_seconds() | |
print("Next Departure at: {}".format(next_dep_date)) | |
print("Seconds to next departure: {}".format(diff_seconds)) | |
return diff_seconds | |
def set_red(): | |
print("Setting color to red") | |
set_pixels_rgb(255, 0, 0) | |
pixels.show() | |
def set_green(): | |
print("Setting color to green") | |
set_pixels_rgb(0, 255, 0) | |
pixels.show() | |
def set_blue(): | |
print("Setting color to blue") | |
set_pixels_rgb(0, 0, 255) | |
pixels.show() | |
def set_pixels_rgb(r, g, b): | |
"""Library func is broken.""" | |
for i in range(PIXEL_COUNT): | |
pixels.set_pixel_rgb(i, r, g, b) | |
def run(): | |
'''Main Entrypoint''' | |
time = time_to_next_train() | |
if time > 7 * 60: | |
set_blue() | |
elif time > 5 * 60: | |
set_green() | |
else: | |
set_red() | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
pixels.clear() | |
pixels.show() | |
else: | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment