/ PyPortal_EventCountup / code.py
code.py
1 # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 This example will figure out the current local time using the internet, and 7 then draw out a count-up clock since an event occurred! 8 Once the event is happening, a new graphic is shown 9 """ 10 import time 11 import board 12 from adafruit_pyportal import PyPortal 13 from adafruit_bitmap_font import bitmap_font 14 from adafruit_display_text.label import Label 15 16 # The time of the thing! 17 EVENT_YEAR = 1972 18 EVENT_MONTH = 12 19 EVENT_DAY = 7 20 EVENT_HOUR = 5 21 EVENT_MINUTE = 55 22 # we'll make a python-friendly structure 23 event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY, 24 EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds 25 -1, -1, False)) # we dont know day of week/year or DST 26 27 # determine the current working directory 28 # needed so we know where to find files 29 cwd = ("/"+__file__).rsplit('/', 1)[0] 30 # Initialize the pyportal object and let us know what data to fetch and where 31 # to display it 32 pyportal = PyPortal(status_neopixel=board.NEOPIXEL, 33 default_bg=cwd+"/countup_background.bmp") 34 35 big_font = bitmap_font.load_font(cwd+"/fonts/Helvetica-Bold-24.bdf") 36 big_font.load_glyphs(b'0123456789') # pre-load glyphs for fast printing 37 38 years_position = (126, 15) 39 days_position = (13, 41) 40 hours_position = (118, 41) 41 minutes_position = (25, 68) 42 text_color = 0xFF0000 43 44 text_areas = [] 45 for pos in (years_position, days_position, hours_position, minutes_position): 46 textarea = Label(big_font, text=' ') 47 textarea.x = pos[0] 48 textarea.y = pos[1] 49 textarea.color = text_color 50 pyportal.splash.append(textarea) 51 text_areas.append(textarea) 52 refresh_time = None 53 54 55 while True: 56 # only query the online time once per hour (and on first run) 57 if (not refresh_time) or (time.monotonic() - refresh_time) > 3600: 58 try: 59 print("Getting time from internet!") 60 pyportal.get_local_time() 61 refresh_time = time.monotonic() 62 except RuntimeError as e: 63 print("Some error occured, retrying! -", e) 64 continue 65 66 now = time.localtime() 67 print("Current time:", now) 68 69 # We're going to do a little cheat here, since circuitpython can't 70 # track huge amounts of time, we'll calculate the delta years here 71 if now[0] > (EVENT_YEAR+1): # we add one year to avoid half-years 72 years_since = now[0] - (EVENT_YEAR+1) 73 # and then set the event_time to not include the year delta 74 event_time = time.struct_time((EVENT_YEAR+years_since, EVENT_MONTH, EVENT_DAY, 75 EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds 76 -1, -1, False)) # we dont know day of week/year or DST 77 else: 78 years_since = 0 79 print(event_time) 80 since = time.mktime(now) - time.mktime(event_time) 81 print("Time since not including years (in sec):", since) 82 sec_since = since % 60 83 since //= 60 84 mins_since = since % 60 85 since //= 60 86 hours_since = since % 24 87 since //= 24 88 days_since = since % 365 89 since //= 365 90 years_since += since 91 print("%d years, %d days, %d hours, %d minutes and %s seconds" % 92 (years_since, days_since, hours_since, mins_since, sec_since)) 93 text_areas[0].text = '{}'.format(years_since) # set days textarea 94 text_areas[1].text = '{}'.format(days_since) # set days textarea 95 text_areas[2].text = '{}'.format(hours_since) # set hours textarea 96 text_areas[3].text = '{}'.format(mins_since) # set minutes textarea 97 98 # update every 10 seconds 99 time.sleep(10)