Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,10 @@ msgstr ""
msgid "Name too long"
msgstr ""

#: shared-bindings/displayio/TileGrid.c
msgid "New bitmap must be same size as old bitmap"
msgstr ""

#: ports/espressif/common-hal/_bleio/__init__.c
msgid "Nimble out of memory"
msgstr ""
Expand Down
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/bdmicro_vina_d21/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "MX25L51245G","GD25S512MD"
LONGINT_IMPL = MPZ

CIRCUITPY_ONEWIREIO = 0
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "S25FL216K, GD25Q16C"
LONGINT_IMPL = MPZ

CIRCUITPY_ONEWIREIO = 0
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "W25Q32FV"
LONGINT_IMPL = MPZ

CIRCUITPY_ONEWIREIO = 0
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/stackrduino_m0_pro/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "W25Q64JVxQ"
LONGINT_IMPL = MPZ

CIRCUITPY_ONEWIREIO = 0
70 changes: 69 additions & 1 deletion shared-bindings/displayio/TileGrid.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ static displayio_tilegrid_t *native_tilegrid(mp_obj_t tilegrid_obj) {
mp_obj_assert_native_inited(native_tilegrid);
return MP_OBJ_TO_PTR(native_tilegrid);
}

//| hidden: bool
//| """True when the TileGrid is hidden. This may be False even when a part of a hidden Group."""
//|
Expand Down Expand Up @@ -379,6 +380,72 @@ const mp_obj_property_t displayio_tilegrid_pixel_shader_obj = {
MP_ROM_NONE},
};

//| bitmap: Union[Bitmap,OnDiskBitmap,Shape]
//| """The bitmap of the tilegrid."""
//|
STATIC mp_obj_t displayio_tilegrid_obj_get_bitmap(mp_obj_t self_in) {
displayio_tilegrid_t *self = native_tilegrid(self_in);
return common_hal_displayio_tilegrid_get_bitmap(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_tilegrid_get_bitmap_obj, displayio_tilegrid_obj_get_bitmap);

STATIC mp_obj_t displayio_tilegrid_obj_set_bitmap(mp_obj_t self_in, mp_obj_t bitmap) {
displayio_tilegrid_t *self = native_tilegrid(self_in);

uint16_t new_bitmap_width;
uint16_t new_bitmap_height;
mp_obj_t native = mp_obj_cast_to_native_base(bitmap, &displayio_shape_type);
if (native != MP_OBJ_NULL) {
displayio_shape_t *bmp = MP_OBJ_TO_PTR(native);
new_bitmap_width = bmp->width;
new_bitmap_height = bmp->height;
} else if (mp_obj_is_type(bitmap, &displayio_bitmap_type)) {
displayio_bitmap_t *bmp = MP_OBJ_TO_PTR(bitmap);
native = bitmap;
new_bitmap_width = bmp->width;
new_bitmap_height = bmp->height;
} else if (mp_obj_is_type(bitmap, &displayio_ondiskbitmap_type)) {
displayio_ondiskbitmap_t *bmp = MP_OBJ_TO_PTR(bitmap);
native = bitmap;
new_bitmap_width = bmp->width;
new_bitmap_height = bmp->height;
} else {
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_bitmap);
}

mp_obj_t old_native = mp_obj_cast_to_native_base(self->bitmap, &displayio_shape_type);
if (old_native != MP_OBJ_NULL) {
displayio_shape_t *old_bmp = MP_OBJ_TO_PTR(old_native);
if (old_bmp->width != new_bitmap_width || old_bmp->height != new_bitmap_height) {
mp_raise_ValueError(translate("New bitmap must be same size as old bitmap"));
}
} else if (mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) {
displayio_bitmap_t *old_bmp = MP_OBJ_TO_PTR(self->bitmap);
old_native = self->bitmap;
if (old_bmp->width != new_bitmap_width || old_bmp->height != new_bitmap_height) {
mp_raise_ValueError(translate("New bitmap must be same size as old bitmap"));
}
} else if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) {
displayio_ondiskbitmap_t *old_bmp = MP_OBJ_TO_PTR(self->bitmap);
old_native = self->bitmap;
if (old_bmp->width != new_bitmap_width || old_bmp->height != new_bitmap_height) {
mp_raise_ValueError(translate("New bitmap must be same size as old bitmap"));
}
}

common_hal_displayio_tilegrid_set_bitmap(self, bitmap);

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_tilegrid_set_bitmap_obj, displayio_tilegrid_obj_set_bitmap);

const mp_obj_property_t displayio_tilegrid_bitmap_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_tilegrid_get_bitmap_obj,
(mp_obj_t)&displayio_tilegrid_set_bitmap_obj,
MP_ROM_NONE},
};

//| def __getitem__(self, index: Union[Tuple[int, int], int]) -> int:
//| """Returns the tile index at the given index. The index can either be an x,y tuple or an int equal
//| to ``y * width + x``.
Expand Down Expand Up @@ -454,7 +521,8 @@ STATIC const mp_rom_map_elem_t displayio_tilegrid_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_flip_x), MP_ROM_PTR(&displayio_tilegrid_flip_x_obj) },
{ MP_ROM_QSTR(MP_QSTR_flip_y), MP_ROM_PTR(&displayio_tilegrid_flip_y_obj) },
{ MP_ROM_QSTR(MP_QSTR_transpose_xy), MP_ROM_PTR(&displayio_tilegrid_transpose_xy_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_tilegrid_pixel_shader_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_tilegrid_pixel_shader_obj) },
{ MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_tilegrid_bitmap_obj) },
};
STATIC MP_DEFINE_CONST_DICT(displayio_tilegrid_locals_dict, displayio_tilegrid_locals_dict_table);

Expand Down
3 changes: 3 additions & 0 deletions shared-bindings/displayio/TileGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void common_hal_displayio_tilegrid_set_y(displayio_tilegrid_t *self, mp_int_t y)
mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *self);
void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self, mp_obj_t pixel_shader);

mp_obj_t common_hal_displayio_tilegrid_get_bitmap(displayio_tilegrid_t *self);
void common_hal_displayio_tilegrid_set_bitmap(displayio_tilegrid_t *self, mp_obj_t bitmap);


bool common_hal_displayio_tilegrid_get_flip_x(displayio_tilegrid_t *self);
void common_hal_displayio_tilegrid_set_flip_x(displayio_tilegrid_t *self, bool flip_x);
Expand Down
9 changes: 9 additions & 0 deletions shared-module/displayio/TileGrid.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self,
self->full_change = true;
}

mp_obj_t common_hal_displayio_tilegrid_get_bitmap(displayio_tilegrid_t *self) {
return self->bitmap;
}

void common_hal_displayio_tilegrid_set_bitmap(displayio_tilegrid_t *self, mp_obj_t bitmap) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs additional checking, similar to what is done now during the constructor in shared-bindings.

    if (bitmap_width % tile_width != 0) {
        mp_raise_ValueError(translate("Tile width must exactly divide bitmap width"));
    }
    if (bitmap_height % tile_height != 0) {
        mp_raise_ValueError(translate("Tile height must exactly divide bitmap height"));
    }

in fact, the old and new bitmaps may need to be exactly the same resolution?

As for dealing with the type of the thing, consider factoring out this block from the TileGrid constructor in shared-bindings, and re-using its message as-is:

    mp_obj_t native = mp_obj_cast_to_native_base(bitmap, &displayio_shape_type);
    if (native != MP_OBJ_NULL) {
        displayio_shape_t *bmp = MP_OBJ_TO_PTR(native);
        bitmap_width = bmp->width;
        bitmap_height = bmp->height;
    } else if (mp_obj_is_type(bitmap, &displayio_bitmap_type)) {
        displayio_bitmap_t *bmp = MP_OBJ_TO_PTR(bitmap);
        native = bitmap;
        bitmap_width = bmp->width;
        bitmap_height = bmp->height;
    } else if (mp_obj_is_type(bitmap, &displayio_ondiskbitmap_type)) {
        displayio_ondiskbitmap_t *bmp = MP_OBJ_TO_PTR(bitmap);
        native = bitmap;
        bitmap_width = bmp->width;
        bitmap_height = bmp->height;
    } else {
        mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_bitmap);
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  if (bitmap_width % tile_width != 0) {
        mp_raise_ValueError(translate("Tile width must exactly divide bitmap width"));
    }
    if (bitmap_height % tile_height != 0) {
        mp_raise_ValueError(translate("Tile height must exactly divide bitmap height"));
    }

Error messages add bytes, so . Maybe "Tile aspect ratio must be integral" for both? If they have to be equal, then that's shorter also.

Copy link
Collaborator Author

@FoamyGuy FoamyGuy Apr 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had some success factoring this into a new function and using it from the bitmap setter. But there is still work to do I think.

Right now this code is repeated in the constructor, ideally we could use the new function from the constructor to avoid the repetition. But I'm not sure how to do it that way yet, it needs to return some of the values so they can be used afterward inside the constructor. I'll keep working on it later.

self->bitmap = bitmap;
self->full_change = true;
}

uint16_t common_hal_displayio_tilegrid_get_width(displayio_tilegrid_t *self) {
return self->width_in_tiles;
}
Expand Down