@@ -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 )} },
@@ -228,8 +230,9 @@ static mp_obj_t i2ctarget_i2c_target_request_make_new(const mp_obj_type_t *type,
228230//| """Close the request."""
229231//| ...
230232static mp_obj_t i2ctarget_i2c_target_request_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
231- mp_check_self (mp_obj_is_type (args [0 ], & i2ctarget_i2c_target_request_type ));
232233 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
234+ check_for_deinit (self -> target );
235+
233236 common_hal_i2ctarget_i2c_target_close (self -> target );
234237 return mp_const_none ;
235238}
@@ -238,26 +241,29 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(i2ctarget_i2c_target_request___exit__
238241//| address: int
239242//| """The I2C address of the request."""
240243static mp_obj_t i2ctarget_i2c_target_request_get_address (mp_obj_t self_in ) {
241- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
242244 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
245+ check_for_deinit (self -> target );
246+
243247 return mp_obj_new_int (self -> address );
244248}
245249MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_address_obj , i2ctarget_i2c_target_request_get_address );
246250
247251//| is_read: bool
248252//| """The I2C main controller is reading from this target."""
249253static mp_obj_t i2ctarget_i2c_target_request_get_is_read (mp_obj_t self_in ) {
250- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
251254 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
255+ check_for_deinit (self -> target );
256+
252257 return mp_obj_new_bool (self -> is_read );
253258}
254259MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_is_read_obj , i2ctarget_i2c_target_request_get_is_read );
255260
256261//| is_restart: bool
257262//| """Is Repeated Start Condition."""
258263static mp_obj_t i2ctarget_i2c_target_request_get_is_restart (mp_obj_t self_in ) {
259- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
260264 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
265+ check_for_deinit (self -> target );
266+
261267 return mp_obj_new_bool (self -> is_restart );
262268}
263269MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_is_restart_obj , i2ctarget_i2c_target_request_get_is_restart );
@@ -271,8 +277,9 @@ MP_DEFINE_CONST_PROP_GET(i2ctarget_i2c_target_request_is_restart_obj, i2ctarget_
271277//| :return: Bytes read"""
272278//| ...
273279static mp_obj_t i2ctarget_i2c_target_request_read (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
274- mp_check_self (mp_obj_is_type (pos_args [0 ], & i2ctarget_i2c_target_request_type ));
275280 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
281+ check_for_deinit (self -> target );
282+
276283 enum { ARG_n , ARG_ack };
277284 static const mp_arg_t allowed_args [] = {
278285 { MP_QSTR_n , MP_ARG_INT , {.u_int = -1 } },
@@ -328,8 +335,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(i2ctarget_i2c_target_request_read_obj, 1, i2ctarget_i
328335//| :return: Number of bytes written"""
329336//| ...
330337static mp_obj_t i2ctarget_i2c_target_request_write (mp_obj_t self_in , mp_obj_t buf_in ) {
331- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
332338 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
339+ check_for_deinit (self -> target );
333340
334341 if (!self -> is_read ) {
335342 mp_raise_OSError (MP_EACCES );
@@ -362,8 +369,9 @@ static MP_DEFINE_CONST_FUN_OBJ_2(i2ctarget_i2c_target_request_write_obj, i2ctarg
362369//| ...
363370//|
364371static mp_obj_t i2ctarget_i2c_target_request_ack (uint n_args , const mp_obj_t * args ) {
365- mp_check_self (mp_obj_is_type (args [0 ], & i2ctarget_i2c_target_request_type ));
366372 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
373+ check_for_deinit (self -> target );
374+
367375 bool ack = (n_args == 1 ) ? true : mp_obj_is_true (args [1 ]);
368376
369377 if (self -> is_read ) {
@@ -376,8 +384,8 @@ static mp_obj_t i2ctarget_i2c_target_request_ack(uint n_args, const mp_obj_t *ar
376384MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (i2ctarget_i2c_target_request_ack_obj , 1 , 2 , i2ctarget_i2c_target_request_ack );
377385
378386static mp_obj_t i2ctarget_i2c_target_request_close (mp_obj_t self_in ) {
379- mp_check_self (mp_obj_is_type (self_in , & i2ctarget_i2c_target_request_type ));
380387 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
388+ check_for_deinit (self -> target );
381389
382390 common_hal_i2ctarget_i2c_target_close (self -> target );
383391 return mp_const_none ;
0 commit comments