code.py
 1  # SPDX-FileCopyrightText: 2019 Isaac Wellish for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  'of this day' demo
 7  Display notable info about famous electronics-related peoples
 8  Data sources: https://github.com/adafruit/OTD/tree/master/electronics
 9  """
10  
11  import time
12  import board
13  from adafruit_pyportal import PyPortal
14  
15  cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
16  
17  DAY = ["Day of the year"]
18  PERSON = ["Person"]
19  NOTABLE = ["Notable for"]
20  YEAR = ["Year"]
21  ACCOMPLISH = ["Accomplishment"]
22  WEB = ["Web Reference"]
23  
24  BASE_DATA = "https://raw.githubusercontent.com/adafruit/OTD/master/electronics/"
25  
26  # a function that returns whatever is passed in
27  def identity(x):
28      return x
29  
30  # create pyportal object w no data source (we'll feed it text later)
31  pyportal = PyPortal(url = BASE_DATA, debug=True,
32                      json_path = (DAY, PERSON, NOTABLE, YEAR, ACCOMPLISH, WEB),
33                      status_neopixel = board.NEOPIXEL,
34                      default_bg = cwd + "/on_this_day_bg.bmp",
35                      text_font = cwd+"fonts/Arial-ItalicMT-17.bdf",
36                      text_transform = [identity]*6,  # we do this so the date doesnt get commas
37                      text_position=((10, 70), (10, 100), (10, 130),(60, 160), (105, 190), (10, 220)),
38                      text_color=(0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF),
39                      text_maxlen=(50, 50, 50, 50, 50, 50), # cut off characters
40                     )
41  
42  while True:
43      try:
44          print("Getting time from internet!")
45          pyportal.get_local_time()
46          refresh_time = time.monotonic()
47      except RuntimeError as e:
48          print("Some error occured, retrying! -", e)
49          continue
50  
51      now = time.localtime()
52      print("Current time:", now)
53      url = BASE_DATA+"%02d_%02d.json" % (now.tm_mon, now.tm_mday)
54      print(url)
55      response = None
56      try:
57          response = pyportal.fetch(url)
58          print("Response is", response)
59      except RuntimeError as e:
60          print("Some error occured, retrying! -", e)
61  
62      # Make a QR code from web reference
63      pyportal.show_QR(bytearray(response[5]), qr_size=3,
64                       x=220, y=10)
65  
66      # wait 10 minutes before running again
67      time.sleep(10*60)