From f76c25fbc5a19071ebc693c3d03b8aa04a5296bd 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 +++++++++++++++++++++++++++++++++++++++++--- lv_conf.h | 2 ++ lvgl | 2 +- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/gen/gen_mpy.py b/gen/gen_mpy.py index 22756548e..44e1961cb 100644 --- a/gen/gen_mpy.py +++ b/gen/gen_mpy.py @@ -1337,18 +1337,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 { @@ -3621,6 +3657,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/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 90a4f00f7..00d07390e 160000 --- a/lvgl +++ b/lvgl @@ -1 +1 @@ -Subproject commit 90a4f00f727a247fa41cfa954a0112de25af379a +Subproject commit 00d07390edbab7c13bdad83ed0b389616cb4d96b