1# SPDX-FileCopyrightText: 2019 Sommersoft
2# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
3#
4# SPDX-License-Identifier: Unlicense
5
6# Simple demo of reading and writing the time for the PCF8563 real-time clock.
7# Change the if False to if True below to set the time, otherwise it will just
8# print the current date and time every second. Notice also comments to adjust
9# for working with hardware vs. software I2C.
10
11import time
12import board
13import busio
14
15from adafruit_pcf8563.pcf8563 import PCF8563
16
17# Change to the appropriate I2C clock & data pins here!
18i2c_bus = busio.I2C(board.SCL, board.SDA)
19
20# Create the RTC instance:
21rtc = PCF8563(i2c_bus)
22
23# Lookup table for names of days (nicer printing).
24days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
25
26
27# pylint: disable-msg=using-constant-test
28if False: # change to True if you want to set the time!
29 # year, mon, date, hour, min, sec, wday, yday, isdst
30 t = time.struct_time((2017, 10, 29, 10, 31, 0, 0, -1, -1))
31 # you must set year, mon, date, hour, min, sec and weekday
32 # yearday is not supported, isdst can be set but we don't do anything with it at this time
33 print("Setting time to:", t) # uncomment for debugging
34 rtc.datetime = t
35 print()
36# pylint: enable-msg=using-constant-test
37
38
39# Main loop:
40while True:
41 if rtc.datetime_compromised:
42 print("RTC unset")
43 else:
44 print("RTC reports time is valid")
45 t = rtc.datetime
46 # print(t) # uncomment for debugging
47 print(
48 "The date is {} {}/{}/{}".format(
49 days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year
50 )
51 )
52 print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
53 time.sleep(1) # wait a second