CircuitPython code
"""
This example will figure out the current local time using the internet, and
then draw out a countdown clock until an event occurs!
Once the event is happening, a new graphic is shown
"""
import time
import board
from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.label import Label
EVENT_YEAR = 2019
EVENT_MONTH = 4
EVENT_DAY = 3
EVENT_HOUR = 15
EVENT_MINUTE = 0
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY,
EVENT_HOUR, EVENT_MINUTE, 0,
-1, -1, False))
cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(status_neopixel=board.NEOPIXEL,
default_bg=cwd+"/countdown_background.bmp")
big_font = bitmap_font.load_font(cwd+"/fonts/Helvetica-Bold-36.bdf")
big_font.load_glyphs(b'0123456789')
event_background = cwd+"/countdown_event.bmp"
days_position = (8, 207)
hours_position = (110, 207)
minutes_position = (220, 207)
text_color = 0xFFFFFF
text_areas = []
for pos in (days_position, hours_position, minutes_position):
textarea = Label(big_font, text=' ')
textarea.x = pos[0]
textarea.y = pos[1]
textarea.color = text_color
pyportal.splash.append(textarea)
text_areas.append(textarea)
refresh_time = None
while True:
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
try:
print("Getting time from internet!")
pyportal.get_local_time()
refresh_time = time.monotonic()
except RuntimeError as e:
print("Some error occured, retrying! -", e)
continue
now = time.localtime()
print("Current time:", now)
remaining = time.mktime(event_time) - time.mktime(now)
print("Time remaining (s):", remaining)
if remaining < 0:
pyportal.set_background(event_background)
while True:
pass
secs_remaining = remaining % 60
remaining //= 60
mins_remaining = remaining % 60
remaining //= 60
hours_remaining = remaining % 24
remaining //= 24
days_remaining = remaining
print("%d days, %d hours, %d minutes and %s seconds" %
(days_remaining, hours_remaining, mins_remaining, secs_remaining))
text_areas[0].text = '{:>2}'.format(days_remaining)
text_areas[1].text = '{:>2}'.format(hours_remaining)
text_areas[2].text = '{:>2}'.format(mins_remaining)
time.sleep(10)
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.