@@ -49,7 +49,7 @@ static mp_obj_t mp_obj_new_i2ctarget_i2c_target_request(i2ctarget_i2c_target_obj
49
49
//| :param bool smbus: Use SMBUS timings if the hardware supports it"""
50
50
//| ...
51
51
static 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 );
53
53
enum { ARG_scl , ARG_sda , ARG_addresses , ARG_smbus };
54
54
static const mp_arg_t allowed_args [] = {
55
55
{ 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
85
85
//| """Releases control of the underlying hardware so other classes can use it."""
86
86
//| ...
87
87
static 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 ));
89
88
i2ctarget_i2c_target_obj_t * self = MP_OBJ_TO_PTR (self_in );
90
89
common_hal_i2ctarget_i2c_target_deinit (self );
91
90
return mp_const_none ;
92
91
}
93
92
MP_DEFINE_CONST_FUN_OBJ_1 (i2ctarget_i2c_target_deinit_obj , i2ctarget_i2c_target_obj_deinit );
94
93
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
+
95
100
//| def __enter__(self) -> I2CTarget:
96
101
//| """No-op used in Context Managers."""
97
102
//| ...
@@ -102,7 +107,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(i2ctarget_i2c_target_deinit_obj, i2ctarget_i2c_target_
102
107
//| :ref:`lifetime-and-contextmanagers` for more info."""
103
108
//| ...
104
109
static 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 ));
106
110
i2ctarget_i2c_target_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
107
111
common_hal_i2ctarget_i2c_target_deinit (self );
108
112
return mp_const_none ;
@@ -117,11 +121,9 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(i2ctarget_i2c_target___exit___obj, 4,
117
121
//| :rtype: ~i2ctarget.I2CTargetRequest"""
118
122
//|
119
123
static 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 ));
121
124
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
+
125
127
enum { ARG_timeout };
126
128
static const mp_arg_t allowed_args [] = {
127
129
{ 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
186
188
187
189
static const mp_rom_map_elem_t i2ctarget_i2c_target_locals_dict_table [] = {
188
190
{ 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 ) },
189
192
{ MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& default___enter___obj ) },
190
193
{ MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& i2ctarget_i2c_target___exit___obj ) },
191
194
{ 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,
227
230
//| """Close the request."""
228
231
//| ...
229
232
static 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 ));
231
233
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
234
+ check_for_deinit (self -> target );
235
+
232
236
common_hal_i2ctarget_i2c_target_close (self -> target );
233
237
return mp_const_none ;
234
238
}
@@ -237,26 +241,29 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(i2ctarget_i2c_target_request___exit__
237
241
//| address: int
238
242
//| """The I2C address of the request."""
239
243
static 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 ));
241
244
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
245
+ check_for_deinit (self -> target );
246
+
242
247
return mp_obj_new_int (self -> address );
243
248
}
244
249
MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_address_obj , i2ctarget_i2c_target_request_get_address );
245
250
246
251
//| is_read: bool
247
252
//| """The I2C main controller is reading from this target."""
248
253
static 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 ));
250
254
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
255
+ check_for_deinit (self -> target );
256
+
251
257
return mp_obj_new_bool (self -> is_read );
252
258
}
253
259
MP_DEFINE_CONST_PROP_GET (i2ctarget_i2c_target_request_is_read_obj , i2ctarget_i2c_target_request_get_is_read );
254
260
255
261
//| is_restart: bool
256
262
//| """Is Repeated Start Condition."""
257
263
static 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 ));
259
264
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
265
+ check_for_deinit (self -> target );
266
+
260
267
return mp_obj_new_bool (self -> is_restart );
261
268
}
262
269
MP_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_
270
277
//| :return: Bytes read"""
271
278
//| ...
272
279
static 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 ));
274
280
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
281
+ check_for_deinit (self -> target );
282
+
275
283
enum { ARG_n , ARG_ack };
276
284
static const mp_arg_t allowed_args [] = {
277
285
{ 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
327
335
//| :return: Number of bytes written"""
328
336
//| ...
329
337
static 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 ));
331
338
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
339
+ check_for_deinit (self -> target );
332
340
333
341
if (!self -> is_read ) {
334
342
mp_raise_OSError (MP_EACCES );
@@ -361,8 +369,9 @@ static MP_DEFINE_CONST_FUN_OBJ_2(i2ctarget_i2c_target_request_write_obj, i2ctarg
361
369
//| ...
362
370
//|
363
371
static 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 ));
365
372
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
373
+ check_for_deinit (self -> target );
374
+
366
375
bool ack = (n_args == 1 ) ? true : mp_obj_is_true (args [1 ]);
367
376
368
377
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
375
384
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (i2ctarget_i2c_target_request_ack_obj , 1 , 2 , i2ctarget_i2c_target_request_ack );
376
385
377
386
static 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 ));
379
387
i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
388
+ check_for_deinit (self -> target );
380
389
381
390
common_hal_i2ctarget_i2c_target_close (self -> target );
382
391
return mp_const_none ;
0 commit comments