Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # Display a runtext with double-buffering.
- from samplebase import SampleBase
- from rgbmatrix import graphics
- import time
- import json
- import requests
- from datetime import timedelta
- from timeloop import Timeloop
- from datetime import datetime
- from trains import loadDeparturesForStation, loadDestinationsForDeparture
- def QueryTrains():
- # Parameters
- app_id = "blablabla"
- api_key = "blalblallalasdkudsjufjsdfksdkujfjfn"
- station = "HYN"
- api_url = "https://transportapi.com/v3/uk/train/station/"+station+"/live.json?app_id="+app_id+"&app_key="+api_key+"&darwin=false&train_status=passenger"
- glq_api_url = "https://transportapi.com/v3/uk/train/station/"+station+"/live.json?app_id="+app_id+"&app_key="+api_key+"&calling_at=GLQ&darwin=false&train_status=passenger"
- glc_api_url = "https://transportapi.com/v3/uk/train/station/"+station+"/live.json?app_id="+app_id+"&app_key="+api_key+"&calling_at=GLC&darwin=false&train_status=passenger"
- #Query API
- response = requests.get(glq_api_url)
- data = response.json()
- return data
- class RunText(SampleBase):
- def __init__(self, *args, **kwargs):
- super(RunText, self).__init__(*args, **kwargs)
- self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!")
- def run(self):
- runs = 0
- while True:
- #print(runs)
- print("run",runs)
- if (runs == 0) or (runs >= 5):
- print("..Querying API for updated departures.")
- data = QueryTrains()
- offscreen_canvas = self.matrix.CreateFrameCanvas()
- calling_scroll = self.matrix.CreateFrameCanvas()
- font = graphics.Font()
- font.LoadFont("fonts/6x10.bdf")
- largefont = graphics.Font()
- largefont.LoadFont("fonts/8x13.bdf")
- textColour = graphics.Color(255, 255, 0)
- textPlatformColour = graphics.Color(255, 255, 255)
- textTimeColour = graphics.Color(255,255,255)
- textTimeDueColour = graphics.Color(255,255,255)
- textTimeLateColour = graphics.Color(255,69,0)
- textTimeCancelledColour = graphics.Color(255,0,0)
- #pos = offscreen_canvas.width
- calling_pos = calling_scroll.width
- display_width = 192
- display_height = 32
- display_pwm_nano = 150
- dep1_plat_loc = 19
- dep2_plat_loc = 19
- dep3_plat_loc = 19
- dep4_plat_loc = 19
- dep1_time_loc = 163
- dep2_time_loc = 163
- dep3_time_loc = 163
- dep4_time_loc = 163
- dep1_time_mins_loc = 163
- dep2_time_mins_loc = 163
- dep3_time_mins_loc = 163
- dep4_time_mins_loc = 163
- time_unit = "mins"
- my_text = self.args.text
- dep1_dest = data["departures"]["all"][0]["destination_name"]
- dep1_platform = data["departures"]["all"][0]["platform"]
- dep1_callingAt = "This service will call at Partick, Exhibition Centre, Anderston, Glasgow Central Low Level, High Street ..and wherever else, bla bla bla"
- dep1_departureTime = data["departures"]["all"][0]["expected_departure_time"]
- dep1_departureTimeMins = data["departures"]["all"][0]["best_departure_estimate_mins"]
- dep1_status = data["departures"]["all"][0]["status"]
- dep2_dest = data["departures"]["all"][1]["destination_name"]
- dep2_platform = data["departures"]["all"][1]["platform"]
- dep2_departureTime = data["departures"]["all"][1]["expected_departure_time"]
- dep2_departureTimeMins = data["departures"]["all"][1]["best_departure_estimate_mins"]
- dep2_status = data["departures"]["all"][1]["status"]
- dep3_dest = data["departures"]["all"][2]["destination_name"]
- dep3_platform = data["departures"]["all"][2]["platform"]
- dep3_departureTime = data["departures"]["all"][2]["expected_departure_time"]
- dep3_departureTimeMins = data["departures"]["all"][2]["best_departure_estimate_mins"]
- dep3_status = data["departures"]["all"][2]["status"]
- dep4_dest = data["departures"]["all"][3]["destination_name"]
- dep4_platform = data["departures"]["all"][3]["platform"]
- dep4_departureTime = data["departures"]["all"][3]["expected_departure_time"]
- dep4_departureTimeMins = data["departures"]["all"][3]["best_departure_estimate_mins"]
- dep4_status = data["departures"]["all"][3]["status"]
- clock = data["time_of_day"]
- dep1_callingAt_width = dep1_callingAt
- print("time ran:",clock)
- start_pos = 0
- for i in dep1_callingAt:
- start_pos = start_pos + 1
- print("Total Number of Characters in the scroll message = ", start_pos)
- end_pos = 0 - (20 + (start_pos * 6))
- print("off-screen position required =",end_pos)
- if dep1_platform is None:
- dep1_platform = "_"
- dep1_dest_loc = 19
- if dep2_platform is None:
- dep2_platform = "_"
- if dep3_platform is None:
- dep3_platform = "_"
- if dep4_platform is None:
- dep4_platform = "_"
- # if dep1_dest is None:
- # dep1_dest = "---No Train--"
- # dep1_platform = "_"
- # dep1_departureTime = "--:--"
- # dep1_departureTimeMins = "--"
- # dep1_status = "CANCELLED"
- # if dep2_dest is None:
- # dep2_dest = "---No Train--"
- # dep2_platform = "_"
- # dep2_departureTime = "--:--"
- # dep2_departureTimeMins = "--"
- # dep2_status = "CANCELLED"
- # if dep3_dest is None:
- # dep3_dest = "---No Train--"
- # dep3_platform = "_"
- # dep3_departureTime = "--:--"
- # dep3_departureTimeMins = "--"
- # dep3_status = "CANCELLED"
- # if dep4_dest is None:
- # dep4_dest = "---No Train--"
- # dep4_platform = "_"
- # dep4_departureTime = "--:--"
- # dep4_departureTimeMins = "--"
- # dep4_status = "CANCELLED"
- dep1_platform_chars = 0
- dep2_platform_chars = 0
- dep3_platform_chars = 0
- dep4_platform_chars = 0
- for i in dep1_platform:
- dep1_platform_chars = dep1_platform_chars + 1
- for i in dep2_platform:
- dep2_platform_chars = dep2_platform_chars + 1
- for i in dep3_platform:
- dep3_platform_chars = dep3_platform_chars + 1
- for i in dep4_platform:
- dep4_platform_chars = dep4_platform_chars + 1
- if dep1_platform_chars == 1:
- dep1_dest_loc = 27
- elif dep1_platform_chars == 2:
- dep1_dest_loc = 32
- else: dep1_dest_loc = 37
- if dep2_platform_chars == 1:
- dep2_dest_loc = 27
- elif dep2_platform_chars == 2:
- dep2_dest_loc = 32
- else: dep2_dest_loc = 37
- if dep3_platform_chars == 1:
- dep3_dest_loc = 27
- elif dep3_platform_chars == 2:
- dep3_dest_loc = 32
- else: dep3_dest_loc = 37
- if dep4_platform_chars == 1:
- dep4_dest_loc = 27
- elif dep4_platform_chars == 2:
- dep4_dest_loc = 32
- else: dep4_dest_loc = 37
- if (dep1_departureTimeMins == 0):
- dep1_time = "due"
- dep1_time_mins_loc = 175
- dep1_time_textColour = textTimeDueColour
- elif (dep1_departureTimeMins > 0 and dep1_departureTimeMins < 10):
- dep1_time = str(dep1_departureTimeMins) + "mins"
- dep1_time_mins_loc = 163
- dep1_time_textColour = textTimeColour
- elif (dep1_departureTimeMins > 9 and dep1_departureTimeMins < 100):
- dep1_time = str(dep1_departureTimeMins) + "mins"
- dep1_time_mins_loc = 157
- dep1_time_textColour = textTimeColour
- else: (dep1_time == "?")
- if (dep2_departureTimeMins == 0):
- dep2_time = "due"
- dep2_time_mins_loc = 175
- dep2_time_textColour = textTimeDueColour
- elif (dep2_departureTimeMins > 0 and dep2_departureTimeMins < 10):
- dep2_time = str(dep2_departureTimeMins) + "mins"
- dep2_time_mins_loc = 163
- dep2_time_textColour = textTimeDueColour
- elif (dep2_departureTimeMins > 9 and dep2_departureTimeMins < 100):
- dep2_time = str(dep2_departureTimeMins) + "mins"
- dep2_time_mins_loc = 157
- dep2_time_textColour = textTimeDueColour
- if (dep3_departureTimeMins == 0):
- dep3_time = "due"
- dep3_time_mins_loc = 175
- dep3_time_textColour = textTimeDueColour
- elif (dep3_departureTimeMins > 0 and dep3_departureTimeMins < 10):
- dep3_time = str(dep3_departureTimeMins) + "mins"
- dep3_time_mins_loc = 163
- dep3_time_textColour = textTimeDueColour
- elif (dep3_departureTimeMins > 9):
- dep3_time = str(dep3_departureTimeMins) + "mins"
- dep3_time_mins_loc = 157
- dep3_time_textColour = textTimeDueColour
- if (dep4_departureTimeMins == 0):
- dep4_time = "due"
- dep4_time_mins_loc = 175
- dep4_time_textColour = textTimeDueColour
- elif (dep4_departureTimeMins > 0 and dep4_departureTimeMins < 10):
- dep4_time = str(dep4_departureTimeMins) + "mins"
- dep4_time_mins_loc = 163
- dep4_time_textColour = textTimeDueColour
- elif (dep4_departureTimeMins > 9):
- dep4_time = str(dep4_departureTimeMins) + "mins"
- dep4_time_mins_loc = 157
- dep4_time_textColour = textTimeDueColour
- if (dep1_status == "LATE"):
- dep1_time_textColour = textTimeLateColour
- elif (dep1_status == "EARLY"):
- dep1_time_textColour = textTimeColour
- elif (dep1_status == "CANCELLED"):
- dep1_time_textColour = textTimeCancelledColour
- dep1_departureTime = "cancelled"
- dep1_time = "cancelled"
- dep1_time_loc = 139
- dep1_time_mins_loc = 139
- if (dep2_status == "LATE"):
- dep2_time_textColour = textTimeLateColour
- elif (dep2_status == "EARLY"):
- dep2_time_textColour = textTimeColour
- elif (dep2_status == "CANCELLED"):
- dep2_time_textColour = textTimeCancelledColour
- dep2_departureTime = "cancelled"
- dep2_time = "cancelled"
- dep2_time_loc = 139
- dep2_time_mins_loc = 139
- if (dep3_status == "LATE"):
- dep3_time_textColour = textTimeLateColour
- elif (dep3_status == "EARLY"):
- dep3_time_textColour = textTimeColour
- elif (dep3_status == "CANCELLED"):
- dep3_time_textColour = textTimeCancelledColour
- dep3_departureTime = "cancelled"
- dep3_time = "cancelled"
- dep3_time_loc = 139
- dep3_time_mins_loc = 139
- if (dep4_status == "LATE"):
- dep4_time_textColour = textTimeLateColour
- elif (dep4_status == "EARLY"):
- dep4_time_textColour = textTimeColour
- elif (dep4_status == "CANCELLED"):
- dep4_time_textColour = textTimeCancelledColour
- dep4_departureTime = "cancelled"
- dep4_time = "cancelled"
- dep4_time_loc = 139
- dep4_time_mins_loc = 139
- runs = 1
- else:
- print("Refresh limit reached, waiting...")
- runs = runs + 1
- offscreen_canvas.Clear()
- len = graphics.DrawText(offscreen_canvas, font, 0, 7, textColour, "1st")
- len = graphics.DrawText(offscreen_canvas, font, dep1_plat_loc, 7, textPlatformColour, dep1_platform)
- len = graphics.DrawText(offscreen_canvas, font, dep1_dest_loc, 7, textColour, dep1_dest)
- len = graphics.DrawText(offscreen_canvas, font, dep1_time_loc, 7, dep1_time_textColour, dep1_departureTime)
- len = graphics.DrawText(offscreen_canvas, font, 0, 15, textColour, "2nd")
- len = graphics.DrawText(offscreen_canvas, font, dep2_plat_loc, 15, textPlatformColour, dep2_platform)
- len = graphics.DrawText(offscreen_canvas, font, dep2_dest_loc, 15, textColour, dep2_dest)
- len = graphics.DrawText(offscreen_canvas, font, dep2_time_loc, 15, dep2_time_textColour, dep2_departureTime)
- len = graphics.DrawText(offscreen_canvas, font, 0, 23, textColour, "3rd")
- len = graphics.DrawText(offscreen_canvas, font, dep3_plat_loc, 23, textPlatformColour, dep3_platform)
- len = graphics.DrawText(offscreen_canvas, font, dep3_dest_loc, 23, textColour, dep3_dest)
- len = graphics.DrawText(offscreen_canvas, font, dep3_time_loc, 23, dep3_time_textColour, dep3_departureTime)
- len = graphics.DrawText(offscreen_canvas, font, 0, 31, textColour, "4th")
- len = graphics.DrawText(offscreen_canvas, font, dep4_plat_loc, 31, textPlatformColour, dep4_platform)
- len = graphics.DrawText(offscreen_canvas, font, dep4_dest_loc, 31, textColour, dep4_dest)
- len = graphics.DrawText(offscreen_canvas, font, dep4_time_loc, 31, dep4_time_textColour, dep4_departureTime)
- offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
- time.sleep(3)
- for calling_pos in range(192,end_pos,-1,):
- len = graphics.DrawText(calling_scroll, font, 0, 7, textColour, "1st")
- len = graphics.DrawText(calling_scroll, font, dep1_plat_loc, 7, textPlatformColour, dep1_platform)
- len = graphics.DrawText(calling_scroll, font, dep1_dest_loc, 7, textColour, dep1_dest)
- len = graphics.DrawText(calling_scroll, font, dep1_time_mins_loc, 7, dep1_time_textColour, str(dep1_time))
- len = graphics.DrawText(calling_scroll, font, 0, 23, textColour, "2nd")
- len = graphics.DrawText(calling_scroll, font, dep2_plat_loc, 23, textPlatformColour, dep2_platform)
- len = graphics.DrawText(calling_scroll, font, dep2_dest_loc, 23, textColour, dep2_dest)
- len = graphics.DrawText(calling_scroll, font, dep2_time_mins_loc, 23, dep2_time_textColour, str(dep2_time))
- len = graphics.DrawText(calling_scroll, font, 0, 31, textColour, "3rd")
- len = graphics.DrawText(calling_scroll, font, dep3_plat_loc, 31, textPlatformColour, dep3_platform)
- len = graphics.DrawText(calling_scroll, font, dep3_dest_loc, 31, textColour, dep3_dest)
- len = graphics.DrawText(calling_scroll, font, dep3_time_mins_loc, 31, dep3_time_textColour, str(dep3_time))
- #len = graphics.DrawText(offscreen_canvas, font, 82, 32, textColour, clock)
- len = graphics.DrawText(calling_scroll, font, 0, 15, textColour, "Calling at: ")
- len = graphics.DrawText(calling_scroll, font, calling_pos, 15, textColour, dep1_callingAt)
- calling_scroll = self.matrix.SwapOnVSync(calling_scroll)
- time.sleep(0.015)
- calling_scroll.Clear()
- # Main function
- if __name__ == "__main__":
- run_text = RunText()
- if (not run_text.process()):
- run_text.print_help()
Advertisement
Add Comment
Please, Sign In to add comment