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_Christmas_Countdown/ 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: 2020 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import displayio
from adafruit_magtag.magtag import MagTag
from adafruit_display_shapes.circle import Circle

#  create MagTag and connect to network
try:
    magtag = MagTag()
    magtag.network.connect()
except (ConnectionError, ValueError, RuntimeError) as e:
    print("*** MagTag(), Some error occured, retrying! -", e)
    # Exit program and restart in 1 seconds.
    magtag.exit_and_deep_sleep(1)



#  displayio groups
group = displayio.Group()
tree_group = displayio.Group()
circle_group = displayio.Group()

#  import tree bitmap
filename = "/atree.bmp"

# CircuitPython 6 & 7 compatible
tree = displayio.OnDiskBitmap(open(filename, "rb"))
tree_grid = displayio.TileGrid(
    tree, pixel_shader=getattr(tree, 'pixel_shader', displayio.ColorConverter())
)

# # CircuitPython 7+ compatible
# tree = displayio.OnDiskBitmap(filename)
# tree_grid = displayio.TileGrid(tree, pixel_shader=tree.pixel_shader)

#  add bitmap to its group
tree_group.append(tree_grid)
#  add tree group to the main group
group.append(tree_group)

#  list of circle positions
spots = (
    (246, 53),
    (246, 75),
    (206, 42),
    (206, 64),
    (206, 86),
    (176, 31),
    (176, 53),
    (176, 75),
    (176, 97),
    (136, 42),
    (136, 64),
    (136, 86),
    (106, 31),
    (106, 53),
    (106, 75),
    (106, 97),
    (66, 31),
    (66, 53),
    (66, 75),
    (66, 97),
    (36, 20),
    (36, 42),
    (36, 64),
    (36, 86),
    (36, 108)
    )

#  circles to cover-up bitmap's number ornaments

ball_color = [0x555555, 0xaaaaaa, 0xFFFFFF] # All colors except black (0x000000)
ball_index = 0

#  creating the circles & pulling in positions from spots
for spot in spots:
    circle = Circle(x0=spot[0], y0=spot[1], r=11, fill=ball_color[ball_index]) # Each ball has a color
    ball_index += 1
    ball_index %= len(ball_color)

	#  adding circles to their display group
    circle_group.append(circle)

#  adding circles group to main display group
group.append(circle_group)

#  grabs time from network
magtag.get_local_time()
#  parses time into month, date, etc
now = time.localtime()
month = now[1]
day = now[2]
(hour, minutes, seconds) = now[3:6]
seconds_since_midnight = 60 * (hour*60 + minutes)+seconds
print( f"day is {day}, ({seconds_since_midnight} seconds since midnight)")


#  sets colors of circles to transparent to reveal dates that have passed & current date
for i in range(day):
    circle_group[i].fill = None
    time.sleep(0.1)

#  updates display with bitmap and current circle colors
magtag.display.root_group = group
magtag.display.refresh()
time.sleep(5)

#  goes into deep sleep till a 'stroke' past midnight
print("entering deep sleep")
seconds_to_sleep = 24*60*60 - seconds_since_midnight + 10
print( f"sleeping for {seconds_to_sleep} seconds")
magtag.exit_and_deep_sleep(seconds_to_sleep)

#  entire code will run again after deep sleep cycle
#  similar to hitting the reset button

Bitmap File

Copy atree.bmp from within the zip file downloaded in the last step to the CIRCUITPY main (root) directory.

Review

Make sure you've followed these steps:

  • Created a secrets.py file with your network WiFi info and Adafruit IO info and copied the file to the CIRCUITPY main (root) directory.
  • Loaded all the required library files and directories into the CIRCUITPY /lib directory
  • Copied atree.bmp to the main (root) directory of the CIRCUITPY drive
  • Copied code.py to the main (root) directory of the CIRCUITPY drive

This guide was first published on Dec 09, 2020. It was last updated on Mar 19, 2024.

This page (Coding the MagTag Christmas Countdown) was last updated on Mar 19, 2024.

Text editor powered by tinymce.