Skip to content

Commit e063b06

Browse files
authored
Merge pull request #2756 from caternuson/bitmap_fill
Add fill method to displayio.Bitmap
2 parents 1f17bdb + a9fb34e commit e063b06

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,28 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
178178
return mp_const_none;
179179
}
180180

181+
//| .. method:: fill(value)
182+
//|
183+
//| Fills the bitmap with the supplied palette index value.
184+
//|
185+
STATIC mp_obj_t displayio_bitmap_obj_fill(mp_obj_t self_in, mp_obj_t value_obj) {
186+
displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in);
187+
188+
mp_int_t value = mp_obj_get_int(value_obj);
189+
if (value >= 1 << common_hal_displayio_bitmap_get_bits_per_value(self)) {
190+
mp_raise_ValueError(translate("pixel value requires too many bits"));
191+
}
192+
common_hal_displayio_bitmap_fill(self, value);
193+
194+
return mp_const_none;
195+
}
196+
MP_DEFINE_CONST_FUN_OBJ_2(displayio_bitmap_fill_obj, displayio_bitmap_obj_fill);
197+
181198
STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = {
182199
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) },
183200
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) },
201+
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&displayio_bitmap_fill_obj) },
202+
184203
};
185204
STATIC MP_DEFINE_CONST_DICT(displayio_bitmap_locals_dict, displayio_bitmap_locals_dict_table);
186205

shared-bindings/displayio/Bitmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self);
4141
uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self);
4242
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value);
4343
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
44+
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
4445

4546
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BITMAP_H

shared-module/displayio/Bitmap.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,24 @@ void displayio_bitmap_finish_refresh(displayio_bitmap_t *self) {
162162
self->dirty_area.x1 = 0;
163163
self->dirty_area.x2 = 0;
164164
}
165+
166+
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) {
167+
if (self->read_only) {
168+
mp_raise_RuntimeError(translate("Read-only object"));
169+
}
170+
// Update the dirty area.
171+
self->dirty_area.x1 = 0;
172+
self->dirty_area.x2 = self->width;
173+
self->dirty_area.y1 = 0;
174+
self->dirty_area.y2 = self->height;
175+
176+
// build the packed word
177+
uint32_t word = 0;
178+
for (uint8_t i=0; i<32 / self->bits_per_value; i++) {
179+
word |= (value & self->bitmask) << (32 - ((i+1)*self->bits_per_value));
180+
}
181+
// copy it in
182+
for (uint32_t i=0; i<self->stride * self->height; i++) {
183+
self->data[i] = word;
184+
}
185+
}

0 commit comments

Comments
 (0)