Skip to content

Instantly share code, notes, and snippets.

@willwm
Created August 23, 2022 06:29
Show Gist options
  • Save willwm/6e46cfef03173ea813cd3080cbc32bd5 to your computer and use it in GitHub Desktop.
Save willwm/6e46cfef03173ea813cd3080cbc32bd5 to your computer and use it in GitHub Desktop.
MatrixPortal: Metro Matrix Clock (modified)
# SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Metro Matrix Clock
# Runs on Airlift Metro M4 with 64x32 RGB Matrix display & shield
import time
import board
import displayio
import terminalio
from adafruit_display_text.label import Label
from adafruit_bitmap_font import bitmap_font
from adafruit_matrixportal.network import Network
from adafruit_matrixportal.matrix import Matrix
BLINK = False
DEBUG = False
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print(" Metro Minimal Clock")
print("Time will be set for {}".format(secrets["timezone"]))
# --- Display setup ---
matrix = Matrix()
display = matrix.display
network = Network(status_neopixel=board.NEOPIXEL, debug=False)
# --- Drawing setup ---
group = displayio.Group() # Create a Group
bitmap = displayio.Bitmap(64, 32, 2) # Create a bitmap object,width, height, bit depth
color = displayio.Palette(4) # Create a color palette
color[0] = 0x000000 # black background
color[1] = 0xFF0000 # red
color[2] = 0xCC4000 # amber
color[3] = 0x85FF00 # greenish
# Create a TileGrid using the Bitmap and Palette
tile_grid = displayio.TileGrid(bitmap, pixel_shader=color)
group.append(tile_grid) # Add the TileGrid to the Group
display.show(group)
font = terminalio.FONT
clock_label = Label(font)
def update_time(*, hours=None, minutes=None, show_colon=False):
now = time.localtime() # Get the time values we need
if hours is None:
hours = now[3]
if hours >= 18 or hours < 6: # evening hours to morning
clock_label.color = color[2]
else:
clock_label.color = color[3] # daylight hours
if hours > 12: # Handle times later than 12:59
ampm = "pm"
hours -= 12
elif not hours: # Handle times between 0:00 and 0:59
ampm = "am"
hours = 12
else:
ampm = "am"
if minutes is None:
minutes = now[4]
if BLINK:
colon = ":" if show_colon or now[5] % 2 else " "
else:
colon = ":"
clock_label.text = "{hours}{colon}{minutes:02d} {ampm}\n{timezone}".format(
hours=hours, minutes=minutes, colon=colon, ampm=ampm, timezone=secrets["timezone"]
)
last_check = None
update_time(show_colon=True) # Display whatever time is on the board
group.append(clock_label) # add the clock label to the group
while True:
if last_check is None or time.monotonic() > last_check + 3600:
try:
update_time(
show_colon=True
) # Make sure a colon is displayed while updating
network.get_local_time() # Synchronize Board's clock to Internet
last_check = time.monotonic()
except RuntimeError as e:
print("Some error occured, retrying! -", e)
update_time()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment