1010
1111class OSUpdate (Activity ):
1212
13- keep_running = True
1413 download_update_url = None
1514
1615 # Widgets:
@@ -58,9 +57,6 @@ def onStart(self, screen):
5857 print ("Showing update info..." )
5958 self .show_update_info ()
6059
61- def onStop (self , screen ):
62- self .keep_running = False # this is checked by the update_with_lvgl thread
63-
6460 def show_update_info (self ):
6561 self .status_label .set_text ("Checking for OS updates..." )
6662 hwid = mpos .info .get_hardware_id ()
@@ -140,8 +136,8 @@ def force_update_clicked(self):
140136
141137 def progress_callback (self , percent ):
142138 print (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 } %" )
145141 time .sleep_ms (100 )
146142
147143 # Custom OTA update with LVGL progress
@@ -168,7 +164,7 @@ def update_with_lvgl(self, url):
168164 i = 0
169165 total_size = round_up_to_multiple (total_size , chunk_size )
170166 print (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
172168 time .sleep_ms (100 ) # don't hog the CPU
173169 chunk = response .raw .read (chunk_size )
174170 if not chunk :
@@ -187,7 +183,7 @@ def update_with_lvgl(self, url):
187183 response .close ()
188184 try :
189185 if 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." )
191187 if not simulate : # if the update was completely installed
192188 next_partition .set_boot ()
193189 import machine
@@ -196,12 +192,11 @@ def update_with_lvgl(self, url):
196192 else :
197193 print ("This is an OSUpdate simulation, not attempting to restart the device." )
198194 else :
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
201197 except 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
0 commit comments