Skip to content

Commit 51e702b

Browse files
Wifi app: use update_ui_threadsafe_if_foreground
1 parent 984d644 commit 51e702b

File tree

1 file changed

+20
-38
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.wifi/assets

1 file changed

+20
-38
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class WiFi(Activity):
2828
scan_button_scanning_text = "Scanning..."
2929

3030
ssids=[]
31-
keep_running = True
3231
busy_scanning = False
3332
busy_connecting = False
3433
error_timer = None
@@ -64,34 +63,26 @@ def onCreate(self):
6463

6564
def onResume(self, screen):
6665
print("wifi.py onResume")
66+
super().onResume(screen)
6767
global access_points
6868
access_points = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").get_dict("access_points")
69-
self.keep_running = True
7069
if len(self.ssids) == 0:
7170
if mpos.wifi.WifiService.wifi_busy == False:
7271
mpos.wifi.WifiService.wifi_busy = True
7372
self.start_scan_networks()
7473
else:
7574
self.show_error("Wifi is busy, please try again later.")
7675

77-
def onStop(self, screen):
78-
self.keep_running = False
79-
8076
def show_error(self, message):
81-
if self.keep_running: # called from slow threads so might already have stopped
82-
# Schedule UI updates because different thread
83-
print(f"show_error: Displaying error: {message}")
84-
lv.async_call(lambda l: self.error_label.set_text(message), None)
85-
lv.async_call(lambda l: self.error_label.remove_flag(lv.obj.FLAG.HIDDEN), None)
86-
self.error_timer = lv.timer_create(self.hide_error,5000,None)
87-
self.error_timer.set_repeat_count(1)
77+
# Schedule UI updates because different thread
78+
print(f"show_error: Displaying error: {message}")
79+
self.update_ui_threadsafe_if_foreground(self.error_label.set_text, message)
80+
self.update_ui_threadsafe_if_foreground(self.error_label.remove_flag, lv.obj.FLAG.HIDDEN)
81+
self.error_timer = lv.timer_create(self.hide_error,5000,None)
82+
self.error_timer.set_repeat_count(1)
8883

8984
def hide_error(self, timer):
90-
if self.keep_running:
91-
try: # self.error_label might be None
92-
self.error_label.add_flag(lv.obj.FLAG.HIDDEN)
93-
except Exception as e:
94-
print(f"self.error_label.add_flag(lv.obj.FLAG.HIDDEN) got exception: {e}")
85+
self.update_ui_threadsafe_if_foreground(self.error_label.add_flag,lv.obj.FLAG.HIDDEN)
9586

9687
def scan_networks_thread(self):
9788
global have_network
@@ -115,24 +106,19 @@ def scan_networks_thread(self):
115106
# scan done:
116107
self.busy_scanning = False
117108
mpos.wifi.WifiService.wifi_busy = False
118-
if self.keep_running:
119-
# Schedule UI updates because different thread
120-
lv.async_call(lambda l: self.scan_button_label.set_text(self.scan_button_scan_text), None)
121-
lv.async_call(lambda l: self.scan_button.remove_state(lv.STATE.DISABLED), None)
122-
lv.async_call(lambda l: self.refresh_list(), None)
109+
self.update_ui_threadsafe_if_foreground(self.scan_button_label.set_text,self.scan_button_scan_text)
110+
self.update_ui_threadsafe_if_foreground(self.scan_button.remove_state, lv.STATE.DISABLED)
111+
self.update_ui_threadsafe_if_foreground(self.refresh_list)
123112

124113
def start_scan_networks(self):
125-
print("scan_networks: Showing scanning label")
126114
if self.busy_scanning:
127115
print("Not scanning for networks because already busy_scanning.")
128-
elif not self.keep_running:
129116
return
130-
else:
131-
self.busy_scanning = True
132-
self.scan_button.add_state(lv.STATE.DISABLED)
133-
self.scan_button_label.set_text(self.scan_button_scanning_text)
134-
_thread.stack_size(mpos.apps.good_stack_size())
135-
_thread.start_new_thread(self.scan_networks_thread, ())
117+
self.busy_scanning = True
118+
self.scan_button.add_state(lv.STATE.DISABLED)
119+
self.scan_button_label.set_text(self.scan_button_scanning_text)
120+
_thread.stack_size(mpos.apps.good_stack_size())
121+
_thread.start_new_thread(self.scan_networks_thread, ())
136122

137123
def refresh_list(self):
138124
global have_network
@@ -198,7 +184,7 @@ def attempt_connecting_thread(self, ssid, password):
198184
wlan.disconnect()
199185
wlan.connect(ssid,password)
200186
for i in range(10):
201-
if wlan.isconnected() or not self.keep_running:
187+
if wlan.isconnected():
202188
print(f"attempt_connecting: Connected to {ssid} after {i+1} seconds")
203189
break
204190
print(f"attempt_connecting: Waiting for connection, attempt {i+1}/10")
@@ -219,13 +205,9 @@ def attempt_connecting_thread(self, ssid, password):
219205
if have_network and wlan.isconnected():
220206
mpos.time.sync_time()
221207
self.busy_connecting=False
222-
if self.keep_running:
223-
# Schedule UI updates because different thread
224-
lv.async_call(lambda l: self.scan_button_label.set_text(self.scan_button_scan_text), None)
225-
lv.async_call(lambda l: self.scan_button.remove_state(lv.STATE.DISABLED), None)
226-
lv.async_call(lambda l: self.refresh_list(), None)
227-
228-
208+
self.update_ui_threadsafe_if_foreground(self.scan_button_label.set_text, self.scan_button_scan_text)
209+
self.update_ui_threadsafe_if_foreground(self.scan_button.remove_state, lv.STATE.DISABLED)
210+
self.update_ui_threadsafe_if_foreground(self.refresh_list)
229211

230212

231213

0 commit comments

Comments
 (0)