@@ -28,7 +28,6 @@ class WiFi(Activity):
2828scan_button_scanning_text = "Scanning..."
2929
3030ssids = []
31- keep_running = True
3231busy_scanning = False
3332busy_connecting = False
3433error_timer = None
@@ -64,34 +63,26 @@ def onCreate(self):
6463
6564def onResume (self ,screen ):
6665print ("wifi.py onResume" )
66+ super ().onResume (screen )
6767global access_points
6868access_points = mpos .config .SharedPreferences ("com.micropythonos.system.wifiservice" ).get_dict ("access_points" )
69- self .keep_running = True
7069if len (self .ssids )== 0 :
7170if mpos .wifi .WifiService .wifi_busy == False :
7271mpos .wifi .WifiService .wifi_busy = True
7372self .start_scan_networks ()
7473else :
7574self .show_error ("Wifi is busy, please try again later." )
7675
77- def onStop (self ,screen ):
78- self .keep_running = False
79-
8076def 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
8984def 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
9687def scan_networks_thread (self ):
9788global have_network
@@ -115,24 +106,19 @@ def scan_networks_thread(self):
115106# scan done:
116107self .busy_scanning = False
117108mpos .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
124113def start_scan_networks (self ):
125- print ("scan_networks: Showing scanning label" )
126114if self .busy_scanning :
127115print ("Not scanning for networks because already busy_scanning." )
128- elif not self .keep_running :
129116return
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
137123def refresh_list (self ):
138124global have_network
@@ -198,7 +184,7 @@ def attempt_connecting_thread(self, ssid, password):
198184wlan .disconnect ()
199185wlan .connect (ssid ,password )
200186for i in range (10 ):
201- if wlan .isconnected ()or not self . keep_running :
187+ if wlan .isconnected ():
202188print (f"attempt_connecting: Connected to{ ssid } after{ i + 1 } seconds" )
203189break
204190print (f"attempt_connecting: Waiting for connection, attempt{ i + 1 } /10" )
@@ -219,13 +205,9 @@ def attempt_connecting_thread(self, ssid, password):
219205if have_network and wlan .isconnected ():
220206mpos .time .sync_time ()
221207self .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