From ce57c1e841b642903f672d0126ff966c2bdc2fae Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Thu, 6 Nov 2025 14:07:58 +0100 Subject: [PATCH] Activity.py: limit async_call()s --- internal_filesystem/lib/mpos/app/activity.py | 23 ++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/internal_filesystem/lib/mpos/app/activity.py b/internal_filesystem/lib/mpos/app/activity.py index 4a2ce4b6..ab4d572c 100644 --- a/internal_filesystem/lib/mpos/app/activity.py +++ b/internal_filesystem/lib/mpos/app/activity.py @@ -1,5 +1,6 @@ import lvgl as lv import mpos.ui +import time class Activity: @@ -15,6 +16,8 @@ class Activity: pass def onResume(self, screen): # app gets foreground self._has_foreground = True + mpos.ui.th.add_event_cb(self.thc, 2) + def onPause(self, screen): # app goes to background self._has_foreground = False def onStop(self, screen): @@ -62,15 +65,27 @@ class Activity: def has_foreground(self): return self._has_foreground + def thc(self, a, b): + #print("thc called") + self.counter = 0 + # Execute a function if the Activity is in the foreground def if_foreground(self, func, *args, **kwargs): if self._has_foreground: - return func(*args, **kwargs) + print(f"executing {func} with args {args} and kwargs {kwargs}") + result = func(*args, **kwargs) + return result else: - print(f"[if_foreground] Skipped {func} because _has_foreground=False") + #print(f"[if_foreground] Skipped {func} because _has_foreground=False") return None + counter=0 # Update the UI in a threadsafe way if the Activity is in the foreground - def update_ui_threadsafe_if_foreground(self, func, *args, **kwargs): + def update_ui_threadsafe_if_foreground(self, func, *args, important=False, **kwargs): + self.counter += 1 + if not important and self.counter > 250: + #print(f"skipping because self.counter is {self.counter} and not important") + return # lv.async_call() is needed to update the UI from another thread than the main one (as LVGL is not thread safe) - lv.async_call(lambda _: self.if_foreground(func, *args, **kwargs),None) + result = lv.async_call(lambda _: self.if_foreground(func, *args, **kwargs),None) + return result