Skip to content

Commit 49fff2d

Browse files
committed
initial working fill
1 parent b3b8b5c commit 49fff2d

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-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()
182+
//|
183+
//| Fills the bitmap.
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,35 @@ 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+
168+
for (uint32_t x=0; x<self->width; x++) {
169+
for (uint32_t y=0; y<self->height; y++) {
170+
int32_t row_start = y * self->stride;
171+
uint32_t bytes_per_value = self->bits_per_value / 8;
172+
if (bytes_per_value < 1) {
173+
uint32_t bit_position = (sizeof(size_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value);
174+
uint32_t index = row_start + (x >> self->x_shift);
175+
uint32_t word = self->data[index];
176+
word &= ~(self->bitmask << bit_position);
177+
word |= (value & self->bitmask) << bit_position;
178+
self->data[index] = word;
179+
} else {
180+
size_t* row = self->data + row_start;
181+
if (bytes_per_value == 1) {
182+
((uint8_t*) row)[x] = value;
183+
} else if (bytes_per_value == 2) {
184+
((uint16_t*) row)[x] = value;
185+
} else if (bytes_per_value == 4) {
186+
((uint32_t*) row)[x] = value;
187+
}
188+
}
189+
}
190+
}
191+
192+
self->dirty_area.x1 = 0;
193+
self->dirty_area.x2 = self->width;
194+
self->dirty_area.y1 = 0;
195+
self->dirty_area.y2 = self->height;
196+
}

0 commit comments

Comments
 (0)