1010
1111class OSUpdate (Activity ):
1212
13- keep_running = True
1413download_update_url = None
1514
1615# Widgets:
@@ -58,9 +57,6 @@ def onStart(self, screen):
5857print ("Showing update info..." )
5958self .show_update_info ()
6059
61- def onStop (self ,screen ):
62- self .keep_running = False # this is checked by the update_with_lvgl thread
63-
6460def show_update_info (self ):
6561self .status_label .set_text ("Checking for OS updates..." )
6662hwid = mpos .info .get_hardware_id ()
@@ -140,8 +136,8 @@ def force_update_clicked(self):
140136
141137def progress_callback (self ,percent ):
142138print (f"OTA Update:{ percent :.1f} %" )
143- lv . async_call ( lambda l : self .progress_label . set_text ( f"OTA Update: { percent :.2f } %" ),None )
144- lv . async_call ( lambda l : self .progress_bar . set_value ( int ( percent ), True ), None )
139+ self . update_ui_threadsafe_if_foreground ( self .progress_bar . set_value , int ( percent ),True )
140+ self . update_ui_threadsafe_if_foreground ( self .progress_label . set_text , f"OTA Update: { percent :.2f } %" )
145141time .sleep_ms (100 )
146142
147143# Custom OTA update with LVGL progress
@@ -168,7 +164,7 @@ def update_with_lvgl(self, url):
168164i = 0
169165total_size = round_up_to_multiple (total_size ,chunk_size )
170166print (f"Starting OTA update of size:{ total_size } " )
171- while self .keep_running :# stop if the user navigates away
167+ while self .has_foreground () :# stop if the user navigates away
172168time .sleep_ms (100 )# don't hog the CPU
173169chunk = response .raw .read (chunk_size )
174170if not chunk :
@@ -187,7 +183,7 @@ def update_with_lvgl(self, url):
187183response .close ()
188184try :
189185if bytes_written >= total_size :
190- lv .async_call ( lambda l : self .status_label .set_text ( "Update finished! Please restart." ), None )
186+ lv .update_ui_threadsafe_if_foreground ( self .status_label .set_text , "Update finished! Please restart." )
191187if not simulate :# if the update was completely installed
192188next_partition .set_boot ()
193189import machine
@@ -196,12 +192,11 @@ def update_with_lvgl(self, url):
196192else :
197193print ("This is an OSUpdate simulation, not attempting to restart the device." )
198194else :
199- lv . async_call ( lambda l : self .status_label .set_text ( f"Wrote{ bytes_written } <{ total_size } so not enough!" ), None )
200- self .install_button .remove_state ( lv .STATE .DISABLED )# allow retry
195+ self . update_ui_threadsafe_if_foreground ( self .status_label .set_text , f"Wrote{ bytes_written } <{ total_size } so not enough!" )
196+ self .update_ui_threadsafe_if_foreground ( self . install_button .remove_state , lv .STATE .DISABLED )# allow retry
201197except Exception as e :
202- if self .keep_running :
203- lv .async_call (lambda l :self .status_label .set_text (f"Update error:{ e } " ),None )
204- self .install_button .remove_state (lv .STATE .DISABLED )# allow retry
198+ self .update_ui_threadsafe_if_foreground (self .status_label .set_text ,f"Update error:{ e } " )
199+ self .update_ui_threadsafe_if_foreground (self .install_button .remove_state ,lv .STATE .DISABLED )
205200
206201# Non-class functions:
207202