Skip to content

Commit 3b558c2

Browse files
wificonf: more robust refresh
1 parent 6700b95 commit 3b558c2

File tree

1 file changed

+38
-31
lines changed
  • internal_filesystem/builtin/apps/com.example.wificonf/assets

1 file changed

+38
-31
lines changed

internal_filesystem/builtin/apps/com.example.wificonf/assets/wificonf.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
appscreen = lv.screen_active()
1+
import ujson
2+
import os
3+
import time
4+
import lvgl as lv
5+
import _thread
26

37
havenetwork = True
48
try:
59
import network
610
except Exception as e:
711
havenetwork = False
812

9-
import ujson
10-
import os
11-
import time
12-
import lvgl as lv
13-
import _thread
13+
# Screens:
14+
appscreen = lv.screen_active()
15+
password_page=None
1416

1517
ssids=[]
1618
busy_scanning=False
1719
busy_connecting=False
18-
1920
access_points={}
2021
selected_ssid=None
2122
aplist=None
2223
password_ta=None
23-
password_page=None
2424
keyboard=None
2525
error_label=None
2626
connect_button=None
2727
cancel_button=None
2828

29+
last_tried_ssid = ""
30+
last_tried_result = ""
31+
2932
scan_button=None
3033
scan_button_label=None
3134
scan_button_scan_text = "Rescan"
@@ -67,18 +70,9 @@ def save_config():
6770
print("save_config: Failed to save config")
6871

6972

70-
def scan_done_callback():
71-
print("scan_done_callback called")
72-
global busy_scanning, scan_button_label, scan_button
73-
refresh_list()
74-
scan_button_label.set_text(scan_button_scan_text)
75-
scan_button.add_flag(lv.obj.FLAG.CLICKABLE)
76-
busy_scanning = False
77-
78-
7973
def scan_networks():
8074
print("scan_networks: Scanning for Wi-Fi networks")
81-
global ssids
75+
global ssids, busy_scanning, scan_button_label, scan_button
8276
if havenetwork and not wlan.isconnected(): # restart WiFi hardware in case it's in a bad state
8377
wlan.active(False)
8478
wlan.active(True)
@@ -92,7 +86,10 @@ def scan_networks():
9286
except Exception as e:
9387
print(f"scan_networks: Scan failed: {e}")
9488
show_error("Wi-Fi scan failed")
95-
scan_done_callback()
89+
# scan done:
90+
scan_button_label.set_text(scan_button_scan_text)
91+
scan_button.add_flag(lv.obj.FLAG.CLICKABLE)
92+
busy_scanning = False
9693

9794

9895
def start_scan_networks():
@@ -110,8 +107,9 @@ def start_scan_networks():
110107

111108
def attempt_connecting_done(ssid, result):
112109
print(f"Connecting to {ssid} got result: {result}")
113-
global busy_connecting, scan_button_label, scan_button
114-
refresh_list(ssid, result)
110+
global busy_connecting, scan_button_label, scan_button, last_tried_ssid, last_tried_result
111+
last_tried_ssid = ssid
112+
last_tried_result = result
115113
busy_connecting=False
116114
scan_button_label.set_text(scan_button_scan_text)
117115
scan_button.add_flag(lv.obj.FLAG.CLICKABLE)
@@ -160,28 +158,28 @@ def show_error(message):
160158
timer=lv.timer_create(lambda t: error_label.add_flag(lv.obj.FLAG.HIDDEN),3000,None)
161159
timer.set_repeat_count(1)
162160

163-
def refresh_list(tried_ssid="", result=""):
161+
def refresh_list(timer):
164162
global ssids
165-
print("refresh_list: Clearing current list")
166-
aplist.clean()
167-
print("refresh_list: Populating list with scanned networks")
163+
#print("refresh_list: Clearing current list")
164+
aplist.clean() # this causes an issue with lost taps if an ssid is clicked that has been removed
165+
#print("refresh_list: Populating list with scanned networks")
168166
for ssid in ssids:
169167
if len(ssid) < 1 or len(ssid) > 32:
170168
print(f"Skipping too short or long SSID: {ssid}")
171169
continue
172-
print(f"refresh_list: Adding SSID: {ssid}")
170+
#print(f"refresh_list: Adding SSID: {ssid}")
173171
button=aplist.add_button(None,ssid)
174172
button.add_event_cb(lambda e, s=ssid: select_ssid_cb(e,s),lv.EVENT.CLICKED,None)
175173
if havenetwork and wlan.isconnected() and wlan.config('essid')==ssid:
176174
status="connected"
177-
elif tried_ssid==ssid: # implies not connected
178-
status=result
175+
elif last_tried_ssid==ssid: # implies not connected
176+
status=last_tried_result
179177
elif ssid in access_points:
180178
status="saved"
181179
else:
182180
status=""
183181
if status:
184-
print(f"refresh_list: Setting status '{status}' for SSID: {ssid}")
182+
#print(f"refresh_list: Setting status '{status}' for SSID: {ssid}")
185183
label=lv.label(button)
186184
label.set_text(status)
187185
label.align(lv.ALIGN.RIGHT_MID,-10,0)
@@ -321,15 +319,24 @@ def create_ui():
321319
scan_button_label.set_text(scan_button_scan_text)
322320
scan_button_label.center()
323321
scan_button.add_event_cb(scan_cb,lv.EVENT.CLICKED,None)
324-
print("create_ui: Refreshing list with initial scan")
325-
refresh_list()
326322

323+
def janitor_cb(timer):
324+
if lv.screen_active() != appscreen and lv.screen_active() != password_page:
325+
print("wificonf.py backgrounded, cleaning up...")
326+
janitor.delete()
327+
refresh_list_timer.delete()
328+
print("wificonf.py ending")
329+
330+
janitor = lv.timer_create(janitor_cb, 400, None)
327331

328332
if havenetwork:
329333
wlan=network.WLAN(network.STA_IF)
330334
wlan.active(True)
331335

332336
load_config()
337+
333338
create_ui()
334339
start_scan_networks()
335340

341+
342+
refresh_list_timer = lv.timer_create(refresh_list, 2000, None)

0 commit comments

Comments
 (0)