Skip to content

Commit 83d679d

Browse files
authored
Merge pull request adafruit#1087 from brentru/quar-clock
Quarantine Clock
2 parents f026b68 + d64cd25 commit 83d679d

File tree

3 files changed

+13295
-0
lines changed

3 files changed

+13295
-0
lines changed

PyPortal_Quarantine_Clock/code.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
# Days of the week
39+
week_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
40+
41+
esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
42+
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
43+
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)
44+
45+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
46+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
47+
requests.set_socket(socket, esp)
48+
49+
# initialize pyportal
50+
pyportal = PyPortal(esp=esp,
51+
external_spi=spi,
52+
default_bg = None)
53+
54+
# set pyportal's backlight brightness
55+
pyportal.set_backlight(0.7)
56+
57+
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
58+
print("ESP32 found and in idle mode")
59+
print("Firmware vers.", esp.firmware_version)
60+
print("MAC addr:", [hex(i) for i in esp.MAC_address])
61+
62+
print("Connecting to AP...")
63+
while not esp.is_connected:
64+
try:
65+
esp.connect_AP(secrets['ssid'], secrets['password'])
66+
except RuntimeError as e:
67+
print("could not connect to AP, retrying: ", e)
68+
continue
69+
70+
# Set the font and preload letters
71+
font_large = bitmap_font.load_font("/fonts/Helvetica-Bold-44.bdf")
72+
font_small = bitmap_font.load_font("/fonts/Helvetica-Bold-24.bdf")
73+
font_large.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ')
74+
font_small.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()')
75+
76+
# Set up label for the day
77+
label_day = label.Label(font_large, color=LABEL_DAY_COLOR, max_glyphs=200)
78+
label_day.x = board.DISPLAY.width // 7
79+
label_day.y = 80
80+
pyportal.splash.append(label_day)
81+
82+
# Set up label for the time
83+
label_time = label.Label(font_small, color=LABEL_TIME_COLOR, max_glyphs=200)
84+
label_time.x = board.DISPLAY.width // 4
85+
label_time.y = 150
86+
pyportal.splash.append(label_time)
87+
88+
refresh_time = None
89+
while True:
90+
# only query the network time every hour
91+
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
92+
try:
93+
print("Getting new time from internet...")
94+
pyportal.get_local_time(secrets['timezone'])
95+
refresh_time = time.monotonic()
96+
# set the_time
97+
the_time = time.localtime()
98+
except (ValueError, RuntimeError) as error:
99+
print("Failed to get data, retrying\n", e)
100+
esp.reset()
101+
continue
102+
103+
# Convert tm_wday to name of day
104+
weekday = week_days[the_time.tm_wday]
105+
106+
# set the day label's text
107+
label_day.text = weekday
108+
109+
# set the time label's text
110+
label_time.text = "({})".format(time_names[the_time.tm_hour])
111+
112+
# update every minute
113+
time.sleep(60)

0 commit comments

Comments
 (0)