|
| 1 | +# SPDX-FileCopyrightText: 2025 Tim Cocks |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +Cartoon Character Clock |
| 6 | +
|
| 7 | +This project features an analog clock face rendered on a round display. |
| 8 | +The art and songs are inspired by an early 20th-century public domain |
| 9 | +cartoon. |
| 10 | +
|
| 11 | +""" |
| 12 | +import os |
| 13 | +import time |
| 14 | +import board |
| 15 | +from adafruit_display_analogclock import AnalogClock |
| 16 | +from adafruit_gc9a01a import GC9A01A |
| 17 | +import rtc |
| 18 | +import socketpool |
| 19 | +import displayio |
| 20 | +from fourwire import FourWire |
| 21 | +import adafruit_ntp |
| 22 | +import wifi |
| 23 | +import audiobusio |
| 24 | +from audiomp3 import MP3Decoder |
| 25 | + |
| 26 | +# Set the desired offset from UTC time here. |
| 27 | +# US Eastern Standard Time is -5 |
| 28 | +# US Eastern Daylight Time is -4 |
| 29 | +UTC_OFFSET = -5 |
| 30 | + |
| 31 | +# enable or disable the hourly chime jingle |
| 32 | +HOURLY_CHIME = True |
| 33 | + |
| 34 | +# Set to a tuple containing hour and minute values to |
| 35 | +# enable the alarm e.g. 13, 25 will play the alarm at |
| 36 | +# 1:25 pm adjusted for the given UTC_OFFSET. |
| 37 | +ALARM_TIME = None |
| 38 | +#ALARM_TIME = (13, 25) |
| 39 | + |
| 40 | +# WiFi Setup |
| 41 | +# Get wifi AP credentials from a settings.toml file |
| 42 | +wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID") |
| 43 | +wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 44 | +if wifi_ssid is None: |
| 45 | + print("WiFi credentials are kept in settings.toml, please add them there!") |
| 46 | + raise ValueError("SSID not found in environment variables") |
| 47 | + |
| 48 | +try: |
| 49 | + wifi.radio.connect(wifi_ssid, wifi_password) |
| 50 | +except ConnectionError: |
| 51 | + print("Failed to connect to WiFi with provided credentials") |
| 52 | + raise |
| 53 | + |
| 54 | +# pylint: disable=unsubscriptable-object |
| 55 | + |
| 56 | +if HOURLY_CHIME or ALARM_TIME is not None: |
| 57 | + # Audio Setup |
| 58 | + audio = audiobusio.I2SOut(board.A2, board.A1, board.A0) |
| 59 | + songs = ["song_1.mp3", "song_2.mp3"] |
| 60 | + mp3 = open(songs[0], "rb") |
| 61 | + decoder = MP3Decoder(mp3) |
| 62 | + |
| 63 | +# Display Setup |
| 64 | +spi = board.SPI() |
| 65 | +tft_cs = board.TX |
| 66 | +tft_dc = board.RX |
| 67 | + |
| 68 | +displayio.release_displays() |
| 69 | +display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=None) |
| 70 | +display = GC9A01A(display_bus, width=240, height=240) |
| 71 | +display.rotation = 90 |
| 72 | + |
| 73 | +# Sync time from NTP server |
| 74 | +pool = socketpool.SocketPool(wifi.radio) |
| 75 | +ntp = adafruit_ntp.NTP(pool, server="time.nist.gov", tz_offset=0, cache_seconds=3600) |
| 76 | +rtc.RTC().datetime = ntp.datetime |
| 77 | + |
| 78 | +# Initialize the clock face |
| 79 | +clockface = AnalogClock( |
| 80 | + "clock_short_hand.bmp", |
| 81 | + "clock_long_hand.bmp", |
| 82 | + (120, 120), 106, number_label_scale=2, |
| 83 | + background_color=0x989a97, |
| 84 | + background_img_file="clock_center.bmp", |
| 85 | + background_img_anchor_point=(0.5,0.5), |
| 86 | + background_img_anchored_position=(display.width//2, display.height//2 - 2) |
| 87 | +) |
| 88 | + |
| 89 | +# set the clockface to show on the display |
| 90 | +display.root_group = clockface |
| 91 | + |
| 92 | +print(f"current time {time.localtime().tm_hour + UTC_OFFSET}:{time.localtime().tm_min}") |
| 93 | +cur_hour = time.localtime().tm_hour + UTC_OFFSET |
| 94 | +cur_minute = time.localtime().tm_min |
| 95 | + |
| 96 | +clockface.set_time(cur_hour, cur_minute) |
| 97 | + |
| 98 | +while True: |
| 99 | + # If we need to update the clock hands |
| 100 | + if cur_hour != time.localtime().tm_hour + UTC_OFFSET or cur_minute != time.localtime().tm_min: |
| 101 | + # store current values to comapre with next iteration |
| 102 | + cur_hour = time.localtime().tm_hour + UTC_OFFSET |
| 103 | + cur_minute = time.localtime().tm_min |
| 104 | + |
| 105 | + # update the clock face |
| 106 | + clockface.set_time(cur_hour, cur_minute) |
| 107 | + |
| 108 | + # if the hourly chime is enabled, and it's the top of the hour |
| 109 | + if HOURLY_CHIME and cur_minute == 0: |
| 110 | + # play the hour chime jingle |
| 111 | + audio.play(decoder) |
| 112 | + while audio.playing: |
| 113 | + pass |
| 114 | + |
| 115 | + # if the alarm is enabled and the current time is what |
| 116 | + # it was set to. |
| 117 | + if ALARM_TIME is not None and \ |
| 118 | + cur_hour == ALARM_TIME[0] and cur_minute == ALARM_TIME[1]: |
| 119 | + |
| 120 | + # open the alarm song file |
| 121 | + decoder.file = open("song_2.mp3", "rb") |
| 122 | + |
| 123 | + # play the alarm song twice |
| 124 | + for i in range(2): |
| 125 | + audio.play(decoder) |
| 126 | + while audio.playing: |
| 127 | + pass |
| 128 | + time.sleep(0.5) |
| 129 | + |
| 130 | + # re-open the hourly chime file |
| 131 | + decoder.file = open("song_1.mp3", "rb") |
| 132 | + |
| 133 | + time.sleep(3) |
0 commit comments