WiFi & NTP Time
The WiFi SSID and password are looked up from the settings.toml file using os.getenv()and a connection is made to the specified network. Once connected, the adafruit_ntp module is used to get the current time from an NTP server. The current time is set onto rtc.RTC().datetime so that the internal real-time clock (RTC) will continue to track the time moving forward.
The graphics are comprised of 3 main elements:
- A white background rendered behind all of the text. The background
Bitmapis 1/8 the size of the display and theTileGridfor it is put into aGroupthat has been scaled to 8x. This allows it to consume less RAM but still cover the full display. - The main quote label at the top of the display. This is a
BitmapLabelthat makes use of accented ranges within the text to outline the section that contains the reference to the current time. The quote is wrapped to lines that are the width of the display before being shown. - The book info label at the bottom of the display. Another
BitmapLabel, this one at the bottom of the display shows the book title and author for the current quote. This one will get wrapped to two lines if the title and author are long enough and the current quote is short enough to provide the room for it.
Helper Functions
There are two helper functions inside of code.py: which_line_contains() and find_lines_to_show().
which_line_contains() accepts a list of all_lines to search through, and an important_passage string that contains a value to search for. It will search the provided lines for the specified string and return the index of the line where the string begins. If the string isn't found at all, it will return None. This is used to find where within the full quote the section that references the current time is at.
find_lines_to_show() also accepts a list of all_lines and an important_passage. This function finds up to 7 lines surrounding the current time reference that provide the largest context. Some of the quotes are longer than 7 lines, which is all the room available on the MagTag display. This chooses the lines to show in the event that the quote is too long, ensuring that it will always select the line(s) that contain the current time reference and as much surrounding them as is possible.
Quote Data
The data files which contain the quotes are zipped .csv files. They are separated by the pipe character | instead of commas. That makes it easier to deal with potential commas that could be found in the quotes themselves. There is one zipped file for each hour of the day. They are named with the number of their hour e.g. 0.csv.zip, 1.csv.zip, ..., 23.csv.zip. After being unzipped the files contain 1 quote per row. The rows look like this:
00:00|Midnight|She looked at the digital display of her watch. 00:00:00 Midnight, as the clock had told her. She waited for the next second to arrive, but it didn't.|The Midnight Library|Matt Haig|unknown
The pipes split the row into these different data fields:
- Time of day in 24 hour notation i.e. 00:00 or 15:42.
- The sub-string within the quote that refers to the current time. Some times it's a word or phrase, other times it's a literal time string like "10:27 a.m.".
- The full quote.
- The title of the book that the quote comes from.
- The author of the book.
- Whether the quote contains non kid-friendly references, though many of them are "unknown", so this portion of the data is incomplete.
Main Loop
During each iteration of the main loop, the code will check the current time from the RTC and break out the current hour and minute components. It will use the hour component to select the correct data zip file, then open the file read its contents and decompress them using the zlib module. Next, it searches through the list of quotes in the data to find the ones that refer to the current time, building a list of options containing quotes to choose from. A list of alternates containing the quotes for the previous minutes is also compiled because there are a few minutes out of the day with no quote. When those occur, the clock will show a quote from the previous minute instead. A random quote is selected from the compiled lists.
The data from the selected quote is parsed out of the CSV format and each of the relevant bits is stored in a separate variable: quote, author, title, time_part. Using the helper functions mentioned above, the quote is wrapped into multiple lines that fit on the display and the lines to show are selected. The start and end index of the current time reference are calculated and stored in time_start_index and time_end_index. Finally, the selected lines are put into the quote label, and an accent range is added to the portion that refers to the current time. The accent uses a 1 pixel dark outline around light text for contrast from the standard dark text color used by the rest of the quote. The author and title are placed into the book info label. After refreshing the display, the code waits for 60 seconds and then does it all over again.
Page last edited December 10, 2025
Text editor powered by tinymce.