11import lvgl as lv
22import mpos .ui
3+ import time
34
45class Activity :
56
@@ -15,6 +16,8 @@ def onStart(self, screen):
1516pass
1617def onResume (self ,screen ):# app gets foreground
1718self ._has_foreground = True
19+ mpos .ui .th .add_event_cb (self .thc ,2 )
20+
1821def onPause (self ,screen ):# app goes to background
1922self ._has_foreground = False
2023def onStop (self ,screen ):
@@ -62,15 +65,27 @@ def finish(self):
6265def has_foreground (self ):
6366return self ._has_foreground
6467
68+ def thc (self ,a ,b ):
69+ #print("thc called")
70+ self .counter = 0
71+
6572# Execute a function if the Activity is in the foreground
6673def if_foreground (self ,func ,* args ,** kwargs ):
6774if self ._has_foreground :
68- return func (* args ,** kwargs )
75+ print (f"executing{ func } with args{ args } and kwargs{ kwargs } " )
76+ result = func (* args ,** kwargs )
77+ return result
6978else :
70- print (f"[if_foreground] Skipped{ func } because _has_foreground=False" )
79+ # print(f"[if_foreground] Skipped {func} because _has_foreground=False")
7180return None
7281
82+ counter = 0
7383# Update the UI in a threadsafe way if the Activity is in the foreground
74- def update_ui_threadsafe_if_foreground (self ,func ,* args ,** kwargs ):
84+ def update_ui_threadsafe_if_foreground (self ,func ,* args ,important = False ,** kwargs ):
85+ self .counter += 1
86+ if not important and self .counter > 250 :
87+ #print(f"skipping because self.counter is {self.counter} and not important")
88+ return
7589# lv.async_call() is needed to update the UI from another thread than the main one (as LVGL is not thread safe)
76- lv .async_call (lambda _ :self .if_foreground (func ,* args ,** kwargs ),None )
90+ result = lv .async_call (lambda _ :self .if_foreground (func ,* args ,** kwargs ),None )
91+ return result