Skip to content

Commit 3bfc11d

Browse files
authored
Merge pull request #2858 from adafruit/cp_day
circuitpython day countdowns
2 parents 7d8fd34 + 1b3480b commit 3bfc11d

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed
Binary file not shown.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import displayio
7+
import picodvi
8+
import board
9+
import framebufferio
10+
from adafruit_bitmap_font import bitmap_font
11+
from adafruit_display_text import label
12+
from adafruit_pcf8523.pcf8523 import PCF8523
13+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
14+
15+
EVENT_YEAR = 2024
16+
EVENT_MONTH = 8
17+
EVENT_DAY = 16
18+
EVENT_HOUR = 0
19+
EVENT_MINUTE = 0
20+
# we'll make a python-friendly structure
21+
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY,
22+
EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds
23+
-1, -1, False)) # we dont know day of week/year or DST
24+
25+
# check for DVI Feather with built-in display
26+
if 'DISPLAY' in dir(board):
27+
display = board.DISPLAY
28+
29+
# check for DVI feather without built-in display
30+
elif 'CKP' in dir(board):
31+
displayio.release_displays()
32+
fb = picodvi.Framebuffer(320, 240,
33+
clk_dp=board.CKP, clk_dn=board.CKN,
34+
red_dp=board.D0P, red_dn=board.D0N,
35+
green_dp=board.D1P, green_dn=board.D1N,
36+
blue_dp=board.D2P, blue_dn=board.D2N,
37+
color_depth=8)
38+
display = framebufferio.FramebufferDisplay(fb)
39+
# otherwise assume Pico
40+
else:
41+
displayio.release_displays()
42+
fb = picodvi.Framebuffer(320, 240,
43+
clk_dp=board.GP12, clk_dn=board.GP13,
44+
red_dp=board.GP10, red_dn=board.GP11,
45+
green_dp=board.GP8, green_dn=board.GP9,
46+
blue_dp=board.GP6, blue_dn=board.GP7,
47+
color_depth=8)
48+
display = framebufferio.FramebufferDisplay(fb)
49+
50+
i2c = board.I2C()
51+
rtc = PCF8523(i2c)
52+
set_clock = False
53+
if set_clock:
54+
# year, mon, date, hour, min, sec, wday, yday, isdst
55+
t = time.struct_time((2024, 8, 1, 16, 26, 00, 0, -1, -1))
56+
print("Setting time to:", t)
57+
rtc.datetime = t
58+
print()
59+
# variable to hold RTC datetime
60+
t = rtc.datetime
61+
62+
pink = 0xf1078e
63+
purple = 0x673192
64+
aqua = 0x19beed
65+
group = displayio.Group()
66+
my_font = bitmap_font.load_font("/Helvetica-Bold-16.pcf")
67+
clock_area = label.Label(my_font, text="00:00:00:00", color=pink)
68+
clock_area.anchor_point = (0.0, 1.0)
69+
clock_area.anchored_position = (display.width / 2 - clock_area.width / 2,
70+
display.height - (clock_area.height + 20))
71+
text1 = label.Label(my_font, text="Starting In:", color=aqua)
72+
text1.anchor_point = (0.0, 0.0)
73+
text1.anchored_position = (display.width / 2 - text1.width / 2,
74+
display.height - (clock_area.height + text1.height + 35))
75+
76+
blinka_bitmap = displayio.OnDiskBitmap("/cpday_dvi.bmp")
77+
blinka_grid = displayio.TileGrid(blinka_bitmap, pixel_shader=blinka_bitmap.pixel_shader)
78+
group.append(blinka_grid)
79+
group.append(text1)
80+
group.append(clock_area)
81+
display.root_group = group
82+
83+
clock_clock = ticks_ms()
84+
clock_timer = 1000
85+
while True:
86+
if ticks_diff(ticks_ms(), clock_clock) >= clock_timer:
87+
t = rtc.datetime
88+
remaining = time.mktime(event_time) - time.mktime(t)
89+
secs_remaining = remaining % 60
90+
remaining //= 60
91+
mins_remaining = remaining % 60
92+
remaining //= 60
93+
hours_remaining = remaining % 24
94+
remaining //= 24
95+
days_remaining = remaining
96+
clock_area.text = (f"{days_remaining:0>2}:{hours_remaining:0>2}" +
97+
f":{mins_remaining:0>2}:{secs_remaining:0>2}")
98+
clock_clock = ticks_add(clock_clock, clock_timer)
Binary file not shown.
Binary file not shown.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os
6+
import time
7+
import wifi
8+
import board
9+
import displayio
10+
import socketpool
11+
import microcontroller
12+
from adafruit_bitmap_font import bitmap_font
13+
from adafruit_display_text import bitmap_label
14+
import adafruit_ntp
15+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
16+
17+
timezone = -4
18+
# The time of the thing!
19+
EVENT_YEAR = 2024
20+
EVENT_MONTH = 8
21+
EVENT_DAY = 16
22+
EVENT_HOUR = 0
23+
EVENT_MINUTE = 0
24+
# we'll make a python-friendly structure
25+
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY,
26+
EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds
27+
-1, -1, False)) # we dont know day of week/year or DST
28+
29+
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
30+
pool = socketpool.SocketPool(wifi.radio)
31+
ntp = adafruit_ntp.NTP(pool, tz_offset=timezone, cache_seconds=3600)
32+
33+
display = board.DISPLAY
34+
group = displayio.Group()
35+
font = bitmap_font.load_font("/Helvetica-Bold-16.pcf")
36+
blinka_bitmap = displayio.OnDiskBitmap("/cpday_tft.bmp")
37+
blinka_grid = displayio.TileGrid(blinka_bitmap, pixel_shader=blinka_bitmap.pixel_shader)
38+
scrolling_label = bitmap_label.Label(font, text=" ", y=display.height - 15)
39+
40+
group.append(blinka_grid)
41+
group.append(scrolling_label)
42+
display.root_group = group
43+
display.auto_refresh = False
44+
45+
refresh_clock = ticks_ms()
46+
refresh_timer = 3600 * 1000
47+
clock_clock = ticks_ms()
48+
clock_timer = 1000
49+
scroll_clock = ticks_ms()
50+
scroll_timer = 50
51+
first_run = True
52+
53+
while True:
54+
# only query the online time once per hour (and on first run)
55+
if ticks_diff(ticks_ms(), refresh_clock) >= refresh_timer or first_run:
56+
try:
57+
print("Getting time from internet!")
58+
now = ntp.datetime
59+
print(now)
60+
total_seconds = time.mktime(now)
61+
first_run = False
62+
refresh_clock = ticks_add(refresh_clock, refresh_timer)
63+
except Exception as e: # pylint: disable=broad-except
64+
print("Some error occured, retrying! -", e)
65+
time.sleep(2)
66+
microcontroller.reset()
67+
68+
if ticks_diff(ticks_ms(), clock_clock) >= clock_timer:
69+
remaining = time.mktime(event_time) - total_seconds
70+
secs_remaining = remaining % 60
71+
remaining //= 60
72+
mins_remaining = remaining % 60
73+
remaining //= 60
74+
hours_remaining = remaining % 24
75+
remaining //= 24
76+
days_remaining = remaining
77+
scrolling_label.text = (f"{days_remaining} DAYS, {hours_remaining} HOURS," +
78+
f"{mins_remaining} MINUTES & {secs_remaining} SECONDS")
79+
total_seconds += 1
80+
clock_clock = ticks_add(clock_clock, clock_timer)
81+
if ticks_diff(ticks_ms(), scroll_clock) >= scroll_timer:
82+
scrolling_label.x -= 1
83+
if scrolling_label.x < -(scrolling_label.width + 5):
84+
scrolling_label.x = display.width + 2
85+
display.refresh()
86+
scroll_clock = ticks_add(scroll_clock, scroll_timer)
Binary file not shown.

0 commit comments

Comments
 (0)