# 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 = 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(" 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.root_group = group if not DEBUG: font = bitmap_font.load_font("/IBMPlexMono-Medium-24_jep.bdf") else: 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[1] else: clock_label.color = color[3] # daylight hours if hours > 12: # Handle times later than 12:59 hours -= 12 elif not hours: # Handle times between 0:00 and 0:59 hours = 12 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}".format( hours=hours, minutes=minutes, colon=colon ) bbx, bby, bbwidth, bbh = clock_label.bounding_box # Center the label clock_label.x = round(display.width / 2 - bbwidth / 2) clock_label.y = display.height // 2 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 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)