Skip to content

Commit 6188301

Browse files
authored
Merge pull request #3941 from AdamCummick/add_uart_subclassing
Add UART subclassing
2 parents 1a4061c + 916bd92 commit 6188301

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

shared-bindings/busio/UART.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include "py/ioctl.h"
3737
#include "py/objproperty.h"
38+
#include "py/objtype.h"
3839
#include "py/runtime.h"
3940
#include "py/stream.h"
4041
#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
142143
return (mp_obj_t)self;
143144
}
144145

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+
145158
//| def deinit(self) -> None:
146159
//| """Deinitialises the UART and releases any hardware resources for reuse."""
147160
//| ...
148161
//|
149162
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);
151164
common_hal_busio_uart_deinit(self);
152165
return mp_const_none;
153166
}
@@ -223,7 +236,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_
223236
// These three methods are used by the shared stream methods.
224237
STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
225238
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);
227240
check_for_deinit(self);
228241
byte *buf = buf_in;
229242

@@ -236,15 +249,15 @@ STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size,
236249
}
237250

238251
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);
240253
check_for_deinit(self);
241254
const byte *buf = buf_in;
242255

243256
return common_hal_busio_uart_write(self, buf, size, errcode);
244257
}
245258

246259
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);
248261
check_for_deinit(self);
249262
mp_uint_t ret;
250263
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
267280
//| """The current baudrate."""
268281
//|
269282
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);
271284
check_for_deinit(self);
272285
return MP_OBJ_NEW_SMALL_INT(common_hal_busio_uart_get_baudrate(self));
273286
}
274287
MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_get_baudrate_obj, busio_uart_obj_get_baudrate);
275288

276289
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);
278291
check_for_deinit(self);
279292
common_hal_busio_uart_set_baudrate(self, mp_obj_get_int(baudrate));
280293
return mp_const_none;
@@ -293,7 +306,7 @@ const mp_obj_property_t busio_uart_baudrate_obj = {
293306
//| """The number of bytes in the input buffer, available to be read"""
294307
//|
295308
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);
297310
check_for_deinit(self);
298311
return MP_OBJ_NEW_SMALL_INT(common_hal_busio_uart_rx_characters_available(self));
299312
}
@@ -310,14 +323,14 @@ const mp_obj_property_t busio_uart_in_waiting_obj = {
310323
//| """The current timeout, in seconds (float)."""
311324
//|
312325
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);
314327
check_for_deinit(self);
315328
return mp_obj_new_float(common_hal_busio_uart_get_timeout(self));
316329
}
317330
MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_get_timeout_obj, busio_uart_obj_get_timeout);
318331

319332
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);
321334
check_for_deinit(self);
322335
mp_float_t timeout_float = mp_obj_get_float(timeout);
323336
validate_timeout(timeout_float);
@@ -339,7 +352,7 @@ const mp_obj_property_t busio_uart_timeout_obj = {
339352
//| ...
340353
//|
341354
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);
343356
check_for_deinit(self);
344357
common_hal_busio_uart_clear_rx_buffer(self);
345358
return mp_const_none;

0 commit comments

Comments
 (0)