/ MagTag_SpaceX / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Anne Barela for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # SpaceX Launch Display, by Anne Barela November 2020 6 # MIT License - for Adafruit Industries LLC 7 # See https://github.com/r-spacex/SpaceX-API for API info 8 9 import time 10 import terminalio 11 from adafruit_magtag.magtag import MagTag 12 13 months = ["January", "February", "March", "April", "May", "June", "July", 14 "August", "September", "October", "November", "December"] 15 USE_24HR_TIME = True 16 # in seconds, we can refresh about 100 times on a battery 17 TIME_BETWEEN_REFRESHES = 24 * 60 * 60 # once a day delay 18 19 # Set up data location and fields 20 DATA_SOURCE = "https://api.spacexdata.com/v4/launches/next" 21 DETAIL_LOCATION = ['details'] 22 NAME_LOCATION = ['name'] 23 DATE_LOCATION = ['date_local'] 24 25 # These functions take the JSON data keys and does checks to determine 26 # how to display the data. They're used in the add_text blocks below 27 28 def mission_transform(val): 29 if val == None: 30 val = "Unavailable" 31 return "Mission: " + val 32 33 def time_transform(val2): 34 if val2 == None: 35 return "When: Unavailable" 36 month = int(val2[5:7]) 37 day = int(val2[8:10]) 38 hour = int(val2[11:13]) 39 min = int(val2[14:16]) 40 41 if USE_24HR_TIME: 42 timestring = "%d:%02d" % (hour, min) 43 elif hour > 12: 44 timestring = "%d:%02d pm" % (hour-12, min) 45 else: 46 timestring = "%d:%02d am" % (hour, min) 47 48 return "%s %d, at %s" % (months[month-1], day, timestring) 49 50 def details_transform(val3): 51 if val3 == None or not len(val3): 52 return "Details: To Be Determined" 53 return "Details: " + val3[0:166] + "..." 54 55 # Set up the MagTag with the JSON data parameters 56 magtag = MagTag( 57 url=DATA_SOURCE, 58 json_path=(NAME_LOCATION, DATE_LOCATION, DETAIL_LOCATION) 59 ) 60 61 magtag.add_text( 62 text_font="/fonts/Lato-Bold-ltd-25.bdf", 63 text_position=(10, 15), 64 is_data=False 65 ) 66 # Display heading text below with formatting above 67 magtag.set_text("Next SpaceX Launch") 68 69 # Formatting for the mission text 70 magtag.add_text( 71 text_font="/fonts/Arial-Bold-12.pcf", 72 text_position=(10, 38), 73 text_transform=mission_transform 74 ) 75 76 # Formatting for the launch time text 77 magtag.add_text( 78 text_font="/fonts/Arial-12.bdf", 79 text_position=(10, 60), 80 text_transform=time_transform 81 ) 82 83 # Formatting for the details text 84 magtag.add_text( 85 text_font=terminalio.FONT, 86 text_position=(10, 94), 87 line_spacing=0.8, 88 text_wrap=47, # wrap text at this count 89 text_transform=details_transform 90 ) 91 92 try: 93 # Have the MagTag connect to the internet 94 magtag.network.connect() 95 # This statement gets the JSON data and displays it automagically 96 value = magtag.fetch() 97 print("Response is", value) 98 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 99 print("Some error occured, retrying! -", e) 100 101 # wait 2 seconds for display to complete 102 time.sleep(2) 103 magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES)