Skip to content

Commit b0d87e9

Browse files
committed
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 lvgl#343).
1 parent 53f858c commit b0d87e9

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

gen/gen_mpy.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,18 +1337,54 @@ def register_int_ptr_type(convertor, *types):
13371337
// Register LVGL root pointers
13381338
MP_REGISTER_ROOT_POINTER(void *mp_lv_roots);
13391339
MP_REGISTER_ROOT_POINTER(void *mp_lv_user_data);
1340+
MP_REGISTER_ROOT_POINTER(int mp_lv_roots_initialized);
1341+
MP_REGISTER_ROOT_POINTER(int lvgl_mod_initialized);
13401342
13411343
void *mp_lv_roots;
1344+
void *mp_lv_user_data;
1345+
int mp_lv_roots_initialized = 0;
1346+
int lvgl_mod_initialized = 0;
13421347
13431348
void mp_lv_init_gc()
13441349
{
1345-
static bool mp_lv_roots_initialized = false;
1346-
if (!mp_lv_roots_initialized) {
1350+
if (!MP_STATE_VM(mp_lv_roots_initialized)) {
1351+
// mp_printf(&mp_plat_print, "[ INIT GC ]");
13471352
mp_lv_roots = MP_STATE_VM(mp_lv_roots) = m_new0(lv_global_t, 1);
1348-
mp_lv_roots_initialized = true;
1353+
mp_lv_roots_initialized = MP_STATE_VM(mp_lv_roots_initialized) = 1;
13491354
}
13501355
}
13511356
1357+
void mp_lv_deinit_gc()
1358+
{
1359+
1360+
// mp_printf(&mp_plat_print, "[ DEINIT GC ]");
1361+
mp_lv_roots = MP_STATE_VM(mp_lv_roots) = NULL;
1362+
mp_lv_user_data = MP_STATE_VM(mp_lv_user_data) = NULL;
1363+
mp_lv_roots_initialized = MP_STATE_VM(mp_lv_roots_initialized) = 0;
1364+
lvgl_mod_initialized = MP_STATE_VM(lvgl_mod_initialized) = 0;
1365+
1366+
}
1367+
1368+
static mp_obj_t lvgl_mod___init__(void) {
1369+
if (!MP_STATE_VM(lvgl_mod_initialized)) {
1370+
// __init__ for builtins is called each time the module is imported,
1371+
// so ensure that initialisation only happens once.
1372+
MP_STATE_VM(lvgl_mod_initialized) = true;
1373+
lv_init();
1374+
}
1375+
return mp_const_none;
1376+
}
1377+
static MP_DEFINE_CONST_FUN_OBJ_0(lvgl_mod___init___obj, lvgl_mod___init__);
1378+
1379+
1380+
static mp_obj_t lvgl_mod___del__(void) {
1381+
if (MP_STATE_VM(lvgl_mod_initialized)) {
1382+
lv_deinit();
1383+
}
1384+
return mp_const_none;
1385+
}
1386+
static MP_DEFINE_CONST_FUN_OBJ_0(lvgl_mod___del___obj, lvgl_mod___del__);
1387+
13521388
#else // LV_OBJ_T
13531389
13541390
typedef struct mp_lv_obj_type_t {
@@ -3621,6 +3657,8 @@ def generate_struct_functions(struct_list):
36213657
36223658
static const mp_rom_map_elem_t {module_name}_globals_table[] = {{
36233659
{{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_{module_name}) }},
3660+
{{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&lvgl_mod___init___obj) }},
3661+
{{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&lvgl_mod___del___obj) }},
36243662
{objects}
36253663
{functions}
36263664
{enums}

lv_conf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@
299299
/*Garbage Collector settings
300300
*Used if LVGL is bound to higher level language and the memory is managed by that language*/
301301
extern void mp_lv_init_gc();
302+
extern void mp_lv_deinit_gc();
302303
#define LV_GC_INIT() mp_lv_init_gc()
304+
#define LV_GC_DEINIT() mp_lv_deinit_gc()
303305

304306
#define LV_ENABLE_GLOBAL_CUSTOM 1
305307
#if LV_ENABLE_GLOBAL_CUSTOM

lvgl

Submodule lvgl updated 1 file

0 commit comments

Comments
 (0)