secrets.py

First, make sure you've set up your secrets.py file has your accurate network info stored.

Text Editor

Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.

Alternatively, you can use any text editor that saves simple text files.

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_Cat_Fed_Clock/ 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:

CIRCUITPY
If you're having difficulty running this example, it could be because your MagTag CircuitPython firmware or library needs to be upgraded! Please be sure to follow https://learn.adafruit.com/adafruit-magtag/circuitpython to install the latest CircuitPython firmware and then also replace/update ALL the MagTag-specific libraries mentioned here https://learn.adafruit.com/adafruit-magtag/circuitpython-libraries-2
# SPDX-FileCopyrightText: 2020 Collin Cunningham for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
from adafruit_magtag.magtag import MagTag

USE_AMPM_TIME = True
weekdays = ("mon", "tue", "wed", "thur", "fri", "sat", "sun")
last_sync = None
last_minute = None

magtag = MagTag()

magtag.graphics.set_background("/background.bmp")

mid_x = magtag.graphics.display.width // 2 - 1
magtag.add_text(
    text_font="Lato-Regular-74.bdf",
    text_position=(mid_x,10),
    text_anchor_point=(0.5,0),
    is_data=False,
)
magtag.set_text("00:00a", auto_refresh = False)

magtag.add_text(
    text_font="/BebasNeueRegular-41.bdf",
    text_position=(126,86), #was 141
    text_anchor_point=(0,0),
    is_data=False,
)
magtag.set_text("DAY 00:00a", index = 1, auto_refresh = False)

def hh_mm(time_struct, twelve_hour=True):
    """ Given a time.struct_time, return a string as H:MM or HH:MM, either
        12- or 24-hour style depending on twelve_hour flag.
    """
    postfix = ""
    if twelve_hour:
        if time_struct.tm_hour > 12:
            hour_string = str(time_struct.tm_hour - 12) # 13-23 -> 1-11 (pm)
            postfix = "p"
        elif time_struct.tm_hour > 0:
            hour_string = str(time_struct.tm_hour) # 1-12
            postfix = "a"
            if time_struct.tm_hour == 12:
                postfix = "p" # 12 -> 12 (pm)
        else:
            hour_string = '12' # 0 -> 12 (am)
            postfix = "a"
    else:
        hour_string = '{hh:02d}'.format(hh=time_struct.tm_hour)
    return hour_string + ':{mm:02d}'.format(mm=time_struct.tm_min) + postfix

while True:
    if not last_sync or (time.monotonic() - last_sync) > 3600:
        # at start or once an hour
        magtag.network.get_local_time()
        last_sync = time.monotonic()

    # get current time
    now = time.localtime()

    # minute updated, refresh display!
    if not last_minute or (last_minute != now.tm_min):  # minute has updated
        magtag.set_text(hh_mm(now, USE_AMPM_TIME), index = 0)
        last_minute = now.tm_min

    # timestamp
    if magtag.peripherals.button_a_pressed:
        out = weekdays[now.tm_wday] + " " + hh_mm(now, USE_AMPM_TIME)
        magtag.set_text(out, index = 1)
        while magtag.peripherals.button_a_pressed: # wait till released
            pass

Usage

After copying the code to the MagTag, the board will restart. After a moment the board's display will refresh to show the current time.

To save a reference to the current time of cat feeding, press and release the left-most pushbutton (under the fork & knife icon).

How It Works

import time
from adafruit_magtag.magtag import MagTag

First off, we import code libraries to handle time formatting/calculation and the MagTag library for access to all the board's special features.

USE_AMPM_TIME = True
weekdays = ("mon", "tue", "wed", "thur", "fri", "sat", "sun")
last_sync = None
last_minute = None

magtag = MagTag()

Next, we define some variables. USE_AMPM_TIME determines if time will be displayed in 12 hour or 24 hour (aka military time) format. The weekdays variable stores names for days of the week.

The last_sync variable is defined to keep track of when the time was last synced with the adafruit.io server, and the last_minute variable keeps a reference to the last time the display was updated at a new minute.

Finally the magtag object is created for addressing the board.

magtag.graphics.set_background("/background.bmp")

mid_x = magtag.graphics.display.width // 2 - 1
magtag.add_text(
    text_font="Lato-Regular-74.bdf",
    text_position=(mid_x,10),
    text_anchor_point=(0.5,0),
    is_data=False,
)
magtag.set_text("00:00a", auto_refresh = False)

magtag.add_text(
    text_font="/BebasNeueRegular-41.bdf",
    text_position=(126,86), #was 141
    text_anchor_point=(0,0),
    is_data=False,
)
magtag.set_text("DAY 00:00a", index = 1, auto_refresh = False)

The graphics layout is composed of three parts:

  • set_background sets the bitmap image (with cat & fork/knife icons) as the base image on the display.
  • add_text is called to create the large main time display, then called again to create the smaller reference clock below.
def hh_mm(time_struct, twelve_hour=True):
    """ Given a time.struct_time, return a string as H:MM or HH:MM, either
        12- or 24-hour style depending on twelve_hour flag.
    """
    postfix = ""
    if twelve_hour:
        if time_struct.tm_hour > 12:
            hour_string = str(time_struct.tm_hour - 12) # 13-23 -> 1-11 (pm)
            postfix = "p"
        elif time_struct.tm_hour > 0:
            hour_string = str(time_struct.tm_hour) # 1-12
            postfix = "a"
        else:
            hour_string = '12' # 0 -> 12 (am)
            postfix = "a"
    else:
        hour_string = '{hh:02d}'.format(hh=time_struct.tm_hour)
    return hour_string + ':{mm:02d}'.format(mm=time_struct.tm_min) + postfix

Next, the hh_mm function is defined. This function takes a time_struct value and converts it into a time string formatted for display.

while(True):
    if not last_sync or (time.monotonic() - last_sync) > 3600:
        # at start or once an hour
        magtag.network.get_local_time()
        last_sync = time.monotonic()

In the main loop, we first check to see if it's time to sync local time w the adafruit.io server. This is done the first time the code is run, and once every hour after.

# get current time
    now = time.localtime()

    # minute updated, refresh display!
    if not last_minute or (last_minute != now.tm_min):  # minute has updated
        magtag.set_text(hh_mm(now, USE_AMPM_TIME), index = 0)
        last_minute = now.tm_min

Next we get the the local time as a time_struct and store it in the now variable.

If the minute value of now has changed, the main time display is updated, and a reference is saved in last_minute for comparison next time through the loop.

# timestamp
    if magtag.peripherals.button_a_pressed:
        out = weekdays[now.tm_wday] + " " + hh_mm(now, USE_AMPM_TIME)
        magtag.set_text(out, index = 1)
        while magtag.peripherals.button_a_pressed: # wait till released
            pass

Finally, the code checks to see if the A button is pressed. If it is, the smaller time display is updated using the hh_mm function, prefixed by the current weekday. If the button is held continuously, the code waits for it to be released.

This guide was first published on Dec 15, 2020. It was last updated on Dec 15, 2020.

This page (Code the MagTag Cat Fed Clock) was last updated on May 27, 2023.

Text editor powered by tinymce.