Skip to content

Instantly share code, notes, and snippets.

@chadhutchins182
Created April 8, 2023 23:10
Show Gist options
  • Save chadhutchins182/e3bdf0f5ef3867d026248d73f8138233 to your computer and use it in GitHub Desktop.
Save chadhutchins182/e3bdf0f5ef3867d026248d73f8138233 to your computer and use it in GitHub Desktop.
adafruit Matrix Portal Multiple 64x32 LED panel wall clock supporting multiple time zones.
# 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
from time import time
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
from adafruit_datetime import datetime, timezone
from tzdb import timezone
BLINK = True
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(" Multi-Timezone Wall Clock")
print("Time will be set for {}".format(secrets["timezone"]))
# --- Display setup ---
base_width = 128
base_height = 32
chain_across = 1
tile_down = 1
width = base_width * chain_across
height = base_height * tile_down
matrix = Matrix(width=128, height=32, tile_rows=1)
display = matrix.display
network = Network(status_neopixel=board.NEOPIXEL, debug=False)
# --- Drawing setup ---
group = displayio.Group() # Create a Group
bitmap = displayio.Bitmap(128, 32, 2) # Create a bitmap object,width, height, bit depth
color = displayio.Palette(5) # Create a color palette
color[0] = 0x000000 # black background
color[1] = 0xFF0000 # red
color[2] = 0xCC4000 # amber
color[3] = 0x85FF00 # greenish
color[4] = 0x09ff00 # green
# 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)
if not DEBUG:
font = bitmap_font.load_font("/IBMPlexMono-Medium-14.bdf")
else:
font = terminalio.FONT
clock_label = Label(font)
#zone_label = Label(font, text="LOCAL UTC ", color=color[4])
#zone_label = Label(font, text="PACIFIC LOCAL UTC", color=color[4])
zone_label = Label(font, text="PACIFIC LOCAL ", color=color[4])
zone_label.y = 24
#zone_label.x = (round(display.width / 2 - zone_label.bounding_box[2] / 2))
zone_label.x = (round(128 / 2 - zone_label.bounding_box[2] / 2))
zone_label.scale = 1
def update_time(*, hours=None, minutes=None, seconds=None, show_colon=False):
currDate = datetime.now()
print("UTC Time: ", currDate)
print("CurrDate Offset: ", currDate.utcoffset())
utcHour = currDate.hour
utcMinute = currDate.minute
now = currDate + timezone('America/New_York').utcoffset(currDate)
# now = currDate
print("Eastern Time: ", now)
pacific = currDate + timezone('America/Los_Angeles').utcoffset(currDate)
print("Pacific Time: ", pacific)
#now = time.localtime()
if hours is None:
hours = now.hour
if minutes is None:
minutes = now.minute
if seconds is None:
seconds = now.second
clock_label.color = color[1]
hours2 = pacific.hour
colon = ":"
# clock_label.text = "{hours:02d}{colon}{minutes:02d}{colon}{seconds:02d}{hours2:02d}{colon}{minutes:02d}{colon}{seconds:02d}".format(
# hours=hours, hours2=hours2, minutes=minutes, seconds=seconds, colon=colon
# )
# clock_label.text = "{hours2:02d}{colon}{minutes:02d}{colon}{seconds:02d}{hours:02d}{colon}{minutes:02d}{colon}{seconds:02d}{utcHour:02d}{colon}{utcMinute:02d}{colon}{seconds:02d}".format(
# hours=hours, hours2=hours2, minutes=minutes, seconds=seconds, colon=colon, utcHour=utcHour, utcMinute=utcMinute
# )
clock_label.text = "{hours2:02d}{colon}{minutes:02d}{colon}{seconds:02d}{hours:02d}{colon}{minutes:02d}{colon}{seconds:02d}".format(
hours=hours, hours2=hours2, minutes=minutes, seconds=seconds, colon=colon
)
bbx, bby, bbwidth, bbh = clock_label.bounding_box
# Center the label
clock_label.x = round(128 / 2) - round(bbwidth / 2)
# clock_label.y = display.height // 2
clock_label.y = 10
if DEBUG:
print("Label bounding box: {},{},{},{}".format(bbx, bby, bbwidth, bbh))
print("Label x: {} y: {}".format(clock_label.x, clock_label.y))
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
group.append(zone_label) # add the zone label to the group
while True:
if last_check is None or time.monotonic() > last_check + 300000:
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