Skip to content

Commit f8449dd

Browse files
jimmodpgeorge
authored andcommitted
extmod/modframebuf: Allow blit source to be a subclass of FrameBuffer.
1 parent f6d99bc commit f8449dd

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

extmod/modframebuf.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ typedef struct _mp_obj_framebuf_t {
4141
uint8_t format;
4242
} mp_obj_framebuf_t;
4343

44+
#if !MICROPY_ENABLE_DYNRUNTIME
45+
STATIC const mp_obj_type_t mp_type_framebuf;
46+
#endif
47+
4448
typedef void (*setpixel_t)(const mp_obj_framebuf_t*, int, int, uint32_t);
4549
typedef uint32_t (*getpixel_t)(const mp_obj_framebuf_t*, int, int);
4650
typedef void (*fill_rect_t)(const mp_obj_framebuf_t *, int, int, int, int, uint32_t);
@@ -469,7 +473,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_lin
469473

470474
STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
471475
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
472-
mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(args[1]);
476+
mp_obj_t source_in = mp_obj_cast_to_native_base(args[1], MP_OBJ_FROM_PTR(&mp_type_framebuf));
477+
if (source_in == MP_OBJ_NULL) {
478+
mp_raise_TypeError(NULL);
479+
}
480+
mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(source_in);
481+
473482
mp_int_t x = mp_obj_get_int(args[2]);
474483
mp_int_t y = mp_obj_get_int(args[3]);
475484
mp_int_t key = -1;

tests/extmod/framebuf_subclass.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,25 @@ def foo(self):
1818
fb.pixel(0, 0, 0x0102)
1919
fb.foo()
2020
print(bytes(fb))
21+
22+
# Test that blitting a subclass works.
23+
fb2 = framebuf.FrameBuffer(bytearray(2 * 3 * 3), 3, 3, framebuf.RGB565)
24+
fb.fill(0)
25+
fb.pixel(0, 0, 0x0506)
26+
fb.pixel(2, 2, 0x0708)
27+
fb2.blit(fb, 0, 0)
28+
print(bytes(fb2))
29+
30+
# Test that blitting something that isn't a subclass fails with TypeError.
31+
class NotAFrameBuf:
32+
pass
33+
34+
try:
35+
fb.blit(NotAFrameBuf(), 0, 0)
36+
except TypeError:
37+
print('TypeError')
38+
39+
try:
40+
fb.blit(None, 0, 0)
41+
except TypeError:
42+
print('TypeError')

tests/extmod/framebuf_subclass.py.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
b'\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x03\x04\x03\x04\x03'
2+
b'\x06\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x07'
3+
TypeError
4+
TypeError

0 commit comments

Comments
 (0)