Skip to content

Commit ce57c1e

Browse files
Activity.py: limit async_call()s
1 parent e4d1b5a commit ce57c1e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

internal_filesystem/lib/mpos/app/activity.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import lvgl as lv
22
import mpos.ui
3+
import time
34

45
class Activity:
56

@@ -15,6 +16,8 @@ def onStart(self, screen):
1516
pass
1617
def onResume(self, screen): # app gets foreground
1718
self._has_foreground = True
19+
mpos.ui.th.add_event_cb(self.thc, 2)
20+
1821
def onPause(self, screen): # app goes to background
1922
self._has_foreground = False
2023
def onStop(self, screen):
@@ -62,15 +65,27 @@ def finish(self):
6265
def has_foreground(self):
6366
return 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
6673
def if_foreground(self, func, *args, **kwargs):
6774
if 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
6978
else:
70-
print(f"[if_foreground] Skipped {func} because _has_foreground=False")
79+
#print(f"[if_foreground] Skipped {func} because _has_foreground=False")
7180
return 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

0 commit comments

Comments
 (0)