Installing the Project Code
Download a zip of the project by clicking 'Download: Project Zip' in the preview of code.py below.
After unzipping the file, copy its contents to the CIRCUITPY drive which appears when the MagTag is connected to your computer via a USB cable and turned on via a small on/off switch onboard.
After you've copied everything over, this is what the CIRCUITPY drive should look like. Your lib folder should contain all the libraries on the the MagTag-Specific CircuitPython Libraries page.
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense import time import random import alarm import terminalio from adafruit_magtag.magtag import MagTag # Set up where we'll be fetching data from DATA_SOURCE = ( "https://raw.githubusercontent.com/codyogden/killedbygoogle/main/graveyard.json" ) # Get the MagTag ready MAGTAG = MagTag() MAGTAG.peripherals.neopixel_disable = True MAGTAG.set_background("/bmps/background.bmp") MAGTAG.network.connect() # Prepare the three text fields MAGTAG.add_text( text_font="/fonts/Deutsch-Gothic-14.bdf", text_position=(55, 60,), text_wrap=14, text_anchor_point=(0.5, 0.5), text_scale=1, line_spacing=0.9, is_data=False, ) MAGTAG.add_text( text_font="/fonts/Deutsch-Gothic-14.bdf", text_position=(55, 85,), text_anchor_point=(0.5, 0.5), text_scale=1, is_data=False, ) MAGTAG.add_text( text_font=terminalio.FONT, text_position=(115, 15,), text_wrap=29, text_anchor_point=(0, 0), text_scale=1, line_spacing=1.0, is_data=False, ) MAGTAG.preload_font() # preload characters SONG = ( (330, 1), (370, 1), (392, 2), (370, 2), (330, 2), (330, 1), (370, 1), (392, 1), (494, 1), (370, 1), (392, 1), (330, 2), ) while True: try: # Get the response and turn it into json RESPONSE = MAGTAG.network.requests.get(DATA_SOURCE) VALUE = RESPONSE.json() # Choose a random project to display PROJECT = VALUE[random.randint(0, len(VALUE) - 1)] # Prepare the text to be displayed CLOSED = PROJECT["dateClose"].split("-") CLOSED.reverse() CLOSED = "/".join(CLOSED) print(PROJECT["name"]) # Display the text MAGTAG.set_text(PROJECT["name"], 0, False) MAGTAG.set_text(CLOSED, 1, False) MAGTAG.set_text(PROJECT["description"], 2) # Play a song for notepair in SONG: MAGTAG.peripherals.play_tone(notepair[0], notepair[1] * 0.2) # Put the board to sleep for an hour time.sleep(2) print("Sleeping") PAUSE = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60 * 60) alarm.exit_and_deep_sleep_until_alarms(PAUSE) except (ValueError, RuntimeError) as e: print("Some error occured, retrying! -", e)
import time import random import alarm import terminalio from adafruit_magtag.magtag import MagTag
Then, we set the URL we will use to get the information to display on the ePaper display. After that, we initialize the MagTag object, disable the NeoPixels to save power, set the background image, and tell it to connect to the internet with the credentials we've added to secrets.py.
DATA_SOURCE = ( "https://raw.githubusercontent.com/codyogden/killedbygoogle/main/graveyard.json" ) # Get the MagTag ready MAGTAG = MagTag() MAGTAG.peripherals.neopixel_disable = True MAGTAG.set_background("/bmps/background.bmp") MAGTAG.network.connect()
Next, we set up the three areas we will be adding text to. We will later be able to identify these as an index corresponding to the order they were created in.
MAGTAG.add_text( text_font="/fonts/Deutsch-Gothic-14.bdf", text_position=(55, 60,), text_wrap=14, text_anchor_point=(0.5, 0.5), text_scale=1, line_spacing=0.9, is_data=False, ) MAGTAG.add_text( text_font="/fonts/Deutsch-Gothic-14.bdf", text_position=(55, 85,), text_anchor_point=(0.5, 0.5), text_scale=1, is_data=False, ) MAGTAG.add_text( text_font=terminalio.FONT, text_position=(115, 15,), text_wrap=29, text_anchor_point=(0, 0), text_scale=1, line_spacing=1.0, is_data=False, )
After that, we preload the font that will be displayed to save time later on, and define the tuple of tuples that we will be able to use to play the funeral dirge.
MAGTAG.preload_font() # preload characters SONG = ( (330, 1), (370, 1), (392, 2), (370, 2), (330, 2), (330, 1), (370, 1), (392, 1), (494, 1), (370, 1), (392, 1), (330, 2), )
Now, the program enters the main loop. Everything is put under try/except
statements so that the MagTag will automatically try to run the loop again if there is a ValueError
or RuntimeError
, which can occasionally happen when getting and parsing JSON.
The program will then get the response from the URL set above, convert it to JSON, and then choose a random project to display.
while True: try: # Get the response and turn it into json RESPONSE = MAGTAG.network.requests.get(DATA_SOURCE) VALUE = RESPONSE.json() # Choose a random project to display PROJECT = VALUE[random.randint(0, len(VALUE) - 1)]
Then, the date the project was closed is parsed to make it more readable, followed by printing the name of the project chosen, and setting all the text fields to the desired text.
CLOSED = PROJECT["dateClose"].split("-") CLOSED.reverse() CLOSED = "/".join(CLOSED) print(PROJECT["name"]) # Display the text MAGTAG.set_text(PROJECT["name"], 0, False) MAGTAG.set_text(CLOSED, 1, False) MAGTAG.set_text(PROJECT["description"], 2)
In this section, we play each note in the notepair
tuple that we defined above through the built-in speaker.
for notepair in SONG: MAGTAG.peripherals.play_tone(notepair[0], notepair[1] * 0.2)
After playing the dirge, the MagTag is put into deep sleep mode to save on power for an hour. If you want to change this time, just change how many seconds are added to time.monotonic()
in the third line in this section.
time.sleep(2) print("Sleeping") PAUSE = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60 * 60) alarm.exit_and_deep_sleep_until_alarms(PAUSE)
Finally, any errors that may have occured while getting the JSON and setting the text are caught, and the loop run again regardless of if there were any errors.
except (ValueError, RuntimeError) as e: print("Some error occured, retrying! -", e)