/ PyPortal_Quarantine_Clock / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import board 7 import busio 8 import digitalio 9 from adafruit_esp32spi import adafruit_esp32spi_socket as socket 10 from adafruit_esp32spi import adafruit_esp32spi 11 import adafruit_requests as requests 12 from adafruit_pyportal import PyPortal 13 from adafruit_bitmap_font import bitmap_font 14 from adafruit_display_text import label 15 16 try: 17 from secrets import secrets 18 except ImportError: 19 print("""WiFi settings are kept in secrets.py, please add them there! 20 the secrets dictionary must contain 'ssid' and 'password' at a minimum""") 21 raise 22 23 # Label colors 24 LABEL_DAY_COLOR = 0xFFFFFF 25 LABEL_TIME_COLOR = 0x2a8eba 26 27 # the current working directory (where this file is) 28 cwd = ("/"+__file__).rsplit('/', 1)[0] 29 background = None 30 # un-comment to set background image 31 # background = cwd+"/background.bmp" 32 33 # Descriptions of each hour 34 # https://github.com/mwfisher3/QuarantineClock/blob/master/today.html 35 time_names = ["midnight-ish", "late night", "late", "super late", 36 "super early","really early","dawn","morning", 37 "morning","mid-morning","mid-morning","late morning", 38 "noon-ish","afternoon","afternoon","mid-afternoon", 39 "late afternoon","early evening","early evening","dusk-ish", 40 "evening","evening","late evening","late evening"] 41 42 # Days of the week 43 week_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] 44 45 esp32_cs = digitalio.DigitalInOut(board.ESP_CS) 46 esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY) 47 esp32_reset = digitalio.DigitalInOut(board.ESP_RESET) 48 49 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 50 esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False) 51 requests.set_socket(socket, esp) 52 53 # initialize pyportal 54 pyportal = PyPortal(esp=esp, 55 external_spi=spi) 56 57 # set pyportal's backlight brightness 58 pyportal.set_backlight(0.7) 59 60 if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: 61 print("ESP32 found and in idle mode") 62 print("Firmware vers.", esp.firmware_version) 63 print("MAC addr:", [hex(i) for i in esp.MAC_address]) 64 65 # Set the font and preload letters 66 font_large = bitmap_font.load_font("/fonts/Helvetica-Bold-44.bdf") 67 font_small = bitmap_font.load_font("/fonts/Helvetica-Bold-24.bdf") 68 font_large.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ') 69 font_small.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()') 70 71 # Set up label for the day 72 label_day = label.Label(font_large, color=LABEL_DAY_COLOR) 73 label_day.x = board.DISPLAY.width // 7 74 label_day.y = 80 75 pyportal.splash.append(label_day) 76 77 # Set up label for the time 78 label_time = label.Label(font_small, color=LABEL_TIME_COLOR) 79 label_time.x = board.DISPLAY.width // 4 80 label_time.y = 150 81 pyportal.splash.append(label_time) 82 83 refresh_time = None 84 while True: 85 # only query the network time every hour 86 if (not refresh_time) or (time.monotonic() - refresh_time) > 3600: 87 try: 88 print("Getting new time from internet...") 89 pyportal.get_local_time(secrets['timezone']) 90 refresh_time = time.monotonic() 91 # set the_time 92 the_time = time.localtime() 93 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 94 print("Failed to get data, retrying\n", e) 95 esp.reset() 96 continue 97 98 # Convert tm_wday to name of day 99 weekday = week_days[the_time.tm_wday] 100 101 # set the day label's text 102 label_day.text = weekday 103 104 # set the time label's text 105 label_time.text = "({})".format(time_names[the_time.tm_hour]) 106 107 # update every minute 108 time.sleep(60)