Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory MagTag_Killed_By_Google/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

# 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, ConnectionError, OSError) as e:
        print("Some error occured, retrying! -", e)

Code Run Through

First, the code imports the required libraries.

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)

This guide was first published on Dec 16, 2020. It was last updated on Jun 22, 2024.

This page (Code the Google Graveyard) was last updated on Jun 22, 2024.

Text editor powered by tinymce.