Commit 898d4c12 authored by Damien George's avatar Damien George
Browse files

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.
parent ad166857
...@@ -159,6 +159,15 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size ...@@ -159,6 +159,15 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
return MP_OBJ_FROM_PTR(o); return MP_OBJ_FROM_PTR(o);
} }
STATIC mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
(void)flags;
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
bufinfo->buf = self->buf;
bufinfo->len = self->stride * self->height * (self->format == FRAMEBUF_RGB565 ? 2 : 1);
bufinfo->typecode = 'B'; // view framebuf as bytes
return 0;
}
STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) { STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) {
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t col = mp_obj_get_int(col_in); mp_int_t col = mp_obj_get_int(col_in);
...@@ -441,6 +450,7 @@ STATIC const mp_obj_type_t mp_type_framebuf = { ...@@ -441,6 +450,7 @@ STATIC const mp_obj_type_t mp_type_framebuf = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_FrameBuffer, .name = MP_QSTR_FrameBuffer,
.make_new = framebuf_make_new, .make_new = framebuf_make_new,
.buffer_p = { .get_buffer = framebuf_get_buffer },
.locals_dict = (mp_obj_t)&framebuf_locals_dict, .locals_dict = (mp_obj_t)&framebuf_locals_dict,
}; };
......
...@@ -10,6 +10,9 @@ h = 16 ...@@ -10,6 +10,9 @@ h = 16
buf = bytearray(w * h // 8) buf = bytearray(w * h // 8)
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.MVLSB) fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.MVLSB)
# access as buffer
print(memoryview(fbuf)[0])
# fill # fill
fbuf.fill(1) fbuf.fill(1)
print(buf) print(buf)
......
0
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff') bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray(b'\x01\x00\x00\x00\x01\x80\x00\x00\x00\x80') bytearray(b'\x01\x00\x00\x00\x01\x80\x00\x00\x00\x80')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment