|
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: 2024 Jose D. Montoya |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +# Simple demo of the PCF8523 real-time clock using a built-in display. |
| 7 | +import time |
| 8 | +import board |
| 9 | +from adafruit_display_text.bitmap_label import Label |
| 10 | +from terminalio import FONT |
| 11 | +from displayio import Group |
| 12 | +from adafruit_pcf8523.pcf8523 import PCF8523 |
| 13 | + |
| 14 | + |
| 15 | +# create a main_group to hold anything we want to show on the display. |
| 16 | +main_group = Group() |
| 17 | +# Initialize I2C bus and sensor. |
| 18 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 19 | +rtc = PCF8523(i2c) |
| 20 | + |
| 21 | +# Lookup table for names of days (nicer printing). |
| 22 | +days = ( |
| 23 | + "Monday", |
| 24 | + "Tuesday", |
| 25 | + "Wednesday", |
| 26 | + "Thursday", |
| 27 | + "Friday", |
| 28 | + "Saturday", |
| 29 | + "Sunday", |
| 30 | +) |
| 31 | + |
| 32 | +# Set the time |
| 33 | +t = time.struct_time((2024, 12, 12, 10, 31, 0, 0, -1, -1)) |
| 34 | +rtc.datetime = t |
| 35 | +print("Setting time to:", t) |
| 36 | + |
| 37 | +# Create two Labels to show the readings. If you have a very small |
| 38 | +# display you may need to change to scale=1. |
| 39 | +date_output_label = Label(FONT, text="", scale=2) |
| 40 | +time_output_label = Label(FONT, text="", scale=2) |
| 41 | + |
| 42 | +# place the label in the middle of the screen with anchored positioning |
| 43 | +date_output_label.anchor_point = (0, 0) |
| 44 | +date_output_label.anchored_position = (4, board.DISPLAY.height // 2) |
| 45 | +time_output_label.anchor_point = (0, 0) |
| 46 | +time_output_label.anchored_position = (4, 20 + board.DISPLAY.height // 2) |
| 47 | + |
| 48 | +# add the label to the main_group |
| 49 | +main_group.append(date_output_label) |
| 50 | +main_group.append(time_output_label) |
| 51 | + |
| 52 | +# set the main_group as the root_group of the built-in DISPLAY |
| 53 | +board.DISPLAY.root_group = main_group |
| 54 | + |
| 55 | +# begin main loop |
| 56 | +while True: |
| 57 | + # Update the label.text property to change the text on the display |
| 58 | + t = rtc.datetime |
| 59 | + date_output_label.text = ( |
| 60 | + f"The date is {days[int(t.tm_wday)]} {t.tm_mday}/{t.tm_mon}/{t.tm_year}" |
| 61 | + ) |
| 62 | + time_output_label.text = f"The time is {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}" |
| 63 | + # wait for a bit |
| 64 | + time.sleep(1) |
0 commit comments