Skip to content

Commit b29a183

Browse files
author
brentru
committed
get time from AIO timeserver, localtime, wday conversion
1 parent 4aa5a52 commit b29a183

File tree

1 file changed

+42
-58
lines changed

1 file changed

+42
-58
lines changed

PyPortal_Quarantine_Clock/code.py

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from adafruit_bitmap_font import bitmap_font
1313
from adafruit_display_text import label
1414
import adafruit_touchscreen
15-
from adafruit_ntp import NTP
1615

1716

1817
try:
@@ -30,6 +29,26 @@
3029
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
3130
requests.set_socket(socket, esp)
3231

32+
33+
def wday_to_weekday_name(tm_wday):
34+
"""Returns the name of the weekday based on tm_wday value.
35+
:param int tm_wday: Days since Sunday.
36+
37+
"""
38+
switch = {
39+
0: "Monday",
40+
1: "Tuesday",
41+
2: "Wednesday",
42+
3: "Thursday",
43+
4: "Friday",
44+
5: "Saturday",
45+
6: "Sunday",}
46+
return switch.get(tm_wday, "Not found")
47+
48+
# initialize pyportal
49+
pyportal = PyPortal(esp=esp,
50+
external_spi=spi)
51+
3352
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
3453
print("ESP32 found and in idle mode")
3554
print("Firmware vers.", esp.firmware_version)
@@ -43,87 +62,52 @@
4362
print("could not connect to AP, retrying: ", e)
4463
continue
4564

46-
pyportal = PyPortal(esp=esp,
47-
external_spi=spi)
48-
4965
# Set the font and preload letters
50-
font = bitmap_font.load_font("/fonts/Helvetica-Bold-36.bdf")
51-
font.load_glyphs(b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()')
66+
font_large = bitmap_font.load_font("/fonts/Helvetica-Bold-36.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+
5271

5372
# Set up display group
5473
splash = displayio.Group(max_size=10) # The Main Display Group
5574
view = displayio.Group(max_size=15) # Group for Text objects
5675
splash.append(view)
5776

58-
# Set up text labels
59-
label_time = label.Label(font, color=0x538ab1, max_glyphs=200)
60-
label_time.x = board.DISPLAY.width // 7
77+
# Set up label for the day
78+
label_day = label.Label(font_large, color=0xFFFFFF, max_glyphs=200)
79+
label_day.x = board.DISPLAY.width // 4
80+
label_day.y = 70
81+
view.append(label_day)
82+
83+
# Set up label for the time
84+
label_time = label.Label(font_small, color=0x538ab1, max_glyphs=200)
85+
label_time.x = board.DISPLAY.width // 4
6186
label_time.y = 150
6287
view.append(label_time)
6388

64-
label_day = label.Label(font, color=0xFFFFFF, max_glyphs=200)
65-
label_day.x = board.DISPLAY.width // 7
66-
label_day.y = 40
67-
view.append(label_day)
68-
6989
# Show group splash
7090
board.DISPLAY.show(splash)
7191

72-
# Initialize the NTP object
73-
ntp = NTP(esp)
74-
75-
# Fetch and set the microcontroller's current UTC time
76-
# keep retrying until a valid time is returned
77-
while not ntp.valid_time:
78-
ntp.set_time()
79-
print("Failed to obtain time, retrying in 15 seconds...")
80-
time.sleep(15)
81-
82-
# Get the current time in seconds since Jan 1, 1970
83-
current_time = time.time()
84-
print("Seconds since Jan 1, 1970: {} seconds".format(current_time))
85-
86-
# Convert the current time in seconds since Jan 1, 1970 to a struct_time
87-
now = time.localtime(current_time)
88-
print(now)
89-
90-
# Pretty-parse the struct_time
91-
print(
92-
"It is currently {}/{}/{} at {}:{}:{} UTC".format(
93-
now.tm_mon, now.tm_mday, now.tm_year, now.tm_hour, now.tm_min, now.tm_sec
94-
)
95-
)
96-
97-
98-
def wday_to_weekday_name(tm_wday):
99-
"""Returns the name of the weekday based on tm_wday.
100-
:param int tm_wday: Days since Sunday.
101-
102-
"""
103-
switcher = {
104-
0: "Monday",
105-
1: "Tuesday",
106-
2: "Wednesday",
107-
3: "Thursday",
108-
4: "Friday",
109-
5: "Saturday",
110-
6: "Sunday",}
111-
return switcher.get(tm_wday, "Not found")
92+
# Obtain local time from Time API
93+
pyportal.get_local_time(secrets['timezone'])
94+
the_time = time.localtime()
11295

113-
# Convert date to weekday name
114-
weekday = wday_to_weekday_name(now.tm_wday)
96+
# Convert tm_wday to name of day
97+
weekday = wday_to_weekday_name(the_time.tm_wday)
98+
# Set label_day
11599
label_day.text = "{}".format(weekday)
116100

117101

118-
# Time names
102+
# Time descriptions
119103
time_name = ["midnight-ish", "late night", "late", "super late",
120104
"super early","really early","dawn","morning",
121105
"morning","mid-morning","mid-morning","late morning",
122106
"noon-ish","afternoon","afternoon","mid-afternoon",
123107
"late afternoon","early evening","early evening","dusk-ish",
124108
"evening","evening","late evening","late evening"]
125109

126-
label_time.text = "({})".format(time_name[0])
110+
label_time.text = "({})".format(time_name[the_time.tm_hour])
127111

128112

129113
while True:

0 commit comments

Comments
 (0)