|
12 | 12 | from adafruit_bitmap_font import bitmap_font |
13 | 13 | from adafruit_display_text import label |
14 | 14 | import adafruit_touchscreen |
15 | | -from adafruit_ntp import NTP |
16 | 15 |
|
17 | 16 |
|
18 | 17 | try: |
|
30 | 29 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False) |
31 | 30 | requests.set_socket(socket, esp) |
32 | 31 |
|
| 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 | + |
33 | 52 | if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: |
34 | 53 | print("ESP32 found and in idle mode") |
35 | 54 | print("Firmware vers.", esp.firmware_version) |
|
43 | 62 | print("could not connect to AP, retrying: ", e) |
44 | 63 | continue |
45 | 64 |
|
46 | | -pyportal = PyPortal(esp=esp, |
47 | | - external_spi=spi) |
48 | | - |
49 | 65 | # 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 | + |
52 | 71 |
|
53 | 72 | # Set up display group |
54 | 73 | splash = displayio.Group(max_size=10) # The Main Display Group |
55 | 74 | view = displayio.Group(max_size=15) # Group for Text objects |
56 | 75 | splash.append(view) |
57 | 76 |
|
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 |
61 | 86 | label_time.y = 150 |
62 | 87 | view.append(label_time) |
63 | 88 |
|
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 | | - |
69 | 89 | # Show group splash |
70 | 90 | board.DISPLAY.show(splash) |
71 | 91 |
|
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() |
112 | 95 |
|
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 |
115 | 99 | label_day.text = "{}".format(weekday) |
116 | 100 |
|
117 | 101 |
|
118 | | -# Time names |
| 102 | +# Time descriptions |
119 | 103 | time_name = ["midnight-ish", "late night", "late", "super late", |
120 | 104 | "super early","really early","dawn","morning", |
121 | 105 | "morning","mid-morning","mid-morning","late morning", |
122 | 106 | "noon-ish","afternoon","afternoon","mid-afternoon", |
123 | 107 | "late afternoon","early evening","early evening","dusk-ish", |
124 | 108 | "evening","evening","late evening","late evening"] |
125 | 109 |
|
126 | | -label_time.text = "({})".format(time_name[0]) |
| 110 | +label_time.text = "({})".format(time_name[the_time.tm_hour]) |
127 | 111 |
|
128 | 112 |
|
129 | 113 | while True: |
|
0 commit comments