Skip to content

Commit a58fa42

Browse files
authored
Merge pull request adafruit#1182 from adafruit/TheKitty-patch-46
Code for the CircuitPython Day 2020 on PyPortal guide
2 parents c98be11 + 4bee268 commit a58fa42

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

PyPortal_CircuitPython_2020/code.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
This example will figure out the current local time using the internet, and
3+
then draw out a countdown clock until an event occurs!
4+
Once the event is happening, a new graphic is shown
5+
"""
6+
import time
7+
import board
8+
from adafruit_pyportal import PyPortal
9+
from adafruit_bitmap_font import bitmap_font
10+
from adafruit_display_text.label import Label
11+
12+
# The time of the thing!
13+
EVENT_YEAR = 2020
14+
EVENT_MONTH = 9
15+
EVENT_DAY = 9
16+
EVENT_HOUR = 0
17+
EVENT_MINUTE = 0
18+
# we'll make a python-friendly structure
19+
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY,
20+
EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds
21+
-1, -1, False)) # we dont know day of week/year or DST
22+
23+
# determine the current working directory
24+
# needed so we know where to find files
25+
cwd = ("/"+__file__).rsplit('/', 1)[0]
26+
# Initialize the pyportal object and let us know what data to fetch and where
27+
# to display it
28+
pyportal = PyPortal(status_neopixel=board.NEOPIXEL,
29+
default_bg=cwd+"/circuitpython_day_countdown_background.bmp")
30+
31+
big_font = bitmap_font.load_font(cwd+"/fonts/Helvetica-Bold-36.bdf")
32+
big_font.load_glyphs(b'0123456789') # pre-load glyphs for fast printing
33+
event_background = cwd+"/countdown_event.bmp"
34+
35+
days_position = (8, 207)
36+
hours_position = (110, 207)
37+
minutes_position = (220, 207)
38+
text_color = 0xFFFFFF
39+
40+
text_areas = []
41+
for pos in (days_position, hours_position, minutes_position):
42+
textarea = Label(big_font, max_glyphs=3)
43+
textarea.x = pos[0]
44+
textarea.y = pos[1]
45+
textarea.color = text_color
46+
pyportal.splash.append(textarea)
47+
text_areas.append(textarea)
48+
refresh_time = None
49+
50+
while True:
51+
# only query the online time once per hour (and on first run)
52+
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
53+
try:
54+
print("Getting time from internet!")
55+
pyportal.get_local_time()
56+
refresh_time = time.monotonic()
57+
except RuntimeError as e:
58+
print("Some error occured, retrying! -", e)
59+
continue
60+
61+
now = time.localtime()
62+
print("Current time:", now)
63+
remaining = time.mktime(event_time) - time.mktime(now)
64+
print("Time remaining (s):", remaining)
65+
if remaining < 0:
66+
# oh, its event time!
67+
pyportal.set_background(event_background)
68+
while True: # that's all folks
69+
pass
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+
print("%d days, %d hours, %d minutes and %s seconds" %
78+
(days_remaining, hours_remaining, mins_remaining, secs_remaining))
79+
text_areas[0].text = '{:>2}'.format(days_remaining) # set days textarea
80+
text_areas[1].text = '{:>2}'.format(hours_remaining) # set hours textarea
81+
text_areas[2].text = '{:>2}'.format(mins_remaining) # set minutes textarea
82+
83+
# update every 10 seconds
84+
time.sleep(10)

0 commit comments

Comments
 (0)