From 494a362b07796290cb0b3eae36eff27f1cbf07e3 Mon Sep 17 00:00:00 2001 From: Carlosgg Date: Mon, 24 Jun 2024 02:21:18 +0100 Subject: [PATCH] fix(init/deinit): Properly init/deinit lvgl module. Properly handle root pointers on lvgl init/deinit which fixes init error after a soft reset (see #343). --- gen/gen_mpy.py | 44 +++++++++++++++++++++++++++++++++++++++++--- lib/lv_utils.py | 1 - lv_conf.h | 2 ++ lvgl | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/gen/gen_mpy.py b/gen/gen_mpy.py index af729e479..ac63021a8 100644 --- a/gen/gen_mpy.py +++ b/gen/gen_mpy.py @@ -1108,18 +1108,54 @@ def register_int_ptr_type(convertor, *types): // Register LVGL root pointers MP_REGISTER_ROOT_POINTER(void *mp_lv_roots); MP_REGISTER_ROOT_POINTER(void *mp_lv_user_data); +MP_REGISTER_ROOT_POINTER(int mp_lv_roots_initialized); +MP_REGISTER_ROOT_POINTER(int lvgl_mod_initialized); void *mp_lv_roots; +void *mp_lv_user_data; +int mp_lv_roots_initialized = 0; +int lvgl_mod_initialized = 0; void mp_lv_init_gc() { - static bool mp_lv_roots_initialized = false; - if (!mp_lv_roots_initialized) { + if (!MP_STATE_VM(mp_lv_roots_initialized)) { + // mp_printf(&mp_plat_print, "[ INIT GC ]"); mp_lv_roots = MP_STATE_VM(mp_lv_roots) = m_new0(lv_global_t, 1); - mp_lv_roots_initialized = true; + mp_lv_roots_initialized = MP_STATE_VM(mp_lv_roots_initialized) = 1; } } +void mp_lv_deinit_gc() +{ + + // mp_printf(&mp_plat_print, "[ DEINIT GC ]"); + mp_lv_roots = MP_STATE_VM(mp_lv_roots) = NULL; + mp_lv_user_data = MP_STATE_VM(mp_lv_user_data) = NULL; + mp_lv_roots_initialized = MP_STATE_VM(mp_lv_roots_initialized) = 0; + lvgl_mod_initialized = MP_STATE_VM(lvgl_mod_initialized) = 0; + +} + +static mp_obj_t lvgl_mod___init__(void) { + if (!MP_STATE_VM(lvgl_mod_initialized)) { + // __init__ for builtins is called each time the module is imported, + // so ensure that initialisation only happens once. + MP_STATE_VM(lvgl_mod_initialized) = true; + lv_init(); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_0(lvgl_mod___init___obj, lvgl_mod___init__); + + +static mp_obj_t lvgl_mod___del__(void) { + if (MP_STATE_VM(lvgl_mod_initialized)) { + lv_deinit(); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_0(lvgl_mod___del___obj, lvgl_mod___del__); + #else // LV_OBJ_T typedef struct mp_lv_obj_type_t { @@ -2982,6 +3018,8 @@ def generate_struct_functions(struct_list): static const mp_rom_map_elem_t {module_name}_globals_table[] = {{ {{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_{module_name}) }}, + {{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&lvgl_mod___init___obj) }}, + {{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&lvgl_mod___del___obj) }}, {objects} {functions} {enums} diff --git a/lib/lv_utils.py b/lib/lv_utils.py index 83d3fe95c..259074e3a 100644 --- a/lib/lv_utils.py +++ b/lib/lv_utils.py @@ -180,4 +180,3 @@ async def async_timer(self): def default_exception_sink(self, e): sys.print_exception(e) - event_loop.current_instance().deinit() diff --git a/lv_conf.h b/lv_conf.h index c2d4e6c59..e624057bc 100644 --- a/lv_conf.h +++ b/lv_conf.h @@ -299,7 +299,9 @@ /*Garbage Collector settings *Used if LVGL is bound to higher level language and the memory is managed by that language*/ extern void mp_lv_init_gc(); +extern void mp_lv_deinit_gc(); #define LV_GC_INIT() mp_lv_init_gc() +#define LV_GC_DEINIT() mp_lv_deinit_gc() #define LV_ENABLE_GLOBAL_CUSTOM 1 #if LV_ENABLE_GLOBAL_CUSTOM diff --git a/lvgl b/lvgl index 905de3d7c..00d07390e 160000 --- a/lvgl +++ b/lvgl @@ -1 +1 @@ -Subproject commit 905de3d7c81367608577e1e38d032831a7871c16 +Subproject commit 00d07390edbab7c13bdad83ed0b389616cb4d96b