uasyncio works!

This commit is contained in:
Thomas Farstrike
2025-04-19 20:50:36 +02:00
parent 8db9d77ba9
commit 8bd7919f0b
+68 -47
View File
@@ -149,16 +149,21 @@ display.set_backlight(100)
#indev=cst816s.CST816S(touch_dev,startup_rotation=lv.DISPLAY_ROTATION._180) # button in top left, good
th = task_handler.TaskHandler()
display.set_rotation(lv.DISPLAY_ROTATION._90)
# trying out script running
import lvgl as lv
import _thread
import uasyncio as asyncio
import utime
import gc
# Create a subwindow for the child script
screen = lv.screen_active()
@@ -172,59 +177,75 @@ parent_label = lv.label(screen)
parent_label.set_text("Parent: 0")
parent_label.align(lv.ALIGN.BOTTOM_MID, 0, -10)
# Function to execute the child script in a separate thread
def execute_script(script_source, lvgl_obj):
def thread_func():
try:
script_globals = {
'lv': lv,
'subwindow': lvgl_obj,
'utime': utime
}
print("Child thread: Executing script")
exec(script_source, script_globals)
except Exception as e:
print("Child thread error:", e)
# Function to execute the child script as a coroutine
async def execute_script(script_source, lvgl_obj):
try:
_thread.stack_size(4096)
_thread.start_new_thread(thread_func, ())
print("Child thread started")
# Create globals for the script
script_globals = {
'lv': lv,
'subwindow': lvgl_obj,
'asyncio': asyncio,
'utime': utime
}
print("Child script: Compiling")
# Compile and execute the script
code = compile(script_source, "<string>", "exec")
exec(code, script_globals)
# Assume the script defines an async function 'update_child'
update_child = script_globals.get('update_child')
if update_child:
print("Child script: Starting update_child")
await update_child()
else:
print("Child script error: No update_child function defined")
except Exception as e:
print("Error starting child thread:", e)
print("Child script error:", e)
# Child script buffer: directly updates a label every second (unsafe)
# Child script buffer: updates a label every 2 seconds
script_buffer = """
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)
import asyncio
async def update_child():
print("Child coroutine: 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 coroutine: Updating label to", count)
label.set_text(f"Child: {count}")
await asyncio.sleep_ms(2000) # Update every 2s
"""
execute_script(script_buffer, subwindow)
# Main loop: update parent label every second
count = 0
while True:
count += 1
print("Main thread: Updating parent to", count)
try:
# Parent coroutine: updates parent label every 1 second
async def update_parent():
count = 0
while True:
count += 1
print("Parent coroutine: Updating label to", count)
parent_label.set_text(f"Parent: {count}")
except Exception as e:
print("Parent label error:", e)
utime.sleep_ms(1000)
gc.collect()
print("Parent coroutine: Free memory:", gc.mem_free())
await asyncio.sleep_ms(1000) # Update every 1s
# Main async function to run all tasks
async def main():
print("Main: Starting tasks")
# Start parent and child tasks
asyncio.create_task(update_parent())
asyncio.create_task(execute_script(script_buffer, subwindow))
# Keep the event loop running
while True:
await asyncio.sleep_ms(100) # Yield to other tasks
# Run the event loop
gc.collect()
print("Free memory before loop:", gc.mem_free())
try:
asyncio.run(main())
except Exception as e:
print("Main error:", e)