From 976be2326b9b504d82e2c65c9d42ab9c9a1f6e9e Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Sun, 25 May 2025 12:58:57 +0200 Subject: [PATCH] fix more apps --- .../apps/com.example.cputest/assets/cputest.py | 12 ++++++++---- .../apps/com.example.memtest/assets/memtest.py | 12 ++++++++---- .../apps/com.example.osupdate/assets/osupdate.py | 3 ++- .../apps/com.example.wificonf/assets/wificonf.py | 6 +++--- internal_filesystem/builtin/system/button.py | 4 +++- internal_filesystem/lib/mpos/apps.py | 8 ++++++++ internal_filesystem/lib/threading.py | 3 ++- internal_filesystem/main.py | 13 +------------ 8 files changed, 35 insertions(+), 26 deletions(-) diff --git a/internal_filesystem/apps/com.example.cputest/assets/cputest.py b/internal_filesystem/apps/com.example.cputest/assets/cputest.py index 08e4b7b5..e319e21d 100644 --- a/internal_filesystem/apps/com.example.cputest/assets/cputest.py +++ b/internal_filesystem/apps/com.example.cputest/assets/cputest.py @@ -36,6 +36,9 @@ import hashlib import os import _thread +import mpos.apps +import mpos.ui + keeprunning = True summary = "Running 3 CPU tests...\n\n" @@ -96,19 +99,20 @@ def stress_test_thread(): def janitor_cb(timer): global keeprunning - if lv.screen_active() != appscreen: + if lv.screen_active() != main_screen: print("cputest.py backgrounded, cleaning up...") janitor.delete() keeprunning = False update_status_timer.delete() -appscreen = lv.screen_active() +main_screen = lv.obj() janitor = lv.timer_create(janitor_cb, 1000, None) update_status_timer = lv.timer_create(lambda timer: status.set_text(summary), 750, None) -status = lv.label(appscreen) +status = lv.label(main_screen) status.align(lv.ALIGN.TOP_LEFT, 5, 10) status.set_text(summary) +mpos.ui.load_screen(main_screen) -_thread.stack_size(12*1024) +_thread.stack_size(mpos.apps.good_stack_size()) _thread.start_new_thread(stress_test_thread, ()) diff --git a/internal_filesystem/apps/com.example.memtest/assets/memtest.py b/internal_filesystem/apps/com.example.memtest/assets/memtest.py index 75a2ac03..bc84efa2 100644 --- a/internal_filesystem/apps/com.example.memtest/assets/memtest.py +++ b/internal_filesystem/apps/com.example.memtest/assets/memtest.py @@ -32,6 +32,9 @@ import gc import time import _thread +import mpos.apps +import mpos.ui + # Configuration ALLOCATION_TIMEOUT_MS = 100 # Timeout for a single allocation (in milliseconds) keep_running = True @@ -110,18 +113,19 @@ def stress_test_thread(): def janitor_cb(timer): global keep_running - if lv.screen_active() != appscreen: + if lv.screen_active() != main_screen: print("memtest.py backgrounded, cleaning up...") janitor.delete() keep_running = False -appscreen = lv.screen_active() -status = lv.label(appscreen) +main_screen = lv.obj() +status = lv.label(main_screen) status.align(lv.ALIGN.TOP_LEFT, 5, 10) status.set_style_text_color(lv.color_hex(0xFFFFFF), 0) status.set_style_text_font(lv.font_unscii_8, 0) +mpos.ui.load_screen(main_screen) -_thread.stack_size(12*1024) +_thread.stack_size(mpos.apps.good_stack_size()) _thread.start_new_thread(stress_test_thread, ()) janitor = lv.timer_create(janitor_cb, 400, None) diff --git a/internal_filesystem/builtin/apps/com.example.osupdate/assets/osupdate.py b/internal_filesystem/builtin/apps/com.example.osupdate/assets/osupdate.py index cb9b846a..41712a9e 100644 --- a/internal_filesystem/builtin/apps/com.example.osupdate/assets/osupdate.py +++ b/internal_filesystem/builtin/apps/com.example.osupdate/assets/osupdate.py @@ -4,6 +4,7 @@ import ujson import time import _thread +import mpos.apps import mpos.info import mpos.ui @@ -91,7 +92,7 @@ def update_with_lvgl(url): def install_button_click(download_url): print(f"install_button_click for url {download_url}") try: - _thread.stack_size(12*1024) + _thread.stack_size(mpos.apps.good_stack_size()) _thread.start_new_thread(update_with_lvgl, (download_url,)) except Exception as e: print("Could not start update_with_lvgl thread: ", e) diff --git a/internal_filesystem/builtin/apps/com.example.wificonf/assets/wificonf.py b/internal_filesystem/builtin/apps/com.example.wificonf/assets/wificonf.py index 0872008c..116574d3 100644 --- a/internal_filesystem/builtin/apps/com.example.wificonf/assets/wificonf.py +++ b/internal_filesystem/builtin/apps/com.example.wificonf/assets/wificonf.py @@ -4,6 +4,7 @@ import time import lvgl as lv import _thread +import mpos.apps import mpos.ui # Screens: @@ -99,7 +100,7 @@ def start_scan_networks(): busy_scanning = True scan_button.remove_flag(lv.obj.FLAG.CLICKABLE) scan_button_label.set_text(scan_button_scanning_text) - _thread.stack_size(12*1024) + _thread.stack_size(get_good_stacksize()) _thread.start_new_thread(scan_networks_thread, ()) @@ -144,7 +145,7 @@ def start_attempt_connecting(ssid,password): print("Not attempting connect because busy_connecting.") else: busy_connecting = True - _thread.stack_size(12*1024) + _thread.stack_size(get_good_stacksize()) _thread.start_new_thread(attempt_connecting_thread, (ssid,password)) def show_error(message): @@ -295,7 +296,6 @@ def cancel_cb(event): print("Deleting password screen...") password_page.delete() print("cancel_cb: Restoring main_screen") - #mpos.ui.load_screen(main_screen) mpos.ui.back_screen() def create_ui(): diff --git a/internal_filesystem/builtin/system/button.py b/internal_filesystem/builtin/system/button.py index 01f0feff..6a8cf0b1 100644 --- a/internal_filesystem/builtin/system/button.py +++ b/internal_filesystem/builtin/system/button.py @@ -4,6 +4,8 @@ from machine import Pin, Timer import time import _thread +import mpos.apps + # Configure IO0 as input with pull-up resistor button = Pin(0, Pin.IN, Pin.PULL_UP) @@ -35,7 +37,7 @@ def on_long_press(t): # Callback for when long press duration is reached. timer.deinit() # Stop the timer global is_pressed if is_pressed and button.value() == 0: # Ensure button is still pressed - _thread.stack_size(12*1024) + _thread.stack_size(mpos.apps.good_stack_size()) _thread.start_new_thread(handle_long_press, ()) else: is_pressed = False diff --git a/internal_filesystem/lib/mpos/apps.py b/internal_filesystem/lib/mpos/apps.py index a6addec5..9489bd1f 100644 --- a/internal_filesystem/lib/mpos/apps.py +++ b/internal_filesystem/lib/mpos/apps.py @@ -11,6 +11,13 @@ import mpos.apps import mpos.info import mpos.ui +def good_stack_size(): + stacksize = 24*1024 + import sys + if sys.platform == "esp32": + stacksize = 16*1024 + return stacksize + # Run the script in the current thread: def execute_script(script_source, is_file, cwd=None): thread_id = _thread.get_ident() @@ -64,6 +71,7 @@ def execute_script_new_thread(scriptname, is_file): stack=24*1024 # this doesn't do anything because it's all started in the same thread else: stack=16*1024 # 16KB doesn't seem to be enough for the AppStore app on desktop + stack = mpos.apps.good_stack_size() print(f"app.py: setting stack size for script to {stack}") _thread.stack_size(stack) _thread.start_new_thread(execute_script, (scriptname, is_file)) diff --git a/internal_filesystem/lib/threading.py b/internal_filesystem/lib/threading.py index 92e92755..f7b1a4d5 100644 --- a/internal_filesystem/lib/threading.py +++ b/internal_filesystem/lib/threading.py @@ -18,7 +18,8 @@ class Thread: #stacksize = 12*1024 # small stack sizes 8KB gives segfault directly # 22KB or less is too tight on desktop, 23KB and more is fine - stacksize = 24*1024 + #stacksize = 24*1024 + stacksize = mpos.apps.good_stack_size() #stacksize = 20*1024 print(f"starting thread with stacksize {stacksize}") _thread.stack_size(stacksize) diff --git a/internal_filesystem/main.py b/internal_filesystem/main.py index 42de6297..394463f4 100644 --- a/internal_filesystem/main.py +++ b/internal_filesystem/main.py @@ -17,18 +17,7 @@ except Exception as e: from mpos import apps apps.execute_script("builtin/system/button.py", True) # Install button handler through IRQ -def dummy(): - pass - -import sys -if sys.platform == "esp32": - apps.auto_connect() -else: - # On unix/desktop, at least 24KB of stack size is needed in the REPL to make nostr work there - #import _thread - #_thread.stack_size(24*1024) # on unix/desktop, at least 24KB is needed for doing nostr connections from the REPL - #_thread.start_new_thread(dummy, ()) # not just setting the stack_size but actually starting a thread to apply this globally - pass +apps.auto_connect() apps.restart_launcher()