Last active
January 7, 2021 20:29
-
-
Save hernan43/b38e05f7503f8b6e16cce6aac965e262 to your computer and use it in GitHub Desktop.
MagTag Rocket launch tracker using RocketLaunch API
This file contains hidden or 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
# SpaceX Launch Display, by Anne Barela November 2020 | |
# MIT License - for Adafruit Industries LLC | |
# See https://github.com/r-spacex/SpaceX-API for API info | |
import time | |
import terminalio | |
from adafruit_magtag.magtag import MagTag | |
months = ["January", "February", "March", "April", "May", "June", "July", | |
"August", "September", "October", "November", "December"] | |
USE_24HR_TIME = True | |
# in seconds, we can refresh about 100 times on a battery | |
TIME_BETWEEN_REFRESHES = 24 * 60 * 60 # once a day delay | |
# Set up data location and fields | |
DATA_SOURCE = "https://fdo.rocketlaunch.live/json/launches/next/1" | |
DETAIL_LOCATION = ['result', 0, 'quicktext'] | |
NAME_LOCATION = ['result', 0, 'name'] | |
DATE_LOCATION = ['result', 0, 'sort_date'] | |
# These functions take the JSON data keys and does checks to determine | |
# how to display the data. They're used in the add_text blocks below | |
def mission_transform(val): | |
if val == None: | |
val = "Unavailable" | |
return "Mission: " + str(val) | |
def time_transform(val2): | |
if val2 == None: | |
return "When: Unavailable" | |
# the -18000 is a jank timezone conversion | |
launch_time = time.localtime(int(val2)-18000) | |
month = launch_time[1] | |
day = launch_time[2] | |
hour = launch_time[3] | |
min = launch_time[4] | |
if USE_24HR_TIME: | |
timestring = "%d:%02d" % (hour, min) | |
elif hour > 12: | |
timestring = "%d:%02d pm" % (hour-12, min) | |
else: | |
timestring = "%d:%02d am" % (hour, min) | |
return "%s %d, at %s EST" % (months[month-1], day, timestring) | |
def details_transform(val3): | |
if val3 == None or not len(val3): | |
return "Details: To Be Determined" | |
return "Details: " + val3[0:220] + "..." | |
# Set up the MagTag with the JSON data parameters | |
magtag = MagTag( | |
url=DATA_SOURCE, | |
json_path=(NAME_LOCATION, DATE_LOCATION, DETAIL_LOCATION) | |
) | |
# turn the things off | |
magtag.peripherals.neopixel_disable = True | |
magtag.peripherals.speaker_disable = True | |
magtag.add_text( | |
text_font="/fonts/Lato-Bold-ltd-25.bdf", | |
text_position=(10, 15), | |
is_data=False | |
) | |
# Display heading text below with formatting above | |
magtag.set_text("Next Rocket Launch") | |
# Formatting for the mission text | |
magtag.add_text( | |
text_font="/fonts/Arial-Bold-12.pcf", | |
text_position=(10, 38), | |
text_transform=mission_transform | |
) | |
# Formatting for the launch time text | |
magtag.add_text( | |
text_font="/fonts/Arial-12.bdf", | |
text_position=(10, 60), | |
text_transform=time_transform | |
) | |
# Formatting for the details text | |
magtag.add_text( | |
text_font=terminalio.FONT, | |
text_position=(10, 94), | |
line_spacing=0.8, | |
text_wrap=47, # wrap text at this count | |
text_transform=details_transform | |
) | |
try: | |
# Have the MagTag connect to the internet | |
magtag.network.connect() | |
# This statement gets the JSON data and displays it automagically | |
value = magtag.fetch() | |
print("Response is", value) | |
except (ValueError, RuntimeError) as e: | |
print("Some error occured, retrying! -", e) | |
# wait 2 seconds for display to complete | |
time.sleep(2) | |
magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment