Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 14 additions & 8 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,8 @@ msgstr ""
msgid "File exists"
msgstr ""

#: shared-module/lvfontio/OnDiskFont.c shared-module/os/getenv.c
#: shared-bindings/supervisor/__init__.c shared-module/lvfontio/OnDiskFont.c
#: shared-module/os/getenv.c
msgid "File not found"
msgstr ""

Expand Down Expand Up @@ -1251,6 +1252,7 @@ msgstr ""
#: ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2040.c py/argcheck.c
#: shared-bindings/digitalio/DigitalInOut.c
#: shared-bindings/epaperdisplay/EPaperDisplay.c shared-bindings/pwmio/PWMOut.c
#: shared-bindings/supervisor/__init__.c
#: shared-module/aurora_epaper/aurora_framebuffer.c
#: shared-module/lvfontio/OnDiskFont.c
msgid "Invalid %q"
Expand Down Expand Up @@ -2068,6 +2070,10 @@ msgstr ""
msgid "Tile width must exactly divide bitmap width"
msgstr ""

#: shared-module/displayio/Group.c
msgid "TileGrid must have a pixel_shader"
msgstr ""

#: shared-bindings/alarm/time/TimeAlarm.c
msgid "Time is in the past."
msgstr ""
Expand Down Expand Up @@ -2591,16 +2597,16 @@ msgstr ""
msgid "bits must be 32 or less"
msgstr ""

#: shared-bindings/audiofreeverb/Freeverb.c
msgid "bits_per_sample must be 16"
msgstr ""

#: shared-bindings/audiodelays/Chorus.c shared-bindings/audiodelays/Echo.c
#: shared-bindings/audiodelays/MultiTapDelay.c
#: shared-bindings/audiodelays/PitchShift.c
#: shared-bindings/audiofilters/Distortion.c
#: shared-bindings/audiodelays/Reverb.c
msgid "bits_per_sample must be 16"
msgstr ""

#: shared-bindings/audiodelays/Echo.c shared-bindings/audiofilters/Distortion.c
#: shared-bindings/audiofilters/Filter.c shared-bindings/audiomixer/Mixer.c
#: shared-bindings/audiofilters/Filter.c shared-bindings/audiofilters/Phaser.c
#: shared-bindings/audiomixer/Mixer.c
msgid "bits_per_sample must be 8 or 16"
msgstr ""

Expand Down Expand Up @@ -4017,7 +4023,7 @@ msgstr ""
msgid "rsplit(None,n)"
msgstr ""

#: shared-bindings/audiodelays/Reverb.c
#: shared-bindings/audiofreeverb/Freeverb.c
msgid "samples_signed must be true"
msgstr ""

Expand Down
8 changes: 5 additions & 3 deletions shared-bindings/displayio/TileGrid.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void displayio_tilegrid_validate_pixel_shader(mp_obj_t pixel_shader) {
//| tile_width and tile_height match the height of the bitmap by default.
//|
//| :param Bitmap,OnDiskBitmap bitmap: The bitmap storing one or more tiles.
//| :param ColorConverter,Palette pixel_shader: The pixel shader that produces colors from values
//| :param Optional[ColorConverter,Palette] pixel_shader: The pixel shader that produces colors from values
//| :param int width: Width of the grid in tiles.
//| :param int height: Height of the grid in tiles.
//| :param int tile_width: Width of a single tile in pixels. Defaults to the full Bitmap and must evenly divide into the Bitmap's dimensions.
Expand All @@ -81,7 +81,7 @@ static mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_
enum { ARG_bitmap, ARG_pixel_shader, ARG_width, ARG_height, ARG_tile_width, ARG_tile_height, ARG_default_tile, ARG_x, ARG_y };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
{ MP_QSTR_tile_width, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
Expand Down Expand Up @@ -109,7 +109,9 @@ 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;
displayio_tilegrid_validate_pixel_shader(pixel_shader);
if (pixel_shader != mp_const_none) {
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;
Expand Down
31 changes: 24 additions & 7 deletions shared-bindings/tilepalettemapper/TilePaletteMapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "shared-bindings/util.h"
#include "shared-bindings/displayio/Palette.h"
#include "shared-bindings/displayio/ColorConverter.h"
#include "shared-bindings/displayio/TileGrid.h"
#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h"

//| class TilePaletteMapper:
Expand All @@ -21,33 +22,38 @@
//| bitmap with a wider array of colors."""
//|
//| def __init__(
//| self, palette: displayio.Palette, input_color_count: int, width: int, height: int
//| self, palette: displayio.Palette, input_color_count: int, tilegrid: displayio.TileGrid
//| ) -> None:
//| """Create a TilePaletteMApper object to store a set of color mappings for tiles.
//|
//| :param Union[displayio.Palette, displayio.ColorConverter] pixel_shader:
//| The palette or ColorConverter 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."""
//| :param TileGrid tilegrid: The tilegrid to use with the TilePaletteMapper"""
//|

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_pixel_shader, ARG_input_color_count, ARG_width, ARG_height };
enum { ARG_pixel_shader, ARG_input_color_count, ARG_tilegrid };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pixel_shader, 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_QSTR_tilegrid, MP_ARG_OBJ | 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 pixel_shader = args[ARG_pixel_shader].u_obj;
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);
}

mp_obj_t tilegrid = args[ARG_tilegrid].u_obj;
if (!mp_obj_is_type(tilegrid, &displayio_tilegrid_type)) {
mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_tilegrid);
}

tilepalettemapper_tilepalettemapper_t *self = mp_obj_malloc(tilepalettemapper_tilepalettemapper_t, &tilepalettemapper_tilepalettemapper_type);
common_hal_tilepalettemapper_tilepalettemapper_construct(self, pixel_shader, args[ARG_input_color_count].u_int, args[ARG_width].u_int, args[ARG_height].u_int);
common_hal_tilepalettemapper_tilepalettemapper_construct(self, pixel_shader, args[ARG_input_color_count].u_int, tilegrid);

return MP_OBJ_FROM_PTR(self);
}
Expand Down Expand Up @@ -87,6 +93,17 @@ MP_DEFINE_CONST_FUN_OBJ_1(tilepalettemapper_tilepalettemapper_get_pixel_shader_o
MP_PROPERTY_GETTER(tilepalettemapper_tilepalettemapper_palette_obj,
(mp_obj_t)&tilepalettemapper_tilepalettemapper_get_pixel_shader_obj);

//| tilegrid: displayio.TileGrid
//| """The TileGrid that the TilePaletteMapper is used with."""
//|
static mp_obj_t tilepalettemapper_tilepalettemapper_obj_get_tilegrid(mp_obj_t self_in) {
tilepalettemapper_tilepalettemapper_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_tilepalettemapper_tilepalettemapper_get_tilegrid(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(tilepalettemapper_tilepalettemapper_get_tilegrid_obj, tilepalettemapper_tilepalettemapper_obj_get_tilegrid);

MP_PROPERTY_GETTER(tilepalettemapper_tilepalettemapper_tilegrid_obj,
(mp_obj_t)&tilepalettemapper_tilepalettemapper_get_tilegrid_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
Expand Down
3 changes: 2 additions & 1 deletion shared-bindings/tilepalettemapper/TilePaletteMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
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);
mp_obj_t paltte, uint16_t input_color_count, mp_obj_t tilegrid);


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_pixel_shader(tilepalettemapper_tilepalettemapper_t *self);
mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_tilegrid(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);
3 changes: 3 additions & 0 deletions shared-module/displayio/Group.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ static void _add_layer(displayio_group_t *self, mp_obj_t layer) {
mp_obj_t native_layer = mp_obj_cast_to_native_base(layer, &displayio_tilegrid_type);
if (native_layer != MP_OBJ_NULL) {
displayio_tilegrid_t *tilegrid = native_layer;
if (tilegrid->pixel_shader == mp_const_none) {
mp_raise_ValueError(MP_ERROR_TEXT("TileGrid must have a pixel_shader"));
}
if (tilegrid->in_group) {
mp_raise_ValueError(MP_ERROR_TEXT("Layer already in a group"));
} else {
Expand Down
53 changes: 23 additions & 30 deletions shared-module/displayio/TileGrid.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,25 +275,7 @@ uint16_t common_hal_displayio_tilegrid_get_tile(displayio_tilegrid_t *self, uint
}
}

void common_hal_displayio_tilegrid_set_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y, uint16_t tile_index) {
if (tile_index >= self->tiles_in_bitmap) {
mp_raise_ValueError(MP_ERROR_TEXT("Tile index out of bounds"));
}

void *tiles = self->tiles;
if (self->inline_tiles) {
tiles = &self->tiles;
}
if (tiles == NULL) {
return;
}

uint32_t index = y * self->width_in_tiles + x;
if (self->tiles_in_bitmap > 255) {
((uint16_t *)tiles)[index] = tile_index;
} else {
((uint8_t *)tiles)[index] = (uint8_t)tile_index;
}
void displayio_tilegrid_mark_tile_dirty(displayio_tilegrid_t *self, uint16_t x, uint16_t y) {
displayio_area_t temp_area;
displayio_area_t *tile_area;
if (!self->partial_change) {
Expand All @@ -317,10 +299,31 @@ void common_hal_displayio_tilegrid_set_tile(displayio_tilegrid_t *self, uint16_t
if (self->partial_change) {
displayio_area_union(&self->dirty_area, &temp_area, &self->dirty_area);
}

self->partial_change = true;
}

void common_hal_displayio_tilegrid_set_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y, uint16_t tile_index) {
if (tile_index >= self->tiles_in_bitmap) {
mp_raise_ValueError(MP_ERROR_TEXT("Tile index out of bounds"));
}

void *tiles = self->tiles;
if (self->inline_tiles) {
tiles = &self->tiles;
}
if (tiles == NULL) {
return;
}

uint32_t index = y * self->width_in_tiles + x;
if (self->tiles_in_bitmap > 255) {
((uint16_t *)tiles)[index] = tile_index;
} else {
((uint8_t *)tiles)[index] = (uint8_t)tile_index;
}
displayio_tilegrid_mark_tile_dirty(self, x, y);
}

void common_hal_displayio_tilegrid_set_all_tiles(displayio_tilegrid_t *self, uint16_t tile_index) {
if (tile_index >= self->tiles_in_bitmap) {
mp_raise_ValueError(MP_ERROR_TEXT("Tile index out of bounds"));
Expand Down Expand Up @@ -613,11 +616,6 @@ 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)) {
Expand Down Expand Up @@ -671,11 +669,6 @@ 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;
Expand Down
2 changes: 2 additions & 0 deletions shared-module/displayio/TileGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ 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);

void displayio_tilegrid_mark_tile_dirty(displayio_tilegrid_t *self, uint16_t x, uint16_t y);
27 changes: 12 additions & 15 deletions shared-module/tilepalettemapper/TilePaletteMapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
#include "shared-bindings/tilepalettemapper/TilePaletteMapper.h"
#include "shared-bindings/displayio/Palette.h"
#include "shared-bindings/displayio/ColorConverter.h"
#include "shared-bindings/displayio/TileGrid.h"

void common_hal_tilepalettemapper_tilepalettemapper_construct(tilepalettemapper_tilepalettemapper_t *self,
mp_obj_t pixel_shader, uint16_t input_color_count, uint16_t width, uint16_t height) {

mp_obj_t pixel_shader, uint16_t input_color_count, mp_obj_t tilegrid) {
self->tilegrid = tilegrid;
self->pixel_shader = pixel_shader;
self->width_in_tiles = width;
self->height_in_tiles = height;
self->width_in_tiles = common_hal_displayio_tilegrid_get_width(tilegrid);
self->height_in_tiles = common_hal_displayio_tilegrid_get_height(tilegrid);
self->input_color_count = input_color_count;
self->needs_refresh = false;
int mappings_len = width * height;
int mappings_len = self->width_in_tiles * self->height_in_tiles;
self->tile_mappings = (uint32_t **)m_malloc(mappings_len * sizeof(uint32_t *));
for (int i = 0; i < mappings_len; i++) {
self->tile_mappings[i] = (uint32_t *)m_malloc_without_collect(input_color_count * sizeof(uint32_t));
Expand All @@ -31,6 +31,7 @@ void common_hal_tilepalettemapper_tilepalettemapper_construct(tilepalettemapper_
}
}
}
common_hal_displayio_tilegrid_set_pixel_shader(self->tilegrid, self);
}

uint16_t common_hal_tilepalettemapper_tilepalettemapper_get_width(tilepalettemapper_tilepalettemapper_t *self) {
Expand All @@ -45,6 +46,10 @@ mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_pixel_shader(tilepal
return self->pixel_shader;
}

mp_obj_t common_hal_tilepalettemapper_tilepalettemapper_get_tilegrid(tilepalettemapper_tilepalettemapper_t *self) {
return self->tilegrid;
}

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];
Expand All @@ -67,7 +72,7 @@ void common_hal_tilepalettemapper_tilepalettemapper_set_mapping(tilepalettemappe
mp_arg_validate_int_range(mapping_val, 0, palette_max, MP_QSTR_mapping_value);
self->tile_mappings[y * self->width_in_tiles + x][i] = mapping_val;
}
self->needs_refresh = true;
displayio_tilegrid_mark_tile_dirty(self->tilegrid, x, y);
Copy link
Member

Choose a reason for hiding this comment

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

Is there a chance that someone will set the existing mapping again? Maybe we want to detect when nothing is actually changed and not mark the tile dirty then.

}

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) {
Expand All @@ -90,11 +95,3 @@ void tilepalettemapper_tilepalettemapper_get_color(tilepalettemapper_tilepalette
}

}

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;
}
5 changes: 1 addition & 4 deletions shared-module/tilepalettemapper/TilePaletteMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
typedef struct {
mp_obj_base_t base;
mp_obj_t pixel_shader;
mp_obj_t tilegrid;
uint16_t width_in_tiles;
uint16_t height_in_tiles;
uint16_t input_color_count;
uint32_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);