"""
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
"""importtimeimport board
from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.text_areaimport TextArea
# The time of the thing!
EVENT_YEAR =2019
EVENT_MONTH =4
EVENT_DAY =14
EVENT_HOUR =22# 10 pm
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# determine the current working directory# needed so we know where to find files
cwd =("/"+__file__).rsplit('/',1)[0]# Initialize the pyportal object and let us know what data to fetch and where# to display it
pyportal = PyPortal(status_neopixel=board.NEOPIXEL,
default_bg=cwd+"/countdown_background.bmp")
big_font = bitmap_font.load_font(cwd+"/fonts/Trajan-Bold-24.bdf")
big_font.load_glyphs(b'0123456789 DaysHourMinute')# pre-load glyphs for fast printing
event_background = cwd+"/countdown_event.bmp"
pyportal.play_file(cwd+"/Coin.wav")
days_position =(10,175)
hours_position =(10,210)
minutes_position =(10,245)
text_color =0x808080
text_areas =[]for pos in(days_position, hours_position, minutes_position):
textarea = TextArea(big_font, width=20)
textarea.x= pos[0]
textarea.y= pos[1]
textarea.color= text_color
pyportal.splash.append(textarea)
text_areas.append(textarea)
refresh_time =NonewhileTrue:
# only query the online time once per hour (and on first run)if(not refresh_time)or(time.monotonic() - refresh_time)>3600:
try:
print("Getting time from internet!")
pyportal.get_local_time()
refresh_time =time.monotonic()exceptRuntimeErroras 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:
# oh, its event time!
pyportal.set_background(event_background)whileTrue: # that's all folkspass
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} Days'.format(days_remaining)# set days textarea
text_areas[1].text='{:>2} Hours'.format(hours_remaining)# set hours textarea
text_areas[2].text='{:>2} Minutes'.format(mins_remaining)# set minutes textarea# update every 10 secondstime.sleep(10)
"""
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.text_area import TextArea
# The time of the thing!
EVENT_YEAR = 2019
EVENT_MONTH = 4
EVENT_DAY = 14
EVENT_HOUR = 22 # 10 pm
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
# determine the current working directory
# needed so we know where to find files
cwd = ("/"+__file__).rsplit('/', 1)[0]
# Initialize the pyportal object and let us know what data to fetch and where
# to display it
pyportal = PyPortal(status_neopixel=board.NEOPIXEL,
default_bg=cwd+"/countdown_background.bmp")
big_font = bitmap_font.load_font(cwd+"/fonts/Trajan-Bold-24.bdf")
big_font.load_glyphs(b'0123456789 DaysHourMinute') # pre-load glyphs for fast printing
event_background = cwd+"/countdown_event.bmp"
pyportal.play_file(cwd+"/Coin.wav")
days_position = (10, 175)
hours_position = (10, 210)
minutes_position = (10, 245)
text_color = 0x808080
text_areas = []
for pos in (days_position, hours_position, minutes_position):
textarea = TextArea(big_font, width=20)
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:
# only query the online time once per hour (and on first run)
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:
# oh, its event time!
pyportal.set_background(event_background)
while True: # that's all folks
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} Days'.format(days_remaining) # set days textarea
text_areas[1].text = '{:>2} Hours'.format(hours_remaining) # set hours textarea
text_areas[2].text = '{:>2} Minutes'.format(mins_remaining) # set minutes textarea
# update every 10 seconds
time.sleep(10)
Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards
Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.
Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7:30pm ET! To join, head over to YouTube and check out the show’s live chat and our Discord!
Python for Microcontrollers – Adafruit Daily — Select Python on Microcontrollers Newsletter: PyCon AU 2024 Talks, New Raspberry Pi Gear Available and More! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi
EYE on NPI – Adafruit Daily — EYE on NPI Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey