Naive approach crashes too

This commit is contained in:
Thomas Farstrike
2025-04-19 20:48:36 +02:00
parent c617a611c6
commit 8db9d77ba9
+23 -67
View File
@@ -159,43 +159,11 @@ display.set_rotation(lv.DISPLAY_ROTATION._90)
import lvgl as lv
import _thread
import utime
import gc
# Create a list and lock for scheduling LVGL tasks
lvgl_task_queue = []
queue_lock = _thread.allocate_lock()
# 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)
if task is None:
utime.sleep_ms(10) # Avoid tight loop
continue
try:
print("Processing task")
task()
print("Task completed")
except Exception as e:
print("LVGL task error:", e)
gc.collect()
print("Task processor free memory:", gc.mem_free())
# 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(120, 100) # Larger for visibility
subwindow.set_size(120, 100)
subwindow.align(lv.ALIGN.TOP_LEFT, 10, 10)
subwindow.set_style_bg_color(lv.color_hex(0xDDDDDD), lv.PART.MAIN)
@@ -208,14 +176,9 @@ parent_label.align(lv.ALIGN.BOTTOM_MID, 0, -10)
def execute_script(script_source, lvgl_obj):
def thread_func():
try:
def schedule_lvgl_task(task):
with queue_lock:
print("Queuing task")
lvgl_task_queue.append(task)
script_globals = {
'lv': lv,
'subwindow': lvgl_obj,
'schedule_lvgl_task': schedule_lvgl_task,
'utime': utime
}
print("Child thread: Executing script")
@@ -229,37 +192,23 @@ def execute_script(script_source, lvgl_obj):
except Exception as e:
print("Error starting child thread:", e)
# Child script buffer: updates a label every 2 seconds
# Child script buffer: directly updates a label every second (unsafe)
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:
count += 1
print("Child thread: Preparing update", count)
def set_label_text():
print("Child task: Updating label to", count)
label.set_text(f"Child: {count}")
schedule_lvgl_task(set_label_text)
utime.sleep_ms(2000)
schedule_lvgl_task(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:
count += 1
print("Child thread: Updating label to", count)
label.set_text(f"Child: {count}") # Direct LVGL call (unsafe)
utime.sleep_ms(1000)
"""
execute_script(script_buffer, subwindow)
# 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())
count = 0
while True:
count += 1
@@ -268,9 +217,16 @@ while True:
parent_label.set_text(f"Parent: {count}")
except Exception as e:
print("Parent label error:", e)
gc.collect()
print("Main thread free memory:", gc.mem_free())
utime.sleep_ms(1000) # Parent updates every ~1s
utime.sleep_ms(1000)