Skip to content

Commit 898d4c1

Browse files
committed
extmod/modframebuf: Make framebuf implement the buffer protocol.
So that one can easily access the underlying data of the frame buffer, eg to write the data out to a display.
1 parent ad16685 commit 898d4c1

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

extmod/modframebuf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
159159
return MP_OBJ_FROM_PTR(o);
160160
}
161161

162+
STATIC mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
163+
(void)flags;
164+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
165+
bufinfo->buf = self->buf;
166+
bufinfo->len = self->stride * self->height * (self->format == FRAMEBUF_RGB565 ? 2 : 1);
167+
bufinfo->typecode = 'B'; // view framebuf as bytes
168+
return 0;
169+
}
170+
162171
STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) {
163172
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
164173
mp_int_t col = mp_obj_get_int(col_in);
@@ -441,6 +450,7 @@ STATIC const mp_obj_type_t mp_type_framebuf = {
441450
{ &mp_type_type },
442451
.name = MP_QSTR_FrameBuffer,
443452
.make_new = framebuf_make_new,
453+
.buffer_p = { .get_buffer = framebuf_get_buffer },
444454
.locals_dict = (mp_obj_t)&framebuf_locals_dict,
445455
};
446456

tests/extmod/framebuf1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
buf = bytearray(w * h // 8)
1111
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.MVLSB)
1212

13+
# access as buffer
14+
print(memoryview(fbuf)[0])
15+
1316
# fill
1417
fbuf.fill(1)
1518
print(buf)

tests/extmod/framebuf1.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
0
12
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')
23
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
34
bytearray(b'\x01\x00\x00\x00\x01\x80\x00\x00\x00\x80')

0 commit comments

Comments
 (0)