circuitpython day countdowns

Code for the two CP countdown projects. One for Feather DVI and another for ESP32-S3 TFT
This commit is contained in:
Liz 2024-08-02 10:07:06 -04:00
parent 7d8fd34e13
commit 1b3480b2e4
6 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,98 @@
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import displayio
import picodvi
import board
import framebufferio
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import label
from adafruit_pcf8523.pcf8523 import PCF8523
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
EVENT_YEAR = 2024
EVENT_MONTH = 8
EVENT_DAY = 16
EVENT_HOUR = 0
EVENT_MINUTE = 0
# we'll make a python-friendly structure
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY,
EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds
-1, -1, False)) # we dont know day of week/year or DST
# check for DVI Feather with built-in display
if 'DISPLAY' in dir(board):
display = board.DISPLAY
# check for DVI feather without built-in display
elif 'CKP' in dir(board):
displayio.release_displays()
fb = picodvi.Framebuffer(320, 240,
clk_dp=board.CKP, clk_dn=board.CKN,
red_dp=board.D0P, red_dn=board.D0N,
green_dp=board.D1P, green_dn=board.D1N,
blue_dp=board.D2P, blue_dn=board.D2N,
color_depth=8)
display = framebufferio.FramebufferDisplay(fb)
# otherwise assume Pico
else:
displayio.release_displays()
fb = picodvi.Framebuffer(320, 240,
clk_dp=board.GP12, clk_dn=board.GP13,
red_dp=board.GP10, red_dn=board.GP11,
green_dp=board.GP8, green_dn=board.GP9,
blue_dp=board.GP6, blue_dn=board.GP7,
color_depth=8)
display = framebufferio.FramebufferDisplay(fb)
i2c = board.I2C()
rtc = PCF8523(i2c)
set_clock = False
if set_clock:
# year, mon, date, hour, min, sec, wday, yday, isdst
t = time.struct_time((2024, 8, 1, 16, 26, 00, 0, -1, -1))
print("Setting time to:", t)
rtc.datetime = t
print()
# variable to hold RTC datetime
t = rtc.datetime
pink = 0xf1078e
purple = 0x673192
aqua = 0x19beed
group = displayio.Group()
my_font = bitmap_font.load_font("/Helvetica-Bold-16.pcf")
clock_area = label.Label(my_font, text="00:00:00:00", color=pink)
clock_area.anchor_point = (0.0, 1.0)
clock_area.anchored_position = (display.width / 2 - clock_area.width / 2,
display.height - (clock_area.height + 20))
text1 = label.Label(my_font, text="Starting In:", color=aqua)
text1.anchor_point = (0.0, 0.0)
text1.anchored_position = (display.width / 2 - text1.width / 2,
display.height - (clock_area.height + text1.height + 35))
blinka_bitmap = displayio.OnDiskBitmap("/cpday_dvi.bmp")
blinka_grid = displayio.TileGrid(blinka_bitmap, pixel_shader=blinka_bitmap.pixel_shader)
group.append(blinka_grid)
group.append(text1)
group.append(clock_area)
display.root_group = group
clock_clock = ticks_ms()
clock_timer = 1000
while True:
if ticks_diff(ticks_ms(), clock_clock) >= clock_timer:
t = rtc.datetime
remaining = time.mktime(event_time) - time.mktime(t)
secs_remaining = remaining % 60
remaining //= 60
mins_remaining = remaining % 60
remaining //= 60
hours_remaining = remaining % 24
remaining //= 24
days_remaining = remaining
clock_area.text = (f"{days_remaining:0>2}:{hours_remaining:0>2}" +
f":{mins_remaining:0>2}:{secs_remaining:0>2}")
clock_clock = ticks_add(clock_clock, clock_timer)

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

View file

@ -0,0 +1,86 @@
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import os
import time
import wifi
import board
import displayio
import socketpool
import microcontroller
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import bitmap_label
import adafruit_ntp
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
timezone = -4
# The time of the thing!
EVENT_YEAR = 2024
EVENT_MONTH = 8
EVENT_DAY = 16
EVENT_HOUR = 0
EVENT_MINUTE = 0
# we'll make a python-friendly structure
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY,
EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds
-1, -1, False)) # we dont know day of week/year or DST
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=timezone, cache_seconds=3600)
display = board.DISPLAY
group = displayio.Group()
font = bitmap_font.load_font("/Helvetica-Bold-16.pcf")
blinka_bitmap = displayio.OnDiskBitmap("/cpday_tft.bmp")
blinka_grid = displayio.TileGrid(blinka_bitmap, pixel_shader=blinka_bitmap.pixel_shader)
scrolling_label = bitmap_label.Label(font, text=" ", y=display.height - 15)
group.append(blinka_grid)
group.append(scrolling_label)
display.root_group = group
display.auto_refresh = False
refresh_clock = ticks_ms()
refresh_timer = 3600 * 1000
clock_clock = ticks_ms()
clock_timer = 1000
scroll_clock = ticks_ms()
scroll_timer = 50
first_run = True
while True:
# only query the online time once per hour (and on first run)
if ticks_diff(ticks_ms(), refresh_clock) >= refresh_timer or first_run:
try:
print("Getting time from internet!")
now = ntp.datetime
print(now)
total_seconds = time.mktime(now)
first_run = False
refresh_clock = ticks_add(refresh_clock, refresh_timer)
except Exception as e: # pylint: disable=broad-except
print("Some error occured, retrying! -", e)
time.sleep(2)
microcontroller.reset()
if ticks_diff(ticks_ms(), clock_clock) >= clock_timer:
remaining = time.mktime(event_time) - total_seconds
secs_remaining = remaining % 60
remaining //= 60
mins_remaining = remaining % 60
remaining //= 60
hours_remaining = remaining % 24
remaining //= 24
days_remaining = remaining
scrolling_label.text = (f"{days_remaining} DAYS, {hours_remaining} HOURS," +
f"{mins_remaining} MINUTES & {secs_remaining} SECONDS")
total_seconds += 1
clock_clock = ticks_add(clock_clock, clock_timer)
if ticks_diff(ticks_ms(), scroll_clock) >= scroll_timer:
scrolling_label.x -= 1
if scrolling_label.x < -(scrolling_label.width + 5):
scrolling_label.x = display.width + 2
display.refresh()
scroll_clock = ticks_add(scroll_clock, scroll_timer)

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB