diff --git a/appstore.mpy b/appstore.mpy index 0e6f1f79..07196605 100644 --- a/appstore.mpy +++ b/appstore.mpy @@ -165,30 +165,41 @@ import gc lvgl_task_queue = [] queue_lock = _thread.allocate_lock() -# Function to process LVGL tasks from the queue in the main thread +# Function to process LVGL tasks (runs in a separate thread) def process_lvgl_tasks(): while True: task = None with queue_lock: if lvgl_task_queue: - task = lvgl_task_queue.pop(0) # Remove and get the first task + task = lvgl_task_queue.pop(0) if task is None: - break + utime.sleep_ms(10) # Avoid tight loop + continue try: - print("Processing task") # Debug: confirm task execution + print("Processing task") task() - print("Task completed") # Debug: confirm task success + print("Task completed") except Exception as e: print("LVGL task error:", e) + gc.collect() + print("Task processor free memory:", gc.mem_free()) -# Create a subwindow (container) for the child script +# Function to run task processor in a dedicated thread +def task_processor_thread(): + print("Task processor thread started") + try: + process_lvgl_tasks() + except Exception as e: + print("Task processor error:", e) + +# Create a subwindow for the child script screen = lv.screen_active() subwindow = lv.obj(screen) -subwindow.set_size(100, 80) # Slightly larger for visibility +subwindow.set_size(120, 100) # Larger for visibility subwindow.align(lv.ALIGN.TOP_LEFT, 10, 10) subwindow.set_style_bg_color(lv.color_hex(0xDDDDDD), lv.PART.MAIN) -# Create a label on the main screen for parent updates +# Create a label for parent updates parent_label = lv.label(screen) parent_label.set_text("Parent: 0") parent_label.align(lv.ALIGN.BOTTOM_MID, 0, -10) @@ -197,12 +208,10 @@ parent_label.align(lv.ALIGN.BOTTOM_MID, 0, -10) def execute_script(script_source, lvgl_obj): def thread_func(): try: - # Define a function to schedule LVGL tasks from the child thread def schedule_lvgl_task(task): with queue_lock: - print("Queuing task") # Debug + print("Queuing task") lvgl_task_queue.append(task) - # Create a dictionary for the script's globals script_globals = { 'lv': lv, 'subwindow': lvgl_obj, @@ -213,7 +222,6 @@ def execute_script(script_source, lvgl_obj): exec(script_source, script_globals) except Exception as e: print("Child thread error:", e) - # Start the thread with smaller stack size try: _thread.stack_size(4096) _thread.start_new_thread(thread_func, ()) @@ -221,12 +229,13 @@ def execute_script(script_source, lvgl_obj): except Exception as e: print("Error starting child thread:", e) -# Child script buffer: updates a label in the subwindow every 2 seconds +# Child script buffer: updates a label every 2 seconds script_buffer = """ def update_child(): print("Child thread: Creating label") label = lv.label(subwindow) label.set_text("Child: 0") + label.set_style_text_font(lv.font_montserrat_12, 0) # Smaller font label.align(lv.ALIGN.CENTER, 0, 0) count = 0 while True: @@ -241,9 +250,16 @@ schedule_lvgl_task(update_child) """ execute_script(script_buffer, subwindow) -# Main loop: update parent label every second and process LVGL tasks +# Start task processor thread +try: + #_thread.stack_size(4096) + _thread.start_new_thread(task_processor_thread, ()) +except Exception as e: + print("Error starting task processor thread:", e) + +# Main loop: update parent label every second gc.collect() -print("Free memory before loop:", gc.mem_free()) # Debug: check SRAM +print("Free memory before loop:", gc.mem_free()) count = 0 while True: count += 1 @@ -252,11 +268,9 @@ while True: parent_label.set_text(f"Parent: {count}") except Exception as e: print("Parent label error:", e) - process_lvgl_tasks() gc.collect() - print("Free memory:", gc.mem_free()) # Debug: monitor memory - utime.sleep_ms(50) - + print("Main thread free memory:", gc.mem_free()) + utime.sleep_ms(1000) # Parent updates every ~1s