diff --git a/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk b/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk index 46aa0b7dc659f..b3cb3d73015db 100755 --- a/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitbrains_deluxe_m4/mpconfigboard.mk @@ -14,3 +14,4 @@ CIRCUITPY_PS2IO = 1 CIRCUITPY_JPEGIO = 0 CIRCUITPY_SPITARGET = 0 CIRCUITPY_SYNTHIO = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 diff --git a/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk b/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk index 8f06d8d1e5168..f6c07e7aaafb4 100644 --- a/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk @@ -12,3 +12,4 @@ LONGINT_IMPL = MPZ CIRCUITPY_SYNTHIO = 0 CIRCUITPY_JPEGIO = 0 CIRCUITPY_SPITARGET = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 diff --git a/ports/atmel-samd/boards/hallowing_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/hallowing_m4_express/mpconfigboard.mk index 74f1d3e55993e..3a0eeb3f2130e 100644 --- a/ports/atmel-samd/boards/hallowing_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/hallowing_m4_express/mpconfigboard.mk @@ -13,3 +13,4 @@ LONGINT_IMPL = MPZ CIRCUITPY_AESIO = 0 CIRCUITPY_JPEGIO = 0 CIRCUITPY_SYNTHIO = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk index de70d4fe0e552..27bf8c165da88 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk @@ -14,6 +14,7 @@ CIRCUITPY_CODEOP = 0 CIRCUITPY_FLOPPYIO = 0 CIRCUITPY_GIFIO = 0 CIRCUITPY_JPEGIO = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 CIRCUITPY_BITBANG_APA102 = 1 diff --git a/ports/atmel-samd/boards/openbook_m4/mpconfigboard.mk b/ports/atmel-samd/boards/openbook_m4/mpconfigboard.mk index a0f5ada80be5a..20e9cda0eb480 100644 --- a/ports/atmel-samd/boards/openbook_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/openbook_m4/mpconfigboard.mk @@ -15,3 +15,4 @@ CIRCUITPY_SYNTHIO = 0 CIRCUITPY_JPEGIO = 0 CIRCUITPY_FLOPPYIO = 0 CIRCUITPY_TERMINALIO_VT100 = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 diff --git a/ports/atmel-samd/boards/pewpew_lcd/mpconfigboard.mk b/ports/atmel-samd/boards/pewpew_lcd/mpconfigboard.mk index 823f5698156cd..c734323a6ab62 100644 --- a/ports/atmel-samd/boards/pewpew_lcd/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pewpew_lcd/mpconfigboard.mk @@ -54,6 +54,7 @@ CIRCUITPY_PULSEIO = 0 CIRCUITPY_RGBMATRIX = 0 CIRCUITPY_ROTARYIO = 0 CIRCUITPY_RTC = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 CIRCUITPY_ULAB = 0 CIRCUITPY_USB_HID = 0 CIRCUITPY_USB_MIDI = 0 diff --git a/ports/atmel-samd/boards/silicognition-m4-shim/mpconfigboard.mk b/ports/atmel-samd/boards/silicognition-m4-shim/mpconfigboard.mk index 16e439d3ef91f..214099ccc1651 100644 --- a/ports/atmel-samd/boards/silicognition-m4-shim/mpconfigboard.mk +++ b/ports/atmel-samd/boards/silicognition-m4-shim/mpconfigboard.mk @@ -13,3 +13,4 @@ LONGINT_IMPL = MPZ CIRCUITPY_JPEGIO = 0 CIRCUITPY_SPITARGET = 0 CIRCUITPY_SYNTHIO = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 diff --git a/ports/atmel-samd/boards/uartlogger2/mpconfigboard.mk b/ports/atmel-samd/boards/uartlogger2/mpconfigboard.mk index c3626ab98f3bb..82d7f0f43ba59 100644 --- a/ports/atmel-samd/boards/uartlogger2/mpconfigboard.mk +++ b/ports/atmel-samd/boards/uartlogger2/mpconfigboard.mk @@ -13,3 +13,4 @@ CIRCUITPY_SPITARGET = 0 CIRCUITPY_SYNTHIO = 0 CIRCUITPY_JPEGIO = 0 CIRCUITPY_TERMINALIO_VT100 = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index c2961e4165f23..15013b2d831ca 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -60,6 +60,7 @@ CIRCUITPY_ZLIB = 0 # Turn off a few more things that don't fit in 192kB CIRCUITPY_TERMINALIO_VT100 = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 ifeq ($(INTERNAL_FLASH_FILESYSTEM),1) CIRCUITPY_ONEWIREIO ?= 0 diff --git a/ports/stm/boards/meowbit_v121/mpconfigboard.mk b/ports/stm/boards/meowbit_v121/mpconfigboard.mk index 52c191b5b2efb..bde49ad13ef49 100644 --- a/ports/stm/boards/meowbit_v121/mpconfigboard.mk +++ b/ports/stm/boards/meowbit_v121/mpconfigboard.mk @@ -29,6 +29,7 @@ CIRCUITPY_BLEIO_HCI = 0 CIRCUITPY_EPAPERDISPLAY = 0 CIRCUITPY_KEYPAD_DEMUX = 0 CIRCUITPY_SHARPDISPLAY = 0 +CIRCUITPY_TILEPALETTEMAPPER = 0 CIRCUITPY_ULAB = 0 CIRCUITPY_ZLIB = 0 diff --git a/ports/zephyr-cp/boards/nordic/nrf5340dk/autogen_board_info.toml b/ports/zephyr-cp/boards/nordic/nrf5340dk/autogen_board_info.toml index a521b62fb4c30..ba0291f64300a 100644 --- a/ports/zephyr-cp/boards/nordic/nrf5340dk/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/nordic/nrf5340dk/autogen_board_info.toml @@ -90,6 +90,7 @@ struct = true supervisor = false synthio = false terminalio = false +tilepalettemapper = false time = true touchio = false traceback = false diff --git a/ports/zephyr-cp/boards/nordic/nrf54l15dk/autogen_board_info.toml b/ports/zephyr-cp/boards/nordic/nrf54l15dk/autogen_board_info.toml index d8266a9b8515f..88178af3b28c2 100644 --- a/ports/zephyr-cp/boards/nordic/nrf54l15dk/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/nordic/nrf54l15dk/autogen_board_info.toml @@ -90,6 +90,7 @@ struct = true supervisor = false synthio = false terminalio = false +tilepalettemapper = false time = true touchio = false traceback = false diff --git a/ports/zephyr-cp/boards/nordic/nrf7002dk/autogen_board_info.toml b/ports/zephyr-cp/boards/nordic/nrf7002dk/autogen_board_info.toml index baacdff5dd1e7..85b5a9c7e0fad 100644 --- a/ports/zephyr-cp/boards/nordic/nrf7002dk/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/nordic/nrf7002dk/autogen_board_info.toml @@ -90,6 +90,7 @@ struct = true supervisor = false synthio = false terminalio = false +tilepalettemapper = false time = true touchio = false traceback = false diff --git a/ports/zephyr-cp/boards/renesas/ek_ra6m5/autogen_board_info.toml b/ports/zephyr-cp/boards/renesas/ek_ra6m5/autogen_board_info.toml index 31e979864ac1d..674a19ab107c9 100644 --- a/ports/zephyr-cp/boards/renesas/ek_ra6m5/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/renesas/ek_ra6m5/autogen_board_info.toml @@ -90,6 +90,7 @@ struct = true supervisor = false synthio = false terminalio = false +tilepalettemapper = false time = true touchio = false traceback = false diff --git a/ports/zephyr-cp/boards/renesas/ek_ra8d1/autogen_board_info.toml b/ports/zephyr-cp/boards/renesas/ek_ra8d1/autogen_board_info.toml index 0f366c079bb6b..4f920c59f11d9 100644 --- a/ports/zephyr-cp/boards/renesas/ek_ra8d1/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/renesas/ek_ra8d1/autogen_board_info.toml @@ -90,6 +90,7 @@ struct = true supervisor = false synthio = false terminalio = false +tilepalettemapper = false time = true touchio = false traceback = false diff --git a/ports/zephyr-cp/boards/st/nucleo_u575zi_q/autogen_board_info.toml b/ports/zephyr-cp/boards/st/nucleo_u575zi_q/autogen_board_info.toml index cb20c3e59300e..000155eafa744 100644 --- a/ports/zephyr-cp/boards/st/nucleo_u575zi_q/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/st/nucleo_u575zi_q/autogen_board_info.toml @@ -90,6 +90,7 @@ struct = true supervisor = false synthio = false terminalio = false +tilepalettemapper = false time = true touchio = false traceback = false diff --git a/ports/zephyr-cp/boards/st/stm32h7b3i_dk/autogen_board_info.toml b/ports/zephyr-cp/boards/st/stm32h7b3i_dk/autogen_board_info.toml index c755838d554a2..ac0aff61a59c3 100644 --- a/ports/zephyr-cp/boards/st/stm32h7b3i_dk/autogen_board_info.toml +++ b/ports/zephyr-cp/boards/st/stm32h7b3i_dk/autogen_board_info.toml @@ -90,6 +90,7 @@ struct = true supervisor = false synthio = false terminalio = false +tilepalettemapper = false time = true touchio = false traceback = false diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 166256817ad04..513f7d2d64d97 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -396,6 +396,9 @@ endif ifeq ($(CIRCUITPY_FONTIO),1) SRC_PATTERNS += fontio/% endif +ifeq ($(CIRCUITPY_TILEPALETTEMAPPER),1) +SRC_PATTERNS += tilepalettemapper/% +endif ifeq ($(CIRCUITPY_TIME),1) SRC_PATTERNS += time/% endif @@ -733,6 +736,8 @@ SRC_SHARED_MODULE_ALL = \ synthio/__init__.c \ terminalio/Terminal.c \ terminalio/__init__.c \ + tilepalettemapper/__init__.c \ + tilepalettemapper/TilePaletteMapper.c \ time/__init__.c \ traceback/__init__.c \ uheap/__init__.c \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 69ebd03e8b295..831c1ec245fc3 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -558,6 +558,9 @@ CFLAGS += -DCIRCUITPY_TERMINALIO_VT100=$(CIRCUITPY_TERMINALIO_VT100) CIRCUITPY_FONTIO ?= $(call enable-if-all,$(CIRCUITPY_DISPLAYIO) $(CIRCUITPY_TERMINALIO)) CFLAGS += -DCIRCUITPY_FONTIO=$(CIRCUITPY_FONTIO) +CIRCUITPY_TILEPALETTEMAPPER ?= $(CIRCUITPY_DISPLAYIO) +CFLAGS += -DCIRCUITPY_TILEPALETTEMAPPER=$(CIRCUITPY_TILEPALETTEMAPPER) + CIRCUITPY_TIME ?= 1 CFLAGS += -DCIRCUITPY_TIME=$(CIRCUITPY_TIME) diff --git a/shared-bindings/displayio/TileGrid.c b/shared-bindings/displayio/TileGrid.c index 99f1699fbe89c..a8757fbcc17f2 100644 --- a/shared-bindings/displayio/TileGrid.c +++ b/shared-bindings/displayio/TileGrid.c @@ -17,6 +17,25 @@ #include "shared-bindings/displayio/ColorConverter.h" #include "shared-bindings/displayio/OnDiskBitmap.h" #include "shared-bindings/displayio/Palette.h" +#ifdef CIRCUITPY_TILEPALETTEMAPPER +#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h" +#endif + + +void displayio_tilegrid_validate_pixel_shader(mp_obj_t pixel_shader) { + bool valid_type = true; + if (!mp_obj_is_type(pixel_shader, &displayio_palette_type) && !mp_obj_is_type(pixel_shader, &displayio_colorconverter_type)) { + valid_type = false; + } + #if CIRCUITPY_TILEPALETTEMAPPER + if (mp_obj_is_type(pixel_shader, &tilepalettemapper_tilepalettemapper_type)) { + valid_type = true; + } + #endif + if (!valid_type) { + mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_pixel_shader); + } +} //| class TileGrid: //| """A grid of tiles sourced out of one bitmap @@ -89,10 +108,7 @@ static mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_ mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_bitmap); } mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; - if (!mp_obj_is_type(pixel_shader, &displayio_colorconverter_type) && - !mp_obj_is_type(pixel_shader, &displayio_palette_type)) { - mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_pixel_shader); - } + displayio_tilegrid_validate_pixel_shader(pixel_shader); uint16_t tile_width = args[ARG_tile_width].u_int; if (tile_width == 0) { tile_width = bitmap_width; @@ -323,10 +339,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(displayio_tilegrid_get_pixel_shader_obj, displayio_til static mp_obj_t displayio_tilegrid_obj_set_pixel_shader(mp_obj_t self_in, mp_obj_t pixel_shader) { displayio_tilegrid_t *self = native_tilegrid(self_in); - if (!mp_obj_is_type(pixel_shader, &displayio_palette_type) && !mp_obj_is_type(pixel_shader, &displayio_colorconverter_type)) { - mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_pixel_shader); - } - + displayio_tilegrid_validate_pixel_shader(pixel_shader); common_hal_displayio_tilegrid_set_pixel_shader(self, pixel_shader); return mp_const_none; diff --git a/shared-bindings/tilepalettemapper/TilePaletteMapper.c b/shared-bindings/tilepalettemapper/TilePaletteMapper.c new file mode 100644 index 0000000000000..ee1475d16b50f --- /dev/null +++ b/shared-bindings/tilepalettemapper/TilePaletteMapper.c @@ -0,0 +1,167 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT +#include +#include +#include "shared/runtime/context_manager_helpers.h" +#include "py/binary.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/util.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h" + +//| class TilePaletteMapper: +//| """Remaps color indices from the source bitmap to alternate indices on a +//| per-tile basis. This allows for altering coloring of tiles based on +//| their tilegrid location. It also allows for using a limited color +//| bitmap with a wider array of colors.""" +//| +//| def __init__( +//| self, palette: displayio.Palette, input_color_count: int, width: int, height: int +//| ) -> None: +//| """Create a TilePaletteMApper object to store a set of color mappings for tiles. +//| +//| :param displayio.Palette palette: The palette to get mapped colors from. +//| :param int input_color_count: The number of colors in in the input bitmap. +//| :param int width: The width of the grid in tiles. +//| :param int height: The height of the grid in tiles.""" +//| + +static mp_obj_t tilepalettemapper_tilepalettemapper_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_palette, ARG_input_color_count, ARG_width, ARG_height }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_palette, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_input_color_count, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + mp_obj_t palette = args[ARG_palette].u_obj; + if (!mp_obj_is_type(palette, &displayio_palette_type)) { + mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_pixel_shader); + } + tilepalettemapper_tilepalettemapper_t *self = mp_obj_malloc(tilepalettemapper_tilepalettemapper_t, &tilepalettemapper_tilepalettemapper_type); + common_hal_tilepalettemapper_tilepalettemapper_construct(self, palette, args[ARG_input_color_count].u_int, args[ARG_width].u_int, args[ARG_height].u_int); + + return MP_OBJ_FROM_PTR(self); +} + +//| width: int +//| """Width of the tile palette mapper in tiles.""" +static mp_obj_t tilepalettemapper_tilepalettemapper_obj_get_width(mp_obj_t self_in) { + tilepalettemapper_tilepalettemapper_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_tilepalettemapper_tilepalettemapper_get_width(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(tilepalettemapper_tilepalettemapper_get_width_obj, tilepalettemapper_tilepalettemapper_obj_get_width); + +MP_PROPERTY_GETTER(tilepalettemapper_tilepalettemapper_width_obj, + (mp_obj_t)&tilepalettemapper_tilepalettemapper_get_width_obj); + +//| height: int +//| """Height of the tile palette mapper in tiles.""" +static mp_obj_t tilepalettemapper_tilepalettemapper_obj_get_height(mp_obj_t self_in) { + tilepalettemapper_tilepalettemapper_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_tilepalettemapper_tilepalettemapper_get_height(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(tilepalettemapper_tilepalettemapper_get_height_obj, tilepalettemapper_tilepalettemapper_obj_get_height); + +MP_PROPERTY_GETTER(tilepalettemapper_tilepalettemapper_height_obj, + (mp_obj_t)&tilepalettemapper_tilepalettemapper_get_height_obj); + + +//| palette: displayio.Palette +//| """The palette that the mapper uses.""" +//| +static mp_obj_t tilepalettemapper_tilepalettemapper_obj_get_palette(mp_obj_t self_in) { + tilepalettemapper_tilepalettemapper_t *self = MP_OBJ_TO_PTR(self_in); + return common_hal_tilepalettemapper_tilepalettemapper_get_palette(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(tilepalettemapper_tilepalettemapper_get_palette_obj, tilepalettemapper_tilepalettemapper_obj_get_palette); + +MP_PROPERTY_GETTER(tilepalettemapper_tilepalettemapper_palette_obj, + (mp_obj_t)&tilepalettemapper_tilepalettemapper_get_palette_obj); + + +//| def __getitem__(self, index: Union[Tuple[int, int], int]) -> Tuple[int]: +//| """Returns the mapping for the given index. The index can either be an x,y tuple or an int equal +//| to ``y * width + x``. +//| +//| This allows you to:: +//| +//| print(tpm[0])""" +//| ... +//| +//| def __setitem__(self, index: Union[Tuple[int, int], int], value: List[int]) -> None: +//| """Sets the mapping at the given tile index. The index can either be an x,y tuple or an int equal +//| to ``y * width + x``. +//| +//| This allows you to:: +//| +//| tpm[0] = [1,0] +//| +//| or:: +//| +//| tpm[0,0] = [1,0]""" +//| ... +//| +//| +static mp_obj_t tilepalettemapper_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value_obj) { + tilepalettemapper_tilepalettemapper_t *self = MP_OBJ_TO_PTR(self_in); + if (mp_obj_is_type(index_obj, &mp_type_slice)) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("Slices not supported")); + } else { + uint16_t x = 0; + uint16_t y = 0; + if (mp_obj_is_small_int(index_obj)) { + mp_int_t i = MP_OBJ_SMALL_INT_VALUE(index_obj); + uint16_t width = common_hal_tilepalettemapper_tilepalettemapper_get_width(self); + x = i % width; + y = i / width; + } else { + mp_obj_t *items; + mp_obj_get_array_fixed_n(index_obj, 2, &items); + x = mp_obj_get_int(items[0]); + y = mp_obj_get_int(items[1]); + } + if (x >= common_hal_tilepalettemapper_tilepalettemapper_get_width(self) || + y >= common_hal_tilepalettemapper_tilepalettemapper_get_height(self)) { + mp_raise_IndexError(MP_ERROR_TEXT("Tile index out of bounds")); + } + + if (value_obj == MP_OBJ_SENTINEL) { + // load + return common_hal_tilepalettemapper_tilepalettemapper_get_mapping(self, x, y); + } else if (value_obj == mp_const_none) { + return MP_OBJ_NULL; // op not supported + } else { + size_t len = 0; + mp_obj_t *items; + mp_obj_list_get(value_obj, &len, &items); + mp_arg_validate_int_range(len, 0, self->input_color_count, MP_QSTR_mapping_length); + common_hal_tilepalettemapper_tilepalettemapper_set_mapping(self, x, y, len, items); + } + } + return mp_const_none; +} + + +static const mp_rom_map_elem_t tilepalettemapper_tilepalettemapper_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&tilepalettemapper_tilepalettemapper_width_obj) }, + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&tilepalettemapper_tilepalettemapper_height_obj) }, + { MP_ROM_QSTR(MP_QSTR_palette), MP_ROM_PTR(&tilepalettemapper_tilepalettemapper_palette_obj) }, +}; +static MP_DEFINE_CONST_DICT(tilepalettemapper_tilepalettemapper_locals_dict, tilepalettemapper_tilepalettemapper_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + tilepalettemapper_tilepalettemapper_type, + MP_QSTR_TilePaletteMapper, + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, + make_new, tilepalettemapper_tilepalettemapper_make_new, + locals_dict, &tilepalettemapper_tilepalettemapper_locals_dict, + subscr, tilepalettemapper_subscr, + iter, mp_obj_generic_subscript_getiter + ); diff --git a/shared-bindings/tilepalettemapper/TilePaletteMapper.h b/shared-bindings/tilepalettemapper/TilePaletteMapper.h new file mode 100644 index 0000000000000..0e4eced23b7c3 --- /dev/null +++ b/shared-bindings/tilepalettemapper/TilePaletteMapper.h @@ -0,0 +1,18 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT +#include "shared-module/tilepalettemapper/TilePaletteMapper.h" + +extern const mp_obj_type_t tilepalettemapper_tilepalettemapper_type; + +void common_hal_tilepalettemapper_tilepalettemapper_construct(tilepalettemapper_tilepalettemapper_t *self, + mp_obj_t paltte, uint16_t input_color_count, uint16_t bitmap_width_in_tiles, uint16_t bitmap_height_in_tiles); + + +uint16_t common_hal_tilepalettemapper_tilepalettemapper_get_width(tilepalettemapper_tilepalettemapper_t *self); +uint16_t common_hal_tilepalettemapper_tilepalettemapper_get_height(tilepalettemapper_tilepalettemapper_t *self); +mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_palette(tilepalettemapper_tilepalettemapper_t *self); +mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_mapping(tilepalettemapper_tilepalettemapper_t *self, uint16_t x, uint16_t y); +void common_hal_tilepalettemapper_tilepalettemapper_set_mapping(tilepalettemapper_tilepalettemapper_t *self, uint16_t x, uint16_t y, size_t len, mp_obj_t *items); diff --git a/shared-bindings/tilepalettemapper/__init__.c b/shared-bindings/tilepalettemapper/__init__.c new file mode 100644 index 0000000000000..966de95076659 --- /dev/null +++ b/shared-bindings/tilepalettemapper/__init__.c @@ -0,0 +1,21 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks +// +// SPDX-License-Identifier: MIT +#include "py/obj.h" +#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h" + + +static const mp_rom_map_elem_t tilepalettemapper_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_tilepalettemapper) }, + { MP_ROM_QSTR(MP_QSTR_TilePaletteMapper), MP_ROM_PTR(&tilepalettemapper_tilepalettemapper_type) }, +}; +static MP_DEFINE_CONST_DICT(tilepalettemapper_module_globals, tilepalettemapper_module_globals_table); + +const mp_obj_module_t tilepalettemapper_module = { + .base = {&mp_type_module }, + .globals = (mp_obj_dict_t *)&tilepalettemapper_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_tilepalettemapper, tilepalettemapper_module); diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c index 926637c11fbdd..e0d18d0accfb8 100644 --- a/shared-module/displayio/TileGrid.c +++ b/shared-module/displayio/TileGrid.c @@ -11,6 +11,9 @@ #include "shared-bindings/displayio/ColorConverter.h" #include "shared-bindings/displayio/OnDiskBitmap.h" #include "shared-bindings/displayio/Palette.h" +#if CIRCUITPY_TILEPALETTEMAPPER +#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h" +#endif void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_t bitmap, uint16_t bitmap_width_in_tiles, uint16_t bitmap_height_in_tiles, @@ -468,7 +471,9 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, continue; } int16_t local_x = input_pixel.x / self->absolute_transform->scale; - uint16_t tile_location = ((local_y / self->tile_height + self->top_left_y) % self->height_in_tiles) * self->width_in_tiles + (local_x / self->tile_width + self->top_left_x) % self->width_in_tiles; + uint16_t x_tile_index = (local_x / self->tile_width + self->top_left_x) % self->width_in_tiles; + uint16_t y_tile_index = (local_y / self->tile_height + self->top_left_y) % self->height_in_tiles; + uint16_t tile_location = y_tile_index * self->width_in_tiles + x_tile_index; input_pixel.tile = tiles[tile_location]; input_pixel.tile_x = (input_pixel.tile % self->bitmap_width_in_tiles) * self->tile_width + local_x % self->tile_width; input_pixel.tile_y = (input_pixel.tile / self->bitmap_width_in_tiles) * self->tile_height + local_y % self->tile_height; @@ -485,6 +490,11 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, } output_pixel.opaque = true; + #if CIRCUITPY_TILEPALETTEMAPPER + if (mp_obj_is_type(self->pixel_shader, &tilepalettemapper_tilepalettemapper_type)) { + tilepalettemapper_tilepalettemapper_get_color(self->pixel_shader, colorspace, &input_pixel, &output_pixel, x_tile_index, y_tile_index); + } + #endif if (self->pixel_shader == mp_const_none) { output_pixel.pixel = input_pixel.pixel; } else if (mp_obj_is_type(self->pixel_shader, &displayio_palette_type)) { @@ -550,6 +560,11 @@ void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) { } else if (mp_obj_is_type(self->pixel_shader, &displayio_colorconverter_type)) { displayio_colorconverter_finish_refresh(self->pixel_shader); } + #if CIRCUITPY_TILEPALETTEMAPPER + if (mp_obj_is_type(self->pixel_shader, &tilepalettemapper_tilepalettemapper_type)) { + tilepalettemapper_tilepalettemapper_finish_refresh(self->pixel_shader); + } + #endif if (mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) { displayio_bitmap_finish_refresh(self->bitmap); } else if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { @@ -603,6 +618,12 @@ displayio_area_t *displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *sel displayio_palette_needs_refresh(self->pixel_shader)) || (mp_obj_is_type(self->pixel_shader, &displayio_colorconverter_type) && displayio_colorconverter_needs_refresh(self->pixel_shader)); + #if CIRCUITPY_TILEPALETTEMAPPER + self->full_change = self->full_change || + (mp_obj_is_type(self->pixel_shader, &tilepalettemapper_tilepalettemapper_type) && + tilepalettemapper_tilepalettemapper_needs_refresh(self->pixel_shader)); + #endif + if (self->full_change || first_draw) { self->current_area.next = tail; return &self->current_area; diff --git a/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h index c9c31696bf8f2..36d2c9a69b6cb 100644 --- a/shared-module/displayio/TileGrid.h +++ b/shared-module/displayio/TileGrid.h @@ -67,3 +67,4 @@ bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_ void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self); bool displayio_tilegrid_get_rendered_hidden(displayio_tilegrid_t *self); +void displayio_tilegrid_validate_pixel_shader(mp_obj_t pixel_shader); diff --git a/shared-module/tilepalettemapper/TilePaletteMapper.c b/shared-module/tilepalettemapper/TilePaletteMapper.c new file mode 100644 index 0000000000000..c988e908ab00b --- /dev/null +++ b/shared-module/tilepalettemapper/TilePaletteMapper.c @@ -0,0 +1,78 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT +#include +#include "py/runtime.h" +#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h" +#include "shared-bindings/displayio/Palette.h" + +void common_hal_tilepalettemapper_tilepalettemapper_construct(tilepalettemapper_tilepalettemapper_t *self, + mp_obj_t palette, uint16_t input_color_count, uint16_t width, uint16_t height) { + + self->palette = palette; + self->width_in_tiles = width; + self->height_in_tiles = height; + self->input_color_count = input_color_count; + self->needs_refresh = false; + int mappings_len = width * height; + self->tile_mappings = (uint16_t **)m_malloc(mappings_len * sizeof(uint16_t *)); + for (int i = 0; i < mappings_len; i++) { + self->tile_mappings[i] = (uint16_t *)m_malloc(input_color_count * sizeof(uint16_t)); + for (uint16_t j = 0; j < input_color_count; j++) { + self->tile_mappings[i][j] = j; + } + } +} + +uint16_t common_hal_tilepalettemapper_tilepalettemapper_get_width(tilepalettemapper_tilepalettemapper_t *self) { + return self->width_in_tiles; +} + +uint16_t common_hal_tilepalettemapper_tilepalettemapper_get_height(tilepalettemapper_tilepalettemapper_t *self) { + return self->height_in_tiles; +} + +mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_palette(tilepalettemapper_tilepalettemapper_t *self) { + return self->palette; +} + +mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_mapping(tilepalettemapper_tilepalettemapper_t *self, uint16_t x, uint16_t y) { + int index = x + y * self->width_in_tiles; + mp_obj_t result[self->input_color_count]; + for (uint32_t i = 0; i < self->input_color_count; i++) { + result[i] = mp_obj_new_int(self->tile_mappings[index][i]); + } + return mp_obj_new_tuple(self->input_color_count, result); +} + +void common_hal_tilepalettemapper_tilepalettemapper_set_mapping(tilepalettemapper_tilepalettemapper_t *self, uint16_t x, uint16_t y, size_t len, mp_obj_t *items) { + uint32_t palette_len = common_hal_displayio_palette_get_len(self->palette); + for (uint16_t i = 0; i < MIN(len, self->input_color_count); i++) { + int mapping_val = mp_arg_validate_type_int(items[i], MP_QSTR_mapping_value); + mp_arg_validate_int_range(mapping_val, 0, palette_len - 1, MP_QSTR_mapping_value); + self->tile_mappings[y * self->width_in_tiles + x][i] = mapping_val; + } + self->needs_refresh = true; +} + +void tilepalettemapper_tilepalettemapper_get_color(tilepalettemapper_tilepalettemapper_t *self, const _displayio_colorspace_t *colorspace, displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color, uint16_t x_tile_index, uint16_t y_tile_index) { + if (x_tile_index >= self->width_in_tiles || y_tile_index >= self->height_in_tiles) { + displayio_palette_get_color(self->palette, colorspace, input_pixel, output_color); + return; + } + uint16_t tile_index = y_tile_index * self->width_in_tiles + x_tile_index; + uint32_t mapped_index = self->tile_mappings[tile_index][input_pixel->pixel]; + displayio_input_pixel_t tmp_pixel; + tmp_pixel.pixel = mapped_index; + displayio_palette_get_color(self->palette, colorspace, &tmp_pixel, output_color); +} + +bool tilepalettemapper_tilepalettemapper_needs_refresh(tilepalettemapper_tilepalettemapper_t *self) { + return self->needs_refresh; +} + +void tilepalettemapper_tilepalettemapper_finish_refresh(tilepalettemapper_tilepalettemapper_t *self) { + self->needs_refresh = false; +} diff --git a/shared-module/tilepalettemapper/TilePaletteMapper.h b/shared-module/tilepalettemapper/TilePaletteMapper.h new file mode 100644 index 0000000000000..8502d4bf53601 --- /dev/null +++ b/shared-module/tilepalettemapper/TilePaletteMapper.h @@ -0,0 +1,29 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "shared-module/displayio/Palette.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_t palette; + uint16_t width_in_tiles; + uint16_t height_in_tiles; + uint16_t input_color_count; + uint16_t **tile_mappings; + bool needs_refresh; +} tilepalettemapper_tilepalettemapper_t; + +bool tilepalettemapper_tilepalettemapper_needs_refresh(tilepalettemapper_tilepalettemapper_t *self); +void tilepalettemapper_tilepalettemapper_finish_refresh(tilepalettemapper_tilepalettemapper_t *self); + +void tilepalettemapper_tilepalettemapper_get_color(tilepalettemapper_tilepalettemapper_t *self, const _displayio_colorspace_t *colorspace, displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color, uint16_t x_tile_index, uint16_t y_tile_index); diff --git a/shared-module/tilepalettemapper/__init__.c b/shared-module/tilepalettemapper/__init__.c new file mode 100644 index 0000000000000..e69de29bb2d1d