Skip to content

Commit

Permalink
fix(init/deinit): Properly init/deinit lvgl module.
Browse files Browse the repository at this point in the history
Properly handle root pointers on lvgl init/deinit which fixes
init error after a soft reset (see #343).
  • Loading branch information
Carglglz committed Jul 21, 2024
1 parent 53f858c commit edaa4c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
44 changes: 41 additions & 3 deletions gen/gen_mpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit edaa4c3

Please sign in to comment.