|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +import digitalio |
| 5 | +from adafruit_esp32spi import adafruit_esp32spi_socket as socket |
| 6 | +from adafruit_esp32spi import adafruit_esp32spi |
| 7 | +import adafruit_requests as requests |
| 8 | +from adafruit_pyportal import PyPortal |
| 9 | +from adafruit_bitmap_font import bitmap_font |
| 10 | +from adafruit_display_text import label |
| 11 | + |
| 12 | +try: |
| 13 | + from secrets import secrets |
| 14 | +except ImportError: |
| 15 | + print("""WiFi settings are kept in secrets.py, please add them there! |
| 16 | +the secrets dictionary must contain 'ssid' and 'password' at a minimum""") |
| 17 | + raise |
| 18 | + |
| 19 | +# Label colors |
| 20 | +LABEL_DAY_COLOR = 0xFFFFFF |
| 21 | +LABEL_TIME_COLOR = 0x2a8eba |
| 22 | + |
| 23 | +# the current working directory (where this file is) |
| 24 | +cwd = ("/"+__file__).rsplit('/', 1)[0] |
| 25 | +background = None |
| 26 | +# un-comment to set background image |
| 27 | +# background = cwd+"/background.bmp" |
| 28 | + |
| 29 | +# Descriptions of each hour |
| 30 | +# https://github.com/mwfisher3/QuarantineClock/blob/master/today.html |
| 31 | +time_names = ["midnight-ish", "late night", "late", "super late", |
| 32 | + "super early","really early","dawn","morning", |
| 33 | + "morning","mid-morning","mid-morning","late morning", |
| 34 | + "noon-ish","afternoon","afternoon","mid-afternoon", |
| 35 | + "late afternoon","early evening","early evening","dusk-ish", |
| 36 | + "evening","evening","late evening","late evening"] |
| 37 | + |
| 38 | +# Months of the year |
| 39 | +months = ["January", "January", "February", "March", "April", |
| 40 | + "May", "June", "July", "August", |
| 41 | + "September", "October", "November", "December"] |
| 42 | + |
| 43 | +# Dictionary of tm_mon and month name. |
| 44 | +# note: tm_mon structure in CircuitPython ranges from [1,12] |
| 45 | +months = { |
| 46 | + 1: "January", |
| 47 | + 2: "February", |
| 48 | + 3: "March", |
| 49 | + 4: "April", |
| 50 | + 5: "May", |
| 51 | + 6: "June", |
| 52 | + 7: "July", |
| 53 | + 8: "August", |
| 54 | + 9: "September", |
| 55 | + 10: "October", |
| 56 | + 11: "November", |
| 57 | + 12: "December" |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +esp32_cs = digitalio.DigitalInOut(board.ESP_CS) |
| 62 | +esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY) |
| 63 | +esp32_reset = digitalio.DigitalInOut(board.ESP_RESET) |
| 64 | + |
| 65 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 66 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False) |
| 67 | +requests.set_socket(socket, esp) |
| 68 | + |
| 69 | +# initialize pyportal |
| 70 | +pyportal = PyPortal(esp=esp, |
| 71 | + external_spi=spi, |
| 72 | + default_bg = None) |
| 73 | + |
| 74 | +# set pyportal's backlight brightness |
| 75 | +pyportal.set_backlight(0.2) |
| 76 | + |
| 77 | +if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: |
| 78 | + print("ESP32 found and in idle mode") |
| 79 | + print("Firmware vers.", esp.firmware_version) |
| 80 | + print("MAC addr:", [hex(i) for i in esp.MAC_address]) |
| 81 | + |
| 82 | +print("Connecting to AP...") |
| 83 | +while not esp.is_connected: |
| 84 | + try: |
| 85 | + esp.connect_AP(secrets['ssid'], secrets['password']) |
| 86 | + except RuntimeError as e: |
| 87 | + print("could not connect to AP, retrying: ", e) |
| 88 | + continue |
| 89 | + |
| 90 | +# Set the font and preload letters |
| 91 | +font_large = bitmap_font.load_font("/fonts/Helvetica-Bold-44.bdf") |
| 92 | +font_small = bitmap_font.load_font("/fonts/Helvetica-Bold-24.bdf") |
| 93 | +font_large.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ') |
| 94 | +font_small.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()') |
| 95 | + |
| 96 | +# Set up label for the month |
| 97 | +label_month = label.Label(font_large, color=LABEL_DAY_COLOR, max_glyphs=200) |
| 98 | +label_month.x = board.DISPLAY.width // 10 |
| 99 | +label_month.y = 80 |
| 100 | +pyportal.splash.append(label_month) |
| 101 | + |
| 102 | +# Set up label for the time |
| 103 | +label_time = label.Label(font_small, color=LABEL_TIME_COLOR, max_glyphs=200) |
| 104 | +label_time.x = board.DISPLAY.width // 3 |
| 105 | +label_time.y = 150 |
| 106 | +pyportal.splash.append(label_time) |
| 107 | + |
| 108 | +refresh_time = None |
| 109 | +while True: |
| 110 | + # only query the network time every hour |
| 111 | + if (not refresh_time) or (time.monotonic() - refresh_time) > 3600: |
| 112 | + try: |
| 113 | + print("Getting new time from internet...") |
| 114 | + pyportal.get_local_time(secrets['timezone']) |
| 115 | + refresh_time = time.monotonic() |
| 116 | + # set the_time |
| 117 | + the_time = time.localtime() |
| 118 | + except (ValueError, RuntimeError) as error: |
| 119 | + print("Failed to get data, retrying\n", e) |
| 120 | + esp.reset() |
| 121 | + continue |
| 122 | + |
| 123 | + # convert tm_mon value to month name |
| 124 | + month = months[the_time.tm_mon] |
| 125 | + |
| 126 | + # determine and display how far we are in the month |
| 127 | + if 1 <= the_time.tm_mday <= 14: |
| 128 | + label_month.text = "Early %s-ish"%month |
| 129 | + elif 15 <= the_time.tm_mday <= 24: |
| 130 | + label_month.text = "Mid %s-ish"%month |
| 131 | + else: |
| 132 | + label_month.text = "Late %s-ish"%month |
| 133 | + |
| 134 | + # set the time label's text |
| 135 | + label_time.text = "({})".format(time_names[the_time.tm_hour]) |
| 136 | + |
| 137 | + # update every minute |
| 138 | + time.sleep(60) |
0 commit comments