28
28
#include <string.h>
29
29
30
30
#include "py/runtime.h"
31
+ #include "py/objtype.h"
31
32
#include "py/proto.h"
32
33
33
34
#if MICROPY_PY_FRAMEBUF
@@ -304,17 +305,26 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, cons
304
305
return MP_OBJ_FROM_PTR (o );
305
306
}
306
307
308
+ STATIC const mp_obj_type_t mp_type_framebuf ;
309
+
310
+ // Helper to ensure we have the native super class instead of a subclass.
311
+ static mp_obj_framebuf_t * native_framebuf (mp_obj_t framebuf_obj ) {
312
+ mp_obj_t native_framebuf = mp_instance_cast_to_native_base (framebuf_obj , & mp_type_framebuf );
313
+ mp_obj_assert_native_inited (native_framebuf );
314
+ return MP_OBJ_TO_PTR (native_framebuf );
315
+ }
316
+
307
317
STATIC mp_int_t framebuf_get_buffer (mp_obj_t self_in , mp_buffer_info_t * bufinfo , mp_uint_t flags ) {
308
318
(void )flags ;
309
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (self_in );
319
+ mp_obj_framebuf_t * self = native_framebuf (self_in );
310
320
bufinfo -> buf = self -> buf ;
311
321
bufinfo -> len = self -> stride * self -> height * (self -> format == FRAMEBUF_RGB565 ? 2 : 1 );
312
322
bufinfo -> typecode = 'B' ; // view framebuf as bytes
313
323
return 0 ;
314
324
}
315
325
316
326
STATIC mp_obj_t framebuf_fill (mp_obj_t self_in , mp_obj_t col_in ) {
317
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (self_in );
327
+ mp_obj_framebuf_t * self = native_framebuf (self_in );
318
328
mp_int_t col = mp_obj_get_int (col_in );
319
329
formats [self -> format ].fill_rect (self , 0 , 0 , self -> width , self -> height , col );
320
330
return mp_const_none ;
@@ -324,7 +334,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(framebuf_fill_obj, framebuf_fill);
324
334
STATIC mp_obj_t framebuf_fill_rect (size_t n_args , const mp_obj_t * args ) {
325
335
(void )n_args ;
326
336
327
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
337
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
328
338
mp_int_t x = mp_obj_get_int (args [1 ]);
329
339
mp_int_t y = mp_obj_get_int (args [2 ]);
330
340
mp_int_t width = mp_obj_get_int (args [3 ]);
@@ -338,7 +348,7 @@ STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args) {
338
348
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (framebuf_fill_rect_obj , 6 , 6 , framebuf_fill_rect );
339
349
340
350
STATIC mp_obj_t framebuf_pixel (size_t n_args , const mp_obj_t * args ) {
341
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
351
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
342
352
mp_int_t x = mp_obj_get_int (args [1 ]);
343
353
mp_int_t y = mp_obj_get_int (args [2 ]);
344
354
if (0 <= x && x < self -> width && 0 <= y && y < self -> height ) {
@@ -357,7 +367,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_pixel_obj, 3, 4, framebuf_pi
357
367
STATIC mp_obj_t framebuf_hline (size_t n_args , const mp_obj_t * args ) {
358
368
(void )n_args ;
359
369
360
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
370
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
361
371
mp_int_t x = mp_obj_get_int (args [1 ]);
362
372
mp_int_t y = mp_obj_get_int (args [2 ]);
363
373
mp_int_t w = mp_obj_get_int (args [3 ]);
@@ -372,7 +382,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_hline_obj, 5, 5, framebuf_hl
372
382
STATIC mp_obj_t framebuf_vline (size_t n_args , const mp_obj_t * args ) {
373
383
(void )n_args ;
374
384
375
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
385
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
376
386
mp_int_t x = mp_obj_get_int (args [1 ]);
377
387
mp_int_t y = mp_obj_get_int (args [2 ]);
378
388
mp_int_t h = mp_obj_get_int (args [3 ]);
@@ -387,7 +397,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_vline_obj, 5, 5, framebuf_vl
387
397
STATIC mp_obj_t framebuf_rect (size_t n_args , const mp_obj_t * args ) {
388
398
(void )n_args ;
389
399
390
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
400
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
391
401
mp_int_t x = mp_obj_get_int (args [1 ]);
392
402
mp_int_t y = mp_obj_get_int (args [2 ]);
393
403
mp_int_t w = mp_obj_get_int (args [3 ]);
@@ -406,7 +416,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 6, framebuf_rec
406
416
STATIC mp_obj_t framebuf_line (size_t n_args , const mp_obj_t * args ) {
407
417
(void )n_args ;
408
418
409
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
419
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
410
420
mp_int_t x1 = mp_obj_get_int (args [1 ]);
411
421
mp_int_t y1 = mp_obj_get_int (args [2 ]);
412
422
mp_int_t x2 = mp_obj_get_int (args [3 ]);
@@ -470,8 +480,8 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
470
480
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (framebuf_line_obj , 6 , 6 , framebuf_line );
471
481
472
482
STATIC mp_obj_t framebuf_blit (size_t n_args , const mp_obj_t * args ) {
473
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
474
- mp_obj_framebuf_t * source = MP_OBJ_TO_PTR (args [1 ]);
483
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
484
+ mp_obj_framebuf_t * source = native_framebuf (args [1 ]);
475
485
mp_int_t x = mp_obj_get_int (args [2 ]);
476
486
mp_int_t y = mp_obj_get_int (args [3 ]);
477
487
mp_int_t key = -1 ;
@@ -513,7 +523,7 @@ STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
513
523
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (framebuf_blit_obj , 4 , 5 , framebuf_blit );
514
524
515
525
STATIC mp_obj_t framebuf_scroll (mp_obj_t self_in , mp_obj_t xstep_in , mp_obj_t ystep_in ) {
516
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (self_in );
526
+ mp_obj_framebuf_t * self = native_framebuf (self_in );
517
527
mp_int_t xstep = mp_obj_get_int (xstep_in );
518
528
mp_int_t ystep = mp_obj_get_int (ystep_in );
519
529
int sx , y , xend , yend , dx , dy ;
@@ -546,7 +556,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(framebuf_scroll_obj, framebuf_scroll);
546
556
547
557
STATIC mp_obj_t framebuf_text (size_t n_args , const mp_obj_t * args ) {
548
558
// extract arguments
549
- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
559
+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
550
560
const char * str = mp_obj_str_get_str (args [1 ]);
551
561
mp_int_t x0 = mp_obj_get_int (args [2 ]);
552
562
mp_int_t y0 = mp_obj_get_int (args [3 ]);
0 commit comments