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 countdown clock until an event occurs!
  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_WEEKDAY = 2       # monday = 0 .. sunday = 6
 18  EVENT_HOUR = 20         # in 24-hour time
 19  EVENT_MINUTE = 00
 20  EVENT_DURATION = 3600   # in seconds!
 21  # Instead of messing around with timezones, just put in
 22  # the *location* of the event, and we'll automatically set the PyPortal's
 23  # time to that location. Then compute the math from there
 24  # for a list of valid locations, see http://worldtimeapi.org/api/timezone
 25  EVENT_LOCATION = "America/New_York"  # set to None if its for your local time
 26  
 27  # the current working directory (where this file is)
 28  cwd = ("/"+__file__).rsplit('/', 1)[0]
 29  event_background = cwd+"/countdown_event.bmp"
 30  countdown_background = cwd+"/countdown_background.bmp"
 31  
 32  # Initialize the pyportal object and let us know what data to fetch and where
 33  # to display it
 34  pyportal = PyPortal(status_neopixel=board.NEOPIXEL,
 35                      default_bg=countdown_background)
 36  
 37  big_font = bitmap_font.load_font(cwd+"/fonts/Helvetica-Bold-36.bdf")
 38  big_font.load_glyphs(b'0123456789') # pre-load glyphs for fast printing
 39  
 40  days_position = (25, 212)
 41  hours_position = (110, 212)
 42  minutes_position = (220, 212)
 43  text_color = 0x000000
 44  
 45  text_areas = []
 46  for pos in (days_position, hours_position, minutes_position):
 47      textarea = Label(big_font, text='  ')
 48      textarea.x = pos[0]
 49      textarea.y = pos[1]
 50      textarea.color = text_color
 51      pyportal.splash.append(textarea)
 52      text_areas.append(textarea)
 53  refresh_time = None
 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(location=EVENT_LOCATION)
 61              refresh_time = time.monotonic()
 62          except RuntimeError as e:
 63              print("Some error occured, retrying! -", e)
 64              continue
 65  
 66      the_time = time.localtime()
 67      print("Time at location", EVENT_LOCATION, ":", the_time)
 68  
 69      # The easiest way to tell when
 70      mins_remaining = EVENT_MINUTE - the_time[4]
 71      if mins_remaining < 0:
 72          mins_remaining += 60
 73      # add minutes to go forward
 74      the_time = time.localtime(time.mktime(the_time) + mins_remaining * 60)
 75      #print("minute fastforward:", the_time)
 76  
 77      hours_remaining = EVENT_HOUR - the_time[3]
 78      if hours_remaining < 0:
 79          hours_remaining += 24
 80      # add hours to go forward
 81      the_time = time.localtime(time.mktime(the_time) + hours_remaining * 60 * 60)
 82      #print("hour fastforward:", the_time)
 83  
 84      days_remaining = EVENT_WEEKDAY - the_time[6]
 85      if days_remaining < 0:
 86          days_remaining += 7
 87  
 88      total_sec_remaining = days_remaining * 24 * 60 * 60
 89      total_sec_remaining += hours_remaining * 60 * 60
 90      total_sec_remaining += mins_remaining * 60
 91  
 92      print("Remaining: %d days, %d hours, %d minutes (%d total seconds)" %
 93            (days_remaining, hours_remaining, mins_remaining, total_sec_remaining))
 94  
 95      week_of_seconds = 604800
 96      if (week_of_seconds - total_sec_remaining) < EVENT_DURATION:
 97          print("ITS HAPPENING!")
 98          pyportal.set_background(event_background)
 99      else:
100          pyportal.set_background(countdown_background)
101          text_areas[0].text = '{:>1}'.format(days_remaining)  # set days textarea
102          text_areas[1].text = '{:>2}'.format(hours_remaining) # set hours textarea
103          text_areas[2].text = '{:>2}'.format(mins_remaining)  # set minutes textarea
104  
105      # update every 30 seconds
106      time.sleep(30)