Last active
November 15, 2020 01:24
-
-
Save jameseleach/c791bec488238ce191436e65b2f39920 to your computer and use it in GitHub Desktop.
Simple Clock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import board | |
from adafruit_matrixportal.matrixportal import MatrixPortal | |
DEBUG = False | |
TIME24H = False | |
# --- Display setup --- | |
matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, bit_depth=4, debug=DEBUG, width=64, height=32) | |
# Create a new label with the color and text selected | |
matrixportal.add_text( | |
text_font="fonts/Dondo.bdf", | |
text_color="#FFFF00", | |
text_position=(0, (matrixportal.graphics.display.height // 2) - 1) | |
) | |
def update_time_display(*, hours=None, minutes=None): | |
now = time.localtime() | |
if hours is None: | |
hours = now.tm_hour | |
if not TIME24H and hours > 12: | |
hours -= 12 | |
if minutes is None: | |
minutes = now.tm_min | |
clock_text = "{hours}:{minutes:02d}".format(hours=hours, minutes=minutes) | |
matrixportal.set_text(clock_text) | |
bbw = matrixportal._text[0].bounding_box[2] | |
matrixportal._text[0].x = round(matrixportal.graphics.display.width / 2 - bbw / 2) | |
last_time_update = None | |
while True: | |
if last_time_update is None or time.monotonic() > last_time_update + 3600: | |
try: | |
update_time_display() | |
matrixportal.get_local_time() | |
last_time_update = time.monotonic() | |
except RuntimeError as e: | |
print("Error when updating time: ", e) | |
update_time_display() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment