Skip to content

Commit 3b18550

Browse files
committed
adding cartoon character clock
1 parent 8dab486 commit 3b18550

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
18.6 KB
Binary file not shown.
2.51 KB
Binary file not shown.
1.88 KB
Binary file not shown.

Cartoon_Character_Clock/code.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 time
13+
import board
14+
from adafruit_display_analogclock import AnalogClock
15+
from adafruit_gc9a01a import GC9A01A
16+
import rtc
17+
import socketpool
18+
import displayio
19+
from fourwire import FourWire
20+
import adafruit_ntp
21+
import wifi
22+
import os
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+
if HOURLY_CHIME or ALARM_TIME is not None:
55+
# Audio Setup
56+
audio = audiobusio.I2SOut(board.A2, board.A1, board.A0)
57+
songs = ["song_1.mp3", "song_2.mp3"]
58+
mp3 = open(songs[0], "rb")
59+
decoder = MP3Decoder(mp3)
60+
61+
# Display Setup
62+
spi = board.SPI()
63+
tft_cs = board.TX
64+
tft_dc = board.RX
65+
66+
displayio.release_displays()
67+
display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=None)
68+
display = GC9A01A(display_bus, width=240, height=240)
69+
display.rotation = 90
70+
71+
# Sync time from NTP server
72+
pool = socketpool.SocketPool(wifi.radio)
73+
ntp = adafruit_ntp.NTP(pool, server="time.nist.gov", tz_offset=0, cache_seconds=3600)
74+
rtc.RTC().datetime = ntp.datetime
75+
76+
# Initialize the clock face
77+
clockface = AnalogClock(
78+
"clock_short_hand.bmp",
79+
"clock_long_hand.bmp",
80+
(120, 120), 106, number_label_scale=2,
81+
background_color=0x989a97,
82+
background_img_file="clock_center.bmp",
83+
background_img_anchor_point=(0.5,0.5),
84+
background_img_anchored_position=(display.width//2, display.height//2 - 2)
85+
)
86+
87+
# set the clockface to show on the display
88+
display.root_group = clockface
89+
90+
print(f"current time {time.localtime().tm_hour + UTC_OFFSET}:{time.localtime().tm_min}")
91+
cur_hour = time.localtime().tm_hour + UTC_OFFSET
92+
cur_minute = time.localtime().tm_min
93+
94+
clockface.set_time(cur_hour, cur_minute)
95+
96+
while True:
97+
# If we need to update the clock hands
98+
if cur_hour != time.localtime().tm_hour + UTC_OFFSET or cur_minute != time.localtime().tm_min:
99+
# store current values to comapre with next iteration
100+
cur_hour = time.localtime().tm_hour + UTC_OFFSET
101+
cur_minute = time.localtime().tm_min
102+
103+
# update the clock face
104+
clockface.set_time(cur_hour, cur_minute)
105+
106+
# if the hourly chime is enabled, and it's the top of the hour
107+
if HOURLY_CHIME and cur_minute == 0:
108+
# play the hour chime jingle
109+
audio.play(decoder)
110+
while audio.playing:
111+
pass
112+
113+
# if the alarm is enabled and the current time is what
114+
# it was set to.
115+
if ALARM_TIME is not None and \
116+
cur_hour == ALARM_TIME[0] and cur_minute == ALARM_TIME[1]:
117+
118+
# open the alarm song file
119+
decoder.file = open("song_2.mp3", "rb")
120+
121+
# play the alarm song twice
122+
for i in range(2):
123+
audio.play(decoder)
124+
while audio.playing:
125+
pass
126+
time.sleep(0.5)
127+
128+
# re-open the hourly chime file
129+
decoder.file = open("song_1.mp3", "rb")
130+
131+
time.sleep(3)

0 commit comments

Comments
 (0)