@@ -20,6 +20,7 @@ class WiFiConfig(Activity):
2020
2121 ssids = []
2222 havenetwork = True
23+ keep_running = True
2324 busy_scanning = False
2425 busy_connecting = False
2526
@@ -59,19 +60,25 @@ def onStart(self, screen):
5960 wlan .active (True )
6061 except Exception as e :
6162 self .havenetwork = False
62- self .start_scan_networks ()
6363
6464 def onResume (self , screen ):
6565 global access_points
6666 access_points = mpos .config .SharedPreferences ("com.micropythonos.wificonf" ).get_dict ("access_points" )
67+ self .keep_running = True
68+ if len (self .ssids ) == 0 :
69+ self .start_scan_networks ()
6770
71+ def onStop (self , screen ):
72+ self .keep_running = False
6873
6974 def show_error (self , message ):
70- print (f"show_error: Displaying error: { message } " )
71- lv .async_call (lambda l : self .error_label .set_text (message ), None )
72- lv .async_call (lambda l : self .error_label .remove_flag (lv .obj .FLAG .HIDDEN ), None )
73- timer = lv .timer_create (lambda t : self .error_label .add_flag (lv .obj .FLAG .HIDDEN ),3000 ,None )
74- timer .set_repeat_count (1 )
75+ if self .keep_running : # called from slow threads so might already have stopped
76+ # Schedule UI updates because different thread
77+ print (f"show_error: Displaying error: { message } " )
78+ lv .async_call (lambda l : self .error_label .set_text (message ), None )
79+ lv .async_call (lambda l : self .error_label .remove_flag (lv .obj .FLAG .HIDDEN ), None )
80+ timer = lv .timer_create (lambda t : self .error_label .add_flag (lv .obj .FLAG .HIDDEN ),3000 ,None )
81+ timer .set_repeat_count (1 )
7582
7683 def scan_networks_thread (self ):
7784 print ("scan_networks: Scanning for Wi-Fi networks" )
@@ -92,14 +99,18 @@ def scan_networks_thread(self):
9299 self .show_error ("Wi-Fi scan failed" )
93100 # scan done:
94101 self .busy_scanning = False
95- lv .async_call (lambda l : self .scan_button_label .set_text (self .scan_button_scan_text ), None )
96- lv .async_call (lambda l : self .scan_button .add_flag (lv .obj .FLAG .CLICKABLE ), None )
97- lv .async_call (lambda l : self .refresh_list (), None )
102+ if self .keep_running :
103+ # Schedule UI updates because different thread
104+ lv .async_call (lambda l : self .scan_button_label .set_text (self .scan_button_scan_text ), None )
105+ lv .async_call (lambda l : self .scan_button .add_flag (lv .obj .FLAG .CLICKABLE ), None )
106+ lv .async_call (lambda l : self .refresh_list (), None )
98107
99108 def start_scan_networks (self ):
100109 print ("scan_networks: Showing scanning label" )
101110 if self .busy_scanning :
102111 print ("Not scanning for networks because already busy_scanning." )
112+ elif not self .keep_running :
113+ return
103114 else :
104115 self .busy_scanning = True
105116 self .scan_button .remove_flag (lv .obj .FLAG .CLICKABLE )
@@ -169,15 +180,16 @@ def attempt_connecting_thread(self, ssid, password):
169180 wlan .disconnect ()
170181 wlan .connect (ssid ,password )
171182 for i in range (10 ):
172- if wlan .isconnected ():
183+ if wlan .isconnected () or not self . keep_running :
173184 print (f"attempt_connecting: Connected to { ssid } after { i + 1 } seconds" )
174185 break
175186 print (f"attempt_connecting: Waiting for connection, attempt { i + 1 } /10" )
176187 time .sleep (1 )
177188 if not wlan .isconnected ():
178189 result = "timeout"
179190 else :
180- print ("Warning: not trying to connect because not havenetwork" )
191+ print ("Warning: not trying to connect because not havenetwork, just waiting a bit..." )
192+ time .sleep (5 )
181193 except Exception as e :
182194 print (f"attempt_connecting: Connection error: { e } " )
183195 result = f"{ e } "
@@ -186,10 +198,11 @@ def attempt_connecting_thread(self, ssid, password):
186198 last_tried_ssid = ssid
187199 last_tried_result = result
188200 self .busy_connecting = False
189- # Schedule UI updates because different thread
190- lv .async_call (lambda l : self .scan_button_label .set_text (self .scan_button_scan_text ), None )
191- lv .async_call (lambda l : self .scan_button .add_flag (lv .obj .FLAG .CLICKABLE ), None )
192- lv .async_call (lambda l : self .refresh_list (), None )
201+ if self .keep_running :
202+ # Schedule UI updates because different thread
203+ lv .async_call (lambda l : self .scan_button_label .set_text (self .scan_button_scan_text ), None )
204+ lv .async_call (lambda l : self .scan_button .add_flag (lv .obj .FLAG .CLICKABLE ), None )
205+ lv .async_call (lambda l : self .refresh_list (), None )
193206
194207
195208
0 commit comments