2828#include <string.h>
2929
3030#include "py/runtime.h"
31+ #include "py/objtype.h"
3132#include "py/proto.h"
3233
3334#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
304305 return MP_OBJ_FROM_PTR (o );
305306}
306307
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+
307317STATIC mp_int_t framebuf_get_buffer (mp_obj_t self_in , mp_buffer_info_t * bufinfo , mp_uint_t flags ) {
308318 (void )flags ;
309- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (self_in );
319+ mp_obj_framebuf_t * self = native_framebuf (self_in );
310320 bufinfo -> buf = self -> buf ;
311321 bufinfo -> len = self -> stride * self -> height * (self -> format == FRAMEBUF_RGB565 ? 2 : 1 );
312322 bufinfo -> typecode = 'B' ; // view framebuf as bytes
313323 return 0 ;
314324}
315325
316326STATIC 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 );
318328 mp_int_t col = mp_obj_get_int (col_in );
319329 formats [self -> format ].fill_rect (self , 0 , 0 , self -> width , self -> height , col );
320330 return mp_const_none ;
@@ -324,7 +334,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(framebuf_fill_obj, framebuf_fill);
324334STATIC mp_obj_t framebuf_fill_rect (size_t n_args , const mp_obj_t * args ) {
325335 (void )n_args ;
326336
327- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
337+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
328338 mp_int_t x = mp_obj_get_int (args [1 ]);
329339 mp_int_t y = mp_obj_get_int (args [2 ]);
330340 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) {
338348STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (framebuf_fill_rect_obj , 6 , 6 , framebuf_fill_rect );
339349
340350STATIC 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 ]);
342352 mp_int_t x = mp_obj_get_int (args [1 ]);
343353 mp_int_t y = mp_obj_get_int (args [2 ]);
344354 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
357367STATIC mp_obj_t framebuf_hline (size_t n_args , const mp_obj_t * args ) {
358368 (void )n_args ;
359369
360- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
370+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
361371 mp_int_t x = mp_obj_get_int (args [1 ]);
362372 mp_int_t y = mp_obj_get_int (args [2 ]);
363373 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
372382STATIC mp_obj_t framebuf_vline (size_t n_args , const mp_obj_t * args ) {
373383 (void )n_args ;
374384
375- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
385+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
376386 mp_int_t x = mp_obj_get_int (args [1 ]);
377387 mp_int_t y = mp_obj_get_int (args [2 ]);
378388 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
387397STATIC mp_obj_t framebuf_rect (size_t n_args , const mp_obj_t * args ) {
388398 (void )n_args ;
389399
390- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
400+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
391401 mp_int_t x = mp_obj_get_int (args [1 ]);
392402 mp_int_t y = mp_obj_get_int (args [2 ]);
393403 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
406416STATIC mp_obj_t framebuf_line (size_t n_args , const mp_obj_t * args ) {
407417 (void )n_args ;
408418
409- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
419+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
410420 mp_int_t x1 = mp_obj_get_int (args [1 ]);
411421 mp_int_t y1 = mp_obj_get_int (args [2 ]);
412422 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) {
470480STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (framebuf_line_obj , 6 , 6 , framebuf_line );
471481
472482STATIC 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 ]);
475485 mp_int_t x = mp_obj_get_int (args [2 ]);
476486 mp_int_t y = mp_obj_get_int (args [3 ]);
477487 mp_int_t key = -1 ;
@@ -513,7 +523,7 @@ STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
513523STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (framebuf_blit_obj , 4 , 5 , framebuf_blit );
514524
515525STATIC 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 );
517527 mp_int_t xstep = mp_obj_get_int (xstep_in );
518528 mp_int_t ystep = mp_obj_get_int (ystep_in );
519529 int sx , y , xend , yend , dx , dy ;
@@ -546,7 +556,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(framebuf_scroll_obj, framebuf_scroll);
546556
547557STATIC mp_obj_t framebuf_text (size_t n_args , const mp_obj_t * args ) {
548558 // extract arguments
549- mp_obj_framebuf_t * self = MP_OBJ_TO_PTR (args [0 ]);
559+ mp_obj_framebuf_t * self = native_framebuf (args [0 ]);
550560 const char * str = mp_obj_str_get_str (args [1 ]);
551561 mp_int_t x0 = mp_obj_get_int (args [2 ]);
552562 mp_int_t y0 = mp_obj_get_int (args [3 ]);
0 commit comments