From f071178d6ad2fb5193f50d10917873b2476c042d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Van=C4=9Bk?= <linuxtardis@gmail.com> Date: Sun, 28 May 2023 22:38:06 +0200 Subject: [PATCH] Turn ICM20608 off after being garbage-collected --- micropython/modules/opencube_brick/ICM20608_api.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/micropython/modules/opencube_brick/ICM20608_api.c b/micropython/modules/opencube_brick/ICM20608_api.c index 7a216c3..81a83a7 100644 --- a/micropython/modules/opencube_brick/ICM20608_api.c +++ b/micropython/modules/opencube_brick/ICM20608_api.c @@ -17,7 +17,7 @@ static void ICM20608_print(const mp_print_t *print, mp_obj_t self_in); // Called on ICM20608 init STATIC mp_obj_t ICM20608_make_new(const mp_obj_type_t* type, size_t n_args, size_t n_kw, const mp_obj_t* args) { - ICM20608_obj_t *self = m_new_obj(ICM20608_obj_t); + ICM20608_obj_t *self = m_new_obj_with_finaliser(ICM20608_obj_t); self->base.type = type; self->icm = (ICM20608G_t*)malloc(sizeof(ICM20608G_t)); opencube_lock_i2c_or_raise(); @@ -31,7 +31,11 @@ static void ICM20608_print(const mp_print_t *print, mp_obj_t self_in) { } STATIC mp_obj_t ICM20608_read_value(mp_obj_t self_in) { - ICM20608_obj_t *self = MP_OBJ_TO_PTR(self_in); + ICM20608_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self->icm == NULL) { + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("ICM20608 has been deinitialized already")); + } + float accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z; opencube_lock_i2c_or_raise(); ICM20608G_read_data(self->icm, @@ -63,6 +67,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(ICM20608_deinit_obj, ICM20608_deinit); STATIC const mp_rom_map_elem_t ICM20608_locals_dict[] = { { MP_ROM_QSTR(MP_QSTR_read_value), MP_ROM_PTR(&ICM20608_read_value_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&ICM20608_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&ICM20608_deinit_obj) }, }; STATIC MP_DEFINE_CONST_DICT(ICM20608_locals_dict_obj, ICM20608_locals_dict); -- GitLab