35
35
36
36
#include "py/ioctl.h"
37
37
#include "py/objproperty.h"
38
+ #include "py/objtype.h"
38
39
#include "py/runtime.h"
39
40
#include "py/stream.h"
40
41
#include "supervisor/shared/translate.h"
@@ -142,12 +143,24 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co
142
143
return (mp_obj_t )self ;
143
144
}
144
145
146
+
147
+ // Helper to ensure we have the native super class instead of a subclass.
148
+ busio_uart_obj_t * native_uart (mp_obj_t uart_obj ) {
149
+ mp_obj_t native_uart = mp_obj_cast_to_native_base (uart_obj , & busio_uart_type );
150
+ if (native_uart == MP_OBJ_NULL ) {
151
+ mp_raise_ValueError_varg (translate ("Must be a %q subclass." ), MP_QSTR_UART );
152
+ }
153
+ mp_obj_assert_native_inited (native_uart );
154
+ return MP_OBJ_TO_PTR (native_uart );
155
+ }
156
+
157
+
145
158
//| def deinit(self) -> None:
146
159
//| """Deinitialises the UART and releases any hardware resources for reuse."""
147
160
//| ...
148
161
//|
149
162
STATIC mp_obj_t busio_uart_obj_deinit (mp_obj_t self_in ) {
150
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
163
+ busio_uart_obj_t * self = native_uart (self_in );
151
164
common_hal_busio_uart_deinit (self );
152
165
return mp_const_none ;
153
166
}
@@ -223,7 +236,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_
223
236
// These three methods are used by the shared stream methods.
224
237
STATIC mp_uint_t busio_uart_read (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
225
238
STREAM_DEBUG ("busio_uart_read stream %d\n" , size );
226
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
239
+ busio_uart_obj_t * self = native_uart (self_in );
227
240
check_for_deinit (self );
228
241
byte * buf = buf_in ;
229
242
@@ -236,15 +249,15 @@ STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size,
236
249
}
237
250
238
251
STATIC mp_uint_t busio_uart_write (mp_obj_t self_in , const void * buf_in , mp_uint_t size , int * errcode ) {
239
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
252
+ busio_uart_obj_t * self = native_uart (self_in );
240
253
check_for_deinit (self );
241
254
const byte * buf = buf_in ;
242
255
243
256
return common_hal_busio_uart_write (self , buf , size , errcode );
244
257
}
245
258
246
259
STATIC mp_uint_t busio_uart_ioctl (mp_obj_t self_in , mp_uint_t request , mp_uint_t arg , int * errcode ) {
247
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
260
+ busio_uart_obj_t * self = native_uart (self_in );
248
261
check_for_deinit (self );
249
262
mp_uint_t ret ;
250
263
if (request == MP_IOCTL_POLL ) {
@@ -267,14 +280,14 @@ STATIC mp_uint_t busio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t
267
280
//| """The current baudrate."""
268
281
//|
269
282
STATIC mp_obj_t busio_uart_obj_get_baudrate (mp_obj_t self_in ) {
270
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
283
+ busio_uart_obj_t * self = native_uart (self_in );
271
284
check_for_deinit (self );
272
285
return MP_OBJ_NEW_SMALL_INT (common_hal_busio_uart_get_baudrate (self ));
273
286
}
274
287
MP_DEFINE_CONST_FUN_OBJ_1 (busio_uart_get_baudrate_obj , busio_uart_obj_get_baudrate );
275
288
276
289
STATIC mp_obj_t busio_uart_obj_set_baudrate (mp_obj_t self_in , mp_obj_t baudrate ) {
277
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
290
+ busio_uart_obj_t * self = native_uart (self_in );
278
291
check_for_deinit (self );
279
292
common_hal_busio_uart_set_baudrate (self , mp_obj_get_int (baudrate ));
280
293
return mp_const_none ;
@@ -293,7 +306,7 @@ const mp_obj_property_t busio_uart_baudrate_obj = {
293
306
//| """The number of bytes in the input buffer, available to be read"""
294
307
//|
295
308
STATIC mp_obj_t busio_uart_obj_get_in_waiting (mp_obj_t self_in ) {
296
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
309
+ busio_uart_obj_t * self = native_uart (self_in );
297
310
check_for_deinit (self );
298
311
return MP_OBJ_NEW_SMALL_INT (common_hal_busio_uart_rx_characters_available (self ));
299
312
}
@@ -310,14 +323,14 @@ const mp_obj_property_t busio_uart_in_waiting_obj = {
310
323
//| """The current timeout, in seconds (float)."""
311
324
//|
312
325
STATIC mp_obj_t busio_uart_obj_get_timeout (mp_obj_t self_in ) {
313
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
326
+ busio_uart_obj_t * self = native_uart (self_in );
314
327
check_for_deinit (self );
315
328
return mp_obj_new_float (common_hal_busio_uart_get_timeout (self ));
316
329
}
317
330
MP_DEFINE_CONST_FUN_OBJ_1 (busio_uart_get_timeout_obj , busio_uart_obj_get_timeout );
318
331
319
332
STATIC mp_obj_t busio_uart_obj_set_timeout (mp_obj_t self_in , mp_obj_t timeout ) {
320
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
333
+ busio_uart_obj_t * self = native_uart (self_in );
321
334
check_for_deinit (self );
322
335
mp_float_t timeout_float = mp_obj_get_float (timeout );
323
336
validate_timeout (timeout_float );
@@ -339,7 +352,7 @@ const mp_obj_property_t busio_uart_timeout_obj = {
339
352
//| ...
340
353
//|
341
354
STATIC mp_obj_t busio_uart_obj_reset_input_buffer (mp_obj_t self_in ) {
342
- busio_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
355
+ busio_uart_obj_t * self = native_uart (self_in );
343
356
check_for_deinit (self );
344
357
common_hal_busio_uart_clear_rx_buffer (self );
345
358
return mp_const_none ;
0 commit comments