@@ -49,7 +49,7 @@ static mp_obj_t mp_obj_new_i2ctarget_i2c_target_request(i2ctarget_i2c_target_obj
4949//| :param bool smbus: Use SMBUS timings if the hardware supports it"""
5050//| ...
5151static mp_obj_t i2ctarget_i2c_target_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
52- i2ctarget_i2c_target_obj_t * self = mp_obj_malloc (i2ctarget_i2c_target_obj_t , & i2ctarget_i2c_target_type );
52+ i2ctarget_i2c_target_obj_t * self = mp_obj_malloc_with_finaliser (i2ctarget_i2c_target_obj_t , & i2ctarget_i2c_target_type );
5353 enum { ARG_scl , ARG_sda , ARG_addresses , ARG_smbus };
5454 static const mp_arg_t allowed_args [] = {
5555 { MP_QSTR_scl , MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -85,13 +85,18 @@ static mp_obj_t i2ctarget_i2c_target_make_new(const mp_obj_type_t *type, size_t
8585//| """Releases control of the underlying hardware so other classes can use it."""
8686//| ...
8787static mp_obj_t i2ctarget_i2c_target_obj_deinit (mp_obj_t self_in ) {
88- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_type ));
8988 i2ctarget_i2c_target_obj_t * self = MP_OBJ_TO_PTR (self_in );
9089 common_hal_i2ctarget_i2c_target_deinit (self );
9190 return mp_const_none ;
9291}
9392MP_DEFINE_CONST_FUN_OBJ_1 (i2ctarget_i2c_target_deinit_obj , i2ctarget_i2c_target_obj_deinit );
9493
94+ static void check_for_deinit (i2ctarget_i2c_target_obj_t * self ) {
95+ if (common_hal_i2ctarget_i2c_target_deinited (self )) {
96+ raise_deinited_error ();
97+ }
98+ }
99+
95100//| def __enter__(self) -> I2CTarget:
96101//| """No-op used in Context Managers."""
97102//| ...
@@ -102,7 +107,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(i2ctarget_i2c_target_deinit_obj, i2ctarget_i2c_target_
102107//| :ref:`lifetime-and-contextmanagers` for more info."""
103108//| ...
104109static mp_obj_t i2ctarget_i2c_target_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
105- mp_check_self (mp_obj_is_type (args [0 ], & i2ctarget_i2c_target_type ));
106110 i2ctarget_i2c_target_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
107111 common_hal_i2ctarget_i2c_target_deinit (self );
108112 return mp_const_none ;
@@ -117,11 +121,9 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(i2ctarget_i2c_target___exit___obj, 4,
117121//| :rtype: ~i2ctarget.I2CTargetRequest"""
118122//|
119123static mp_obj_t i2ctarget_i2c_target_request (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
120- mp_check_self (mp_obj_is_type (pos_args [0 ], & i2ctarget_i2c_target_type ));
121124 i2ctarget_i2c_target_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
122- if (common_hal_i2ctarget_i2c_target_deinited (self )) {
123- raise_deinited_error ();
124- }
125+ check_for_deinit (self );
126+
125127 enum { ARG_timeout };
126128 static const mp_arg_t allowed_args [] = {
127129 { MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NEW_SMALL_INT (-1 )} },
@@ -186,6 +188,7 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(i2ctarget_i2c_target_request_obj, 1, i2ctarget
186188
187189static const mp_rom_map_elem_t i2ctarget_i2c_target_locals_dict_table [] = {
188190 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& i2ctarget_i2c_target_deinit_obj ) },
191+ { MP_ROM_QSTR (MP_QSTR___del__ ), MP_ROM_PTR (& i2ctarget_i2c_target_deinit_obj ) },
189192 { MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& default___enter___obj ) },
190193 { MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& i2ctarget_i2c_target___exit___obj ) },
191194 { MP_ROM_QSTR (MP_QSTR_request ), MP_ROM_PTR (& i2ctarget_i2c_target_request_obj ) },
@@ -227,8 +230,9 @@ static mp_obj_t i2ctarget_i2c_target_request_make_new(const mp_obj_type_t *type,
227230//| """Close the request."""
228231//| ...
229232static mp_obj_t i2ctarget_i2c_target_request_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
230- mp_check_self (mp_obj_is_type (args [0 ], & i2ctarget_i2c_target_request_type ));
231233 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
234+ check_for_deinit (self -> target );
235+
232236 common_hal_i2ctarget_i2c_target_close (self -> target );
233237 return mp_const_none ;
234238}
@@ -237,26 +241,29 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(i2ctarget_i2c_target_request___exit__
237241//| address: int
238242//| """The I2C address of the request."""
239243static mp_obj_t i2ctarget_i2c_target_request_get_address (mp_obj_t self_in ) {
240- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
241244 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
245+ check_for_deinit (self -> target );
246+
242247 return mp_obj_new_int (self -> address );
243248}
244249MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_address_obj , i2ctarget_i2c_target_request_get_address );
245250
246251//| is_read: bool
247252//| """The I2C main controller is reading from this target."""
248253static mp_obj_t i2ctarget_i2c_target_request_get_is_read (mp_obj_t self_in ) {
249- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
250254 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
255+ check_for_deinit (self -> target );
256+
251257 return mp_obj_new_bool (self -> is_read );
252258}
253259MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_is_read_obj , i2ctarget_i2c_target_request_get_is_read );
254260
255261//| is_restart: bool
256262//| """Is Repeated Start Condition."""
257263static mp_obj_t i2ctarget_i2c_target_request_get_is_restart (mp_obj_t self_in ) {
258- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
259264 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
265+ check_for_deinit (self -> target );
266+
260267 return mp_obj_new_bool (self -> is_restart );
261268}
262269MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_is_restart_obj , i2ctarget_i2c_target_request_get_is_restart );
@@ -270,8 +277,9 @@ MP_DEFINE_CONST_PROP_GET(i2ctarget_i2c_target_request_is_restart_obj, i2ctarget_
270277//| :return: Bytes read"""
271278//| ...
272279static mp_obj_t i2ctarget_i2c_target_request_read (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
273- mp_check_self (mp_obj_is_type (pos_args [0 ], & i2ctarget_i2c_target_request_type ));
274280 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
281+ check_for_deinit (self -> target );
282+
275283 enum { ARG_n , ARG_ack };
276284 static const mp_arg_t allowed_args [] = {
277285 { MP_QSTR_n , MP_ARG_INT , {.u_int = -1 } },
@@ -327,8 +335,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(i2ctarget_i2c_target_request_read_obj, 1, i2ctarget_i
327335//| :return: Number of bytes written"""
328336//| ...
329337static mp_obj_t i2ctarget_i2c_target_request_write (mp_obj_t self_in , mp_obj_t buf_in ) {
330- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
331338 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
339+ check_for_deinit (self -> target );
332340
333341 if (!self -> is_read ) {
334342 mp_raise_OSError (MP_EACCES );
@@ -361,8 +369,9 @@ static MP_DEFINE_CONST_FUN_OBJ_2(i2ctarget_i2c_target_request_write_obj, i2ctarg
361369//| ...
362370//|
363371static mp_obj_t i2ctarget_i2c_target_request_ack (uint n_args , const mp_obj_t * args ) {
364- mp_check_self (mp_obj_is_type (args [0 ], & i2ctarget_i2c_target_request_type ));
365372 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
373+ check_for_deinit (self -> target );
374+
366375 bool ack = (n_args == 1 ) ? true : mp_obj_is_true (args [1 ]);
367376
368377 if (self -> is_read ) {
@@ -375,8 +384,8 @@ static mp_obj_t i2ctarget_i2c_target_request_ack(uint n_args, const mp_obj_t *ar
375384MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (i2ctarget_i2c_target_request_ack_obj , 1 , 2 , i2ctarget_i2c_target_request_ack );
376385
377386static mp_obj_t i2ctarget_i2c_target_request_close (mp_obj_t self_in ) {
378- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
379387 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
388+ check_for_deinit (self -> target );
380389
381390 common_hal_i2ctarget_i2c_target_close (self -> target );
382391 return mp_const_none ;
0 commit comments