Skip to content

Commit c0d4b37

Browse files
committed
displayio.Bitmap: Make memoryview()able
1 parent bfc8c89 commit c0d4b37

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,17 @@ STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = {
300300
};
301301
STATIC MP_DEFINE_CONST_DICT(displayio_bitmap_locals_dict, displayio_bitmap_locals_dict_table);
302302

303+
// (the get_buffer protocol returns 0 for success, 1 for failure)
304+
STATIC mp_int_t bitmap_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
305+
displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in);
306+
return common_hal_displayio_bitmap_get_buffer(self, bufinfo, flags);
307+
}
308+
303309
const mp_obj_type_t displayio_bitmap_type = {
304310
{ &mp_type_type },
305311
.name = MP_QSTR_Bitmap,
306312
.make_new = displayio_bitmap_make_new,
307313
.subscr = bitmap_subscr,
308314
.locals_dict = (mp_obj_dict_t *)&displayio_bitmap_locals_dict,
315+
.buffer_p = { .get_buffer = bitmap_get_buffer },
309316
};

shared-bindings/displayio/Bitmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16
4545
uint32_t skip_index, bool skip_index_none);
4646
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
4747
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
48+
int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags);
4849

4950
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BITMAP_H

shared-module/displayio/Bitmap.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,23 @@ void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value)
271271
self->data[i] = word;
272272
}
273273
}
274+
275+
int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
276+
if (flags & MP_BUFFER_WRITE) {
277+
return 1;
278+
}
279+
bufinfo->len = self->stride * self->height * sizeof(size_t);
280+
bufinfo->buf = self->data;
281+
switch (self->bits_per_value) {
282+
case 32:
283+
bufinfo->typecode = 'I';
284+
break;
285+
case 16:
286+
bufinfo->typecode = 'H';
287+
break;
288+
default:
289+
bufinfo->typecode = 'B';
290+
break;
291+
}
292+
return 0;
293+
}

0 commit comments

Comments
 (0)