Skip to content

Commit 37d7704

Browse files
committed
Add easyrtc.py and update splash.py to use the new API
1 parent 0554433 commit 37d7704

File tree

2 files changed

+64
-121
lines changed

2 files changed

+64
-121
lines changed

esp32/modules/easyrtc.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# File: easyrtc.py
2+
# Version: 1
3+
# Description: Wrapper that makes using the clock simple
4+
# License: MIT
5+
# Authors: Renze Nicolai <renze@rnplus.nl>
6+
7+
import machine, time
8+
9+
# Functions
10+
def string(date=False, time=True):
11+
[year, month, mday, wday, hour, min, sec, usec] = machine.RTC().datetime()
12+
monthstr = str(month)
13+
if (month<10):
14+
monthstr = "0"+monthstr
15+
daystr = str(mday)
16+
if (mday<10):
17+
daystr = "0"+daystr
18+
hourstr = str(hour)
19+
if (hour<10):
20+
hourstr = "0"+hourstr
21+
minstr = str(min)
22+
if (min<10):
23+
minstr = "0"+minstr
24+
output = ""
25+
if date:
26+
output += daystr+"-"+monthstr+"-"+str(year)
27+
if time:
28+
output += " "
29+
if time:
30+
output += hourstr+":"+minstr
31+
return output
32+
33+
def configure():
34+
import easywifi
35+
import easydraw
36+
import ntp
37+
if not easywifi.status():
38+
if not easywifi.enable():
39+
return False
40+
easydraw.msg("Configuring clock...", True)
41+
ntp.set_NTP_time()
42+
easydraw.msg("Done")
43+
return True

esp32/modules/splash.py

Lines changed: 21 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,16 @@
1-
import ugfx, time, ntp, badge, machine, deepsleep, network, esp, gc
2-
import appglue, services
3-
41
# File: splash.py
5-
# Version: 3
2+
# Version: 4
63
# Description: Homescreen for SHA2017 badge
74
# License: MIT
85
# Authors: Renze Nicolai <renze@rnplus.nl>
96
# Thomas Roos <?>
107

8+
import ugfx, time, ntp, badge, machine, deepsleep, network, esp, gc
9+
import appglue, services
1110

12-
### FUNCTIONS
11+
import easydraw, easywifi, easyrtc
1312

14-
# RTC
15-
def splash_rtc_string(date=False, time=True):
16-
[year, month, mday, wday, hour, min, sec, usec] = machine.RTC().datetime()
17-
monthstr = str(month)
18-
if (month<10):
19-
monthstr = "0"+monthstr
20-
daystr = str(mday)
21-
if (mday<10):
22-
daystr = "0"+daystr
23-
hourstr = str(hour)
24-
if (hour<10):
25-
hourstr = "0"+hourstr
26-
minstr = str(min)
27-
if (min<10):
28-
minstr = "0"+minstr
29-
output = ""
30-
if date:
31-
output += daystr+"-"+monthstr+"-"+str(year)
32-
if time:
33-
output += " "
34-
if time:
35-
output += hourstr+":"+minstr
36-
return output
13+
### FUNCTIONS
3714

3815
# Graphics
3916
def splash_draw_battery(vUsb, vBatt):
@@ -80,7 +57,7 @@ def splash_draw_actions(sleeping):
8057
if otaAvailable:
8158
info2 = 'Press select to start OTA update'
8259
else:
83-
info2 = splash_rtc_string(True, True)
60+
info2 = easyrtc.string(True, True)
8461

8562
l = ugfx.get_string_width(info1,"Roboto_Regular12")
8663
ugfx.string(296-l, 0, info1, "Roboto_Regular12",ugfx.BLACK)
@@ -118,111 +95,34 @@ def splash_draw(full=False,sleeping=False):
11895
ugfx.flush(ugfx.LUT_FULL)
11996
else:
12097
ugfx.flush(ugfx.LUT_NORMAL)
121-
122-
123-
def splash_draw_msg(message, clear=False):
124-
global splashDrawMsg
125-
splashDrawMsg = True
126-
global splashDrawMsgLineNumber
127-
try:
128-
splashDrawMsgLineNumber
129-
except:
130-
splashDrawMsgLineNumber = 0
131-
132-
if clear:
133-
ugfx.clear(ugfx.WHITE)
134-
ugfx.string(0, 0, message, "PermanentMarker22", ugfx.BLACK)
135-
ugfx.set_lut(ugfx.LUT_FASTER)
136-
ugfx.flush()
137-
splashDrawMsgLineNumber = 0
138-
else:
139-
ugfx.string(0, 30 + (splashDrawMsgLineNumber * 15), message, "Roboto_Regular12", ugfx.BLACK)
140-
ugfx.flush()
141-
splashDrawMsgLineNumber += 1
14298

143-
# WiFi
144-
def splash_wifi_connect():
145-
global wifiStatus
146-
try:
147-
wifiStatus
148-
except:
149-
wifiStatus = False
150-
151-
if not wifiStatus:
152-
nw = network.WLAN(network.STA_IF)
153-
if not nw.isconnected():
154-
nw.active(True)
155-
ssid = badge.nvs_get_str('badge', 'wifi.ssid', 'SHA2017-insecure')
156-
password = badge.nvs_get_str('badge', 'wifi.password')
157-
nw.connect(ssid, password) if password else nw.connect(ssid)
158-
159-
splash_draw_msg("Connecting to WiFi...", True)
160-
splash_draw_msg("("+ssid+")")
161-
162-
timeout = badge.nvs_get_u8('splash', 'wifi.timeout', 40)
163-
while not nw.isconnected():
164-
time.sleep(0.1)
165-
timeout = timeout - 1
166-
if (timeout<1):
167-
splash_draw_msg("Timeout while connecting!")
168-
splash_wifi_disable()
169-
return False
170-
wifiStatus = True
171-
return True
172-
return False
173-
174-
def splash_wifi_active():
175-
global wifiStatus
176-
try:
177-
wifiStatus
178-
except:
179-
wifiStatus = False
180-
return wifiStatus
181-
182-
183-
def splash_wifi_disable():
184-
global wifiStatus
185-
wifiStatus = False
186-
nw = network.WLAN(network.STA_IF)
187-
nw.active(False)
188-
189-
# NTP clock configuration
190-
def splash_ntp():
191-
if not splash_wifi_active():
192-
if not splash_wifi_connect():
193-
return False
194-
splash_draw_msg("Configuring clock...", True)
195-
ntp.set_NTP_time()
196-
splash_draw_msg("Done")
197-
return True
198-
19999
# OTA update checking
200100

201101
def splash_ota_download_info():
202102
import urequests as requests
203-
splash_draw_msg("Checking for updates...", True)
103+
easydraw.msg("Checking for updates...", True)
204104
result = False
205105
try:
206106
data = requests.get("https://badge.sha2017.org/version")
207107
except:
208-
splash_draw_msg("Error:")
209-
splash_draw_msg("Could not download JSON!")
108+
easydraw.msg("Error:")
109+
easydraw.msg("Could not download JSON!")
210110
time.sleep(5)
211111
return False
212112
try:
213113
result = data.json()
214114
except:
215115
data.close()
216-
splash_draw_msg("Error:")
217-
splash_draw_msg("Could not decode JSON!")
116+
easydraw.msg("Error:")
117+
easydraw.msg("Could not decode JSON!")
218118
time.sleep(5)
219119
return False
220120
data.close()
221121
return result
222122

223123
def splash_ota_check():
224-
if not splash_wifi_active():
225-
if not splash_wifi_connect():
124+
if not easywifi.status():
125+
if not easywifi.enable():
226126
return False
227127

228128
info = splash_ota_download_info()
@@ -240,9 +140,9 @@ def splash_ota_start():
240140

241141
# Resources
242142
def splash_resources_install():
243-
splash_draw_msg("Installing resources...",True)
244-
if not splash_wifi_active():
245-
if not splash_wifi_connect():
143+
easydraw.msg("Installing resources...",True)
144+
if not easywifi.status():
145+
if not easywifi.enable():
246146
return False
247147
import woezel
248148
woezel.install("resources")
@@ -426,24 +326,24 @@ def splash_timer_callback(tmr):
426326
elif setupState == 2: # Third boot: force OTA check
427327
print("[SPLASH] Third boot...")
428328
badge.nvs_set_u8('badge', 'setup.state', 3)
429-
otaCheck = splash_ntp() if time.time() < 1482192000 else True
329+
otaCheck = easyrtc.configure() if time.time() < 1482192000 else True
430330
otaAvailable = splash_ota_check()
431331
else: # Normal boot
432332
print("[SPLASH] Normal boot...")
433-
otaCheck = splash_ntp() if time.time() < 1482192000 else True
333+
otaCheck = easyrtc.configure() if time.time() < 1482192000 else True
434334
if (machine.reset_cause() != machine.DEEPSLEEP_RESET) and otaCheck:
435335
otaAvailable = splash_ota_check()
436336
else:
437337
otaAvailable = badge.nvs_get_u8('badge','OTA.ready',0)
438338

439339
# Download resources to fatfs
440340
splash_resources_check()
441-
442-
# Disable WiFi if active
443-
splash_wifi_disable()
444341

445342
# Initialize services
446343
services.setup()
344+
345+
# Disable WiFi if active
346+
easywifi.disable()
447347

448348
# Initialize timer
449349
splash_timer_init()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy