Skip to content

Commit e6a6d24

Browse files
author
brentru
committed
add month clock, linted!
1 parent 83d679d commit e6a6d24

File tree

1 file changed

+120
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)