Guest User

Untitled

a guest
Apr 27th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.09 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Display a runtext with double-buffering.
  3. from samplebase import SampleBase
  4. from rgbmatrix import graphics
  5. import time
  6. import json
  7. import requests
  8.  
  9. from datetime import timedelta
  10. from timeloop import Timeloop
  11. from datetime import datetime
  12.  
  13. from trains import loadDeparturesForStation, loadDestinationsForDeparture
  14.  
  15. def QueryTrains():
  16.     # Parameters
  17.     app_id = "blablabla"
  18.     api_key = "blalblallalasdkudsjufjsdfksdkujfjfn"
  19.     station = "HYN"
  20.     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"
  21.     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"
  22.     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"
  23.  
  24.     #Query API
  25.     response = requests.get(glq_api_url)
  26.     data = response.json()
  27.  
  28.     return data
  29.  
  30. class RunText(SampleBase):
  31.     def __init__(self, *args, **kwargs):
  32.         super(RunText, self).__init__(*args, **kwargs)
  33.         self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!")
  34.  
  35.     def run(self):
  36.  
  37.         runs = 0
  38.  
  39.         while True:
  40.            
  41.             #print(runs)
  42.             print("run",runs)
  43.  
  44.  
  45.             if (runs == 0) or (runs >= 5):
  46.                 print("..Querying API for updated departures.")
  47.                 data = QueryTrains()
  48.  
  49.                 offscreen_canvas = self.matrix.CreateFrameCanvas()
  50.                 calling_scroll = self.matrix.CreateFrameCanvas()
  51.                 font = graphics.Font()
  52.                 font.LoadFont("fonts/6x10.bdf")
  53.                 largefont = graphics.Font()
  54.                 largefont.LoadFont("fonts/8x13.bdf")
  55.                 textColour = graphics.Color(255, 255, 0)
  56.                 textPlatformColour = graphics.Color(255, 255, 255)
  57.                 textTimeColour = graphics.Color(255,255,255)
  58.                 textTimeDueColour = graphics.Color(255,255,255)
  59.                 textTimeLateColour = graphics.Color(255,69,0)
  60.                 textTimeCancelledColour = graphics.Color(255,0,0)
  61.                 #pos = offscreen_canvas.width
  62.                 calling_pos = calling_scroll.width
  63.                 display_width = 192
  64.                 display_height = 32
  65.                 display_pwm_nano = 150
  66.                 dep1_plat_loc = 19
  67.                 dep2_plat_loc = 19
  68.                 dep3_plat_loc = 19
  69.                 dep4_plat_loc = 19
  70.                 dep1_time_loc = 163
  71.                 dep2_time_loc = 163
  72.                 dep3_time_loc = 163
  73.                 dep4_time_loc = 163
  74.                 dep1_time_mins_loc = 163
  75.                 dep2_time_mins_loc = 163
  76.                 dep3_time_mins_loc = 163
  77.                 dep4_time_mins_loc = 163
  78.  
  79.                 time_unit = "mins"
  80.                 my_text = self.args.text
  81.  
  82.  
  83.                 dep1_dest = data["departures"]["all"][0]["destination_name"]
  84.                 dep1_platform = data["departures"]["all"][0]["platform"]
  85.                 dep1_callingAt = "This service will call at Partick, Exhibition Centre, Anderston, Glasgow Central Low Level, High Street ..and wherever else, bla bla bla"
  86.                 dep1_departureTime = data["departures"]["all"][0]["expected_departure_time"]
  87.                 dep1_departureTimeMins = data["departures"]["all"][0]["best_departure_estimate_mins"]
  88.                 dep1_status = data["departures"]["all"][0]["status"]
  89.  
  90.                 dep2_dest = data["departures"]["all"][1]["destination_name"]
  91.                 dep2_platform = data["departures"]["all"][1]["platform"]
  92.                 dep2_departureTime = data["departures"]["all"][1]["expected_departure_time"]
  93.                 dep2_departureTimeMins = data["departures"]["all"][1]["best_departure_estimate_mins"]
  94.                 dep2_status = data["departures"]["all"][1]["status"]
  95.                
  96.                 dep3_dest = data["departures"]["all"][2]["destination_name"]
  97.                 dep3_platform = data["departures"]["all"][2]["platform"]
  98.                 dep3_departureTime = data["departures"]["all"][2]["expected_departure_time"]
  99.                 dep3_departureTimeMins = data["departures"]["all"][2]["best_departure_estimate_mins"]
  100.                 dep3_status = data["departures"]["all"][2]["status"]
  101.                
  102.                 dep4_dest = data["departures"]["all"][3]["destination_name"]
  103.                 dep4_platform = data["departures"]["all"][3]["platform"]
  104.                 dep4_departureTime = data["departures"]["all"][3]["expected_departure_time"]
  105.                 dep4_departureTimeMins = data["departures"]["all"][3]["best_departure_estimate_mins"]
  106.                 dep4_status = data["departures"]["all"][3]["status"]
  107.                
  108.                 clock = data["time_of_day"]
  109.  
  110.                 dep1_callingAt_width = dep1_callingAt
  111.  
  112.                 print("time ran:",clock)
  113.  
  114.                 start_pos = 0
  115.  
  116.                 for i in dep1_callingAt:
  117.                     start_pos = start_pos + 1
  118.  
  119.                 print("Total Number of Characters in the scroll message = ", start_pos)
  120.  
  121.                 end_pos = 0 - (20 + (start_pos * 6))
  122.  
  123.                 print("off-screen position required =",end_pos)
  124.  
  125.  
  126.                 if dep1_platform is None:
  127.                     dep1_platform = "_"
  128.                     dep1_dest_loc = 19
  129.                 if dep2_platform is None:
  130.                     dep2_platform = "_"
  131.                 if dep3_platform is None:
  132.                     dep3_platform = "_"
  133.                 if dep4_platform is None:
  134.                     dep4_platform = "_"
  135.  
  136.  
  137.                 # if dep1_dest is None:
  138.                 #     dep1_dest = "---No Train--"
  139.                 #     dep1_platform = "_"
  140.                 #     dep1_departureTime = "--:--"
  141.                 #     dep1_departureTimeMins = "--"
  142.                 #     dep1_status = "CANCELLED"
  143.  
  144.                 # if dep2_dest is None:
  145.                 #     dep2_dest = "---No Train--"
  146.                 #     dep2_platform = "_"
  147.                 #     dep2_departureTime = "--:--"
  148.                 #     dep2_departureTimeMins = "--"
  149.                 #     dep2_status = "CANCELLED"
  150.  
  151.                 # if dep3_dest is None:
  152.                 #     dep3_dest = "---No Train--"
  153.                 #     dep3_platform = "_"
  154.                 #     dep3_departureTime = "--:--"
  155.                 #     dep3_departureTimeMins = "--"
  156.                 #     dep3_status = "CANCELLED"
  157.  
  158.                 # if dep4_dest is None:
  159.                 #     dep4_dest = "---No Train--"
  160.                 #     dep4_platform = "_"
  161.                 #     dep4_departureTime = "--:--"
  162.                 #     dep4_departureTimeMins = "--"
  163.                 #     dep4_status = "CANCELLED"
  164.  
  165.  
  166.                 dep1_platform_chars = 0
  167.                 dep2_platform_chars = 0
  168.                 dep3_platform_chars = 0
  169.                 dep4_platform_chars = 0
  170.                  
  171.                 for i in dep1_platform:
  172.                     dep1_platform_chars = dep1_platform_chars + 1
  173.                 for i in dep2_platform:
  174.                     dep2_platform_chars = dep2_platform_chars + 1
  175.                 for i in dep3_platform:
  176.                     dep3_platform_chars = dep3_platform_chars + 1
  177.                 for i in dep4_platform:
  178.                     dep4_platform_chars = dep4_platform_chars + 1
  179.  
  180.                 if dep1_platform_chars == 1:
  181.                     dep1_dest_loc = 27
  182.                 elif dep1_platform_chars == 2:
  183.                     dep1_dest_loc = 32
  184.                 else: dep1_dest_loc = 37
  185.  
  186.                 if dep2_platform_chars == 1:
  187.                     dep2_dest_loc = 27
  188.                 elif dep2_platform_chars == 2:
  189.                     dep2_dest_loc = 32
  190.                 else: dep2_dest_loc = 37
  191.  
  192.                 if dep3_platform_chars == 1:
  193.                     dep3_dest_loc = 27
  194.                 elif dep3_platform_chars == 2:
  195.                     dep3_dest_loc = 32
  196.                 else: dep3_dest_loc = 37
  197.  
  198.                 if dep4_platform_chars == 1:
  199.                     dep4_dest_loc = 27
  200.                 elif dep4_platform_chars == 2:
  201.                     dep4_dest_loc = 32
  202.                 else: dep4_dest_loc = 37
  203.  
  204.  
  205.                 if (dep1_departureTimeMins == 0):
  206.                     dep1_time = "due"
  207.                     dep1_time_mins_loc = 175
  208.                     dep1_time_textColour = textTimeDueColour
  209.                 elif (dep1_departureTimeMins > 0 and dep1_departureTimeMins < 10):
  210.                     dep1_time = str(dep1_departureTimeMins) + "mins"
  211.                     dep1_time_mins_loc = 163
  212.                     dep1_time_textColour = textTimeColour
  213.                 elif (dep1_departureTimeMins > 9 and dep1_departureTimeMins < 100):
  214.                     dep1_time = str(dep1_departureTimeMins) + "mins"
  215.                     dep1_time_mins_loc = 157
  216.                     dep1_time_textColour = textTimeColour
  217.                 else: (dep1_time == "?")
  218.  
  219.                 if (dep2_departureTimeMins == 0):
  220.                     dep2_time = "due"
  221.                     dep2_time_mins_loc = 175
  222.                     dep2_time_textColour = textTimeDueColour
  223.                 elif (dep2_departureTimeMins > 0 and dep2_departureTimeMins < 10):
  224.                     dep2_time = str(dep2_departureTimeMins) + "mins"
  225.                     dep2_time_mins_loc = 163
  226.                     dep2_time_textColour = textTimeDueColour
  227.                 elif (dep2_departureTimeMins > 9 and dep2_departureTimeMins < 100):
  228.                     dep2_time = str(dep2_departureTimeMins) + "mins"
  229.                     dep2_time_mins_loc = 157
  230.                     dep2_time_textColour = textTimeDueColour
  231.  
  232.                 if (dep3_departureTimeMins == 0):
  233.                     dep3_time = "due"
  234.                     dep3_time_mins_loc = 175
  235.                     dep3_time_textColour = textTimeDueColour
  236.                 elif (dep3_departureTimeMins > 0 and dep3_departureTimeMins < 10):
  237.                     dep3_time = str(dep3_departureTimeMins) + "mins"
  238.                     dep3_time_mins_loc = 163
  239.                     dep3_time_textColour = textTimeDueColour
  240.                 elif (dep3_departureTimeMins > 9):
  241.                     dep3_time = str(dep3_departureTimeMins) + "mins"
  242.                     dep3_time_mins_loc = 157
  243.                     dep3_time_textColour = textTimeDueColour
  244.  
  245.                 if (dep4_departureTimeMins == 0):
  246.                     dep4_time = "due"
  247.                     dep4_time_mins_loc = 175
  248.                     dep4_time_textColour = textTimeDueColour
  249.                 elif (dep4_departureTimeMins > 0 and dep4_departureTimeMins < 10):
  250.                     dep4_time = str(dep4_departureTimeMins) + "mins"
  251.                     dep4_time_mins_loc = 163
  252.                     dep4_time_textColour = textTimeDueColour
  253.                 elif (dep4_departureTimeMins > 9):
  254.                     dep4_time = str(dep4_departureTimeMins) + "mins"
  255.                     dep4_time_mins_loc = 157
  256.                     dep4_time_textColour = textTimeDueColour
  257.  
  258.  
  259.  
  260.                 if (dep1_status == "LATE"):
  261.                     dep1_time_textColour = textTimeLateColour
  262.                 elif (dep1_status == "EARLY"):
  263.                     dep1_time_textColour = textTimeColour
  264.                 elif (dep1_status == "CANCELLED"):
  265.                     dep1_time_textColour = textTimeCancelledColour
  266.                     dep1_departureTime = "cancelled"
  267.                     dep1_time = "cancelled"
  268.                     dep1_time_loc = 139
  269.                     dep1_time_mins_loc = 139
  270.  
  271.                 if (dep2_status == "LATE"):
  272.                     dep2_time_textColour = textTimeLateColour
  273.                 elif (dep2_status == "EARLY"):
  274.                     dep2_time_textColour = textTimeColour
  275.                 elif (dep2_status == "CANCELLED"):
  276.                     dep2_time_textColour = textTimeCancelledColour
  277.                     dep2_departureTime = "cancelled"
  278.                     dep2_time = "cancelled"
  279.                     dep2_time_loc = 139
  280.                     dep2_time_mins_loc = 139
  281.  
  282.                 if (dep3_status == "LATE"):
  283.                     dep3_time_textColour = textTimeLateColour
  284.                 elif (dep3_status == "EARLY"):
  285.                     dep3_time_textColour = textTimeColour
  286.                 elif (dep3_status == "CANCELLED"):
  287.                     dep3_time_textColour = textTimeCancelledColour
  288.                     dep3_departureTime = "cancelled"
  289.                     dep3_time = "cancelled"
  290.                     dep3_time_loc = 139
  291.                     dep3_time_mins_loc = 139
  292.  
  293.                 if (dep4_status == "LATE"):
  294.                     dep4_time_textColour = textTimeLateColour
  295.                 elif (dep4_status == "EARLY"):
  296.                     dep4_time_textColour = textTimeColour
  297.                 elif (dep4_status == "CANCELLED"):
  298.                     dep4_time_textColour = textTimeCancelledColour
  299.                     dep4_departureTime = "cancelled"
  300.                     dep4_time = "cancelled"
  301.                     dep4_time_loc = 139
  302.                     dep4_time_mins_loc = 139
  303.  
  304.  
  305.                 runs = 1
  306.  
  307.  
  308.             else:
  309.                 print("Refresh limit reached, waiting...")
  310.                 runs = runs + 1
  311.  
  312.  
  313.  
  314.  
  315.             offscreen_canvas.Clear()
  316.             len = graphics.DrawText(offscreen_canvas, font, 0, 7, textColour, "1st")
  317.             len = graphics.DrawText(offscreen_canvas, font, dep1_plat_loc, 7, textPlatformColour, dep1_platform)
  318.             len = graphics.DrawText(offscreen_canvas, font, dep1_dest_loc, 7, textColour, dep1_dest)
  319.             len = graphics.DrawText(offscreen_canvas, font, dep1_time_loc, 7, dep1_time_textColour, dep1_departureTime)
  320.  
  321.             len = graphics.DrawText(offscreen_canvas, font, 0, 15, textColour, "2nd")
  322.             len = graphics.DrawText(offscreen_canvas, font, dep2_plat_loc, 15, textPlatformColour, dep2_platform)
  323.             len = graphics.DrawText(offscreen_canvas, font, dep2_dest_loc, 15, textColour, dep2_dest)
  324.             len = graphics.DrawText(offscreen_canvas, font, dep2_time_loc, 15, dep2_time_textColour, dep2_departureTime)
  325.  
  326.             len = graphics.DrawText(offscreen_canvas, font, 0, 23, textColour, "3rd")
  327.             len = graphics.DrawText(offscreen_canvas, font, dep3_plat_loc, 23, textPlatformColour, dep3_platform)
  328.             len = graphics.DrawText(offscreen_canvas, font, dep3_dest_loc, 23, textColour, dep3_dest)
  329.             len = graphics.DrawText(offscreen_canvas, font, dep3_time_loc, 23, dep3_time_textColour, dep3_departureTime)
  330.  
  331.             len = graphics.DrawText(offscreen_canvas, font, 0, 31, textColour, "4th")
  332.             len = graphics.DrawText(offscreen_canvas, font, dep4_plat_loc, 31, textPlatformColour, dep4_platform)
  333.             len = graphics.DrawText(offscreen_canvas, font, dep4_dest_loc, 31, textColour, dep4_dest)
  334.             len = graphics.DrawText(offscreen_canvas, font, dep4_time_loc, 31, dep4_time_textColour, dep4_departureTime)
  335.  
  336.  
  337.             offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
  338.             time.sleep(3)
  339.  
  340.  
  341.             for calling_pos in range(192,end_pos,-1,):
  342.  
  343.                 len = graphics.DrawText(calling_scroll, font, 0, 7, textColour, "1st")
  344.                 len = graphics.DrawText(calling_scroll, font, dep1_plat_loc, 7, textPlatformColour, dep1_platform)
  345.                 len = graphics.DrawText(calling_scroll, font, dep1_dest_loc, 7, textColour, dep1_dest)
  346.                 len = graphics.DrawText(calling_scroll, font, dep1_time_mins_loc, 7, dep1_time_textColour, str(dep1_time))
  347.  
  348.                 len = graphics.DrawText(calling_scroll, font, 0, 23, textColour, "2nd")
  349.                 len = graphics.DrawText(calling_scroll, font, dep2_plat_loc, 23, textPlatformColour, dep2_platform)
  350.                 len = graphics.DrawText(calling_scroll, font, dep2_dest_loc, 23, textColour, dep2_dest)
  351.                 len = graphics.DrawText(calling_scroll, font, dep2_time_mins_loc, 23, dep2_time_textColour, str(dep2_time))
  352.  
  353.                 len = graphics.DrawText(calling_scroll, font, 0, 31, textColour, "3rd")
  354.                 len = graphics.DrawText(calling_scroll, font, dep3_plat_loc, 31, textPlatformColour, dep3_platform)
  355.                 len = graphics.DrawText(calling_scroll, font, dep3_dest_loc, 31, textColour, dep3_dest)
  356.                 len = graphics.DrawText(calling_scroll, font, dep3_time_mins_loc, 31, dep3_time_textColour, str(dep3_time))
  357.  
  358.  
  359.                 #len = graphics.DrawText(offscreen_canvas, font, 82, 32, textColour, clock)
  360.  
  361.                
  362.                 len = graphics.DrawText(calling_scroll, font, 0, 15, textColour, "Calling at: ")
  363.  
  364.                 len = graphics.DrawText(calling_scroll, font, calling_pos, 15, textColour, dep1_callingAt)
  365.                 calling_scroll = self.matrix.SwapOnVSync(calling_scroll)
  366.                 time.sleep(0.015)
  367.                 calling_scroll.Clear()
  368.  
  369.  
  370.  
  371. # Main function
  372. if __name__ == "__main__":
  373.     run_text = RunText()
  374.     if (not run_text.process()):
  375.         run_text.print_help()
  376.  
Advertisement
Add Comment
Please, Sign In to add comment