Add TileGrid
This commit is contained in:
parent
63d456127b
commit
73cf490635
@ -389,6 +389,7 @@ SRC_SHARED_MODULE = \
|
||||
displayio/Palette.c \
|
||||
displayio/Shape.c \
|
||||
displayio/Sprite.c \
|
||||
displayio/TileGrid.c \
|
||||
gamepad/__init__.c \
|
||||
gamepad/GamePad.c \
|
||||
_stage/__init__.c \
|
||||
|
@ -39,7 +39,7 @@
|
||||
#include "shared-bindings/displayio/Shape.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
void unpack_position(mp_obj_t position_obj, int16_t* x, int16_t* y) {
|
||||
static void unpack_position(mp_obj_t position_obj, int16_t* x, int16_t* y) {
|
||||
// TODO(tannewt): Support any value sequence such as bytearray or bytes.
|
||||
mp_obj_tuple_t *position = MP_OBJ_TO_PTR(position_obj);
|
||||
if (MP_OBJ_IS_TYPE(position_obj, &mp_type_tuple) && position->len == 2) {
|
||||
|
226
shared-bindings/displayio/TileGrid.c
Normal file
226
shared-bindings/displayio/TileGrid.c
Normal file
@ -0,0 +1,226 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "shared-bindings/displayio/TileGrid.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "lib/utils/context_manager_helpers.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/displayio/Bitmap.h"
|
||||
#include "shared-bindings/displayio/ColorConverter.h"
|
||||
#include "shared-bindings/displayio/OnDiskBitmap.h"
|
||||
#include "shared-bindings/displayio/Palette.h"
|
||||
#include "shared-bindings/displayio/Shape.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
static void unpack_position(mp_obj_t position_obj, int16_t* x, int16_t* y) {
|
||||
// TODO(tannewt): Support any value sequence such as bytearray or bytes.
|
||||
mp_obj_tuple_t *position = MP_OBJ_TO_PTR(position_obj);
|
||||
if (MP_OBJ_IS_TYPE(position_obj, &mp_type_tuple) && position->len == 2) {
|
||||
*x = mp_obj_get_int(position->items[0]);
|
||||
*y = mp_obj_get_int(position->items[1]);
|
||||
} else if (position != mp_const_none) {
|
||||
mp_raise_TypeError(translate("position must be 2-tuple"));
|
||||
}
|
||||
}
|
||||
|
||||
//| .. currentmodule:: displayio
|
||||
//|
|
||||
//| :class:`TileGrid` -- A grid of tiles sourced out of one bitmap
|
||||
//| ==========================================================================
|
||||
//|
|
||||
//| Position a grid of tiles sourced from a bitmap and pixel_shader combination. Multiple grids
|
||||
//| can share bitmaps and pixel shaders.
|
||||
//|
|
||||
//| A single tile grid is also known as a Sprite.
|
||||
//|
|
||||
//| .. warning:: This will be changed before 4.0.0. Consider it very experimental.
|
||||
//|
|
||||
//| .. class:: TileGrid(bitmap, *, pixel_shader, position, width=1, height=1, tile_width=None, tile_height=None, default_tile=0)
|
||||
//|
|
||||
//| Create a TileGrid object. The bitmap is source for 2d pixels. The pixel_shader is used to
|
||||
//| convert the value and its location to a display native pixel color. This may be a simple color
|
||||
//| palette lookup, a gradient, a pattern or a color transformer.
|
||||
//|
|
||||
//| tile_width and tile_height match the height of the bitmap by default.
|
||||
//|
|
||||
//| :param displayio.Bitmap bitmap: The bitmap storing one or more tiles.
|
||||
//| :param displayio.Palette pixel_shader: The pixel shader that produces colors from values
|
||||
//| :param tuple position: Upper left corner of the grid
|
||||
//| :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.
|
||||
//| :param int tile_height: Height of a single tile in pixels. Defaults to the full Bitmap and must evenly divide into the Bitmap's dimensions.
|
||||
//| :param in default_tile: Default tile index to show.
|
||||
//|
|
||||
STATIC mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_bitmap, ARG_pixel_shader, ARG_position, ARG_width, ARG_height, ARG_tile_width, ARG_tile_height, ARG_default_tile };
|
||||
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_position, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
|
||||
{ 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} },
|
||||
{ MP_QSTR_tile_height, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
{ MP_QSTR_default_tile, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
mp_obj_t bitmap = args[ARG_bitmap].u_obj;
|
||||
|
||||
uint16_t bitmap_width;
|
||||
uint16_t bitmap_height;
|
||||
mp_obj_t native = mp_instance_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(translate("unsupported bitmap type"));
|
||||
}
|
||||
uint16_t tile_width = args[ARG_tile_width].u_int;
|
||||
if (tile_width == 0) {
|
||||
tile_width = bitmap_width;
|
||||
}
|
||||
uint16_t tile_height = args[ARG_tile_height].u_int;
|
||||
if (tile_height == 0) {
|
||||
tile_height = bitmap_height;
|
||||
}
|
||||
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"));
|
||||
}
|
||||
|
||||
int16_t x = 0;
|
||||
int16_t y = 0;
|
||||
mp_obj_t position_obj = args[ARG_position].u_obj;
|
||||
unpack_position(position_obj, &x, &y);
|
||||
|
||||
displayio_tilegrid_t *self = m_new_obj(displayio_tilegrid_t);
|
||||
self->base.type = &displayio_tilegrid_type;
|
||||
common_hal_displayio_tilegrid_construct(self, native, args[ARG_pixel_shader].u_obj,
|
||||
args[ARG_width].u_int, args[ARG_height].u_int, tile_width, tile_height, x, y,
|
||||
args[ARG_default_tile].u_int);
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
//| .. attribute:: position
|
||||
//|
|
||||
//| The position of the top-left corner of the tilegrid.
|
||||
//|
|
||||
STATIC mp_obj_t displayio_tilegrid_obj_get_position(mp_obj_t self_in) {
|
||||
displayio_tilegrid_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
common_hal_displayio_tilegrid_get_position(self, &x, &y);
|
||||
|
||||
mp_obj_t coords[2];
|
||||
coords[0] = mp_obj_new_int(x);
|
||||
coords[1] = mp_obj_new_int(y);
|
||||
|
||||
return mp_obj_new_tuple(2, coords);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(displayio_tilegrid_get_position_obj, displayio_tilegrid_obj_get_position);
|
||||
|
||||
STATIC mp_obj_t displayio_tilegrid_obj_set_position(mp_obj_t self_in, mp_obj_t value) {
|
||||
displayio_tilegrid_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
int16_t x = 0;
|
||||
int16_t y = 0;
|
||||
unpack_position(value, &x, &y);
|
||||
|
||||
common_hal_displayio_tilegrid_set_position(self, x, y);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(displayio_tilegrid_set_position_obj, displayio_tilegrid_obj_set_position);
|
||||
|
||||
const mp_obj_property_t displayio_tilegrid_position_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&displayio_tilegrid_get_position_obj,
|
||||
(mp_obj_t)&displayio_tilegrid_set_position_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
//| .. attribute:: pixel_shader
|
||||
//|
|
||||
//| The pixel shader of the tilegrid.
|
||||
//|
|
||||
STATIC mp_obj_t displayio_tilegrid_obj_get_pixel_shader(mp_obj_t self_in) {
|
||||
displayio_tilegrid_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return common_hal_displayio_tilegrid_get_pixel_shader(self);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(displayio_tilegrid_get_pixel_shader_obj, displayio_tilegrid_obj_get_pixel_shader);
|
||||
|
||||
STATIC mp_obj_t displayio_tilegrid_obj_set_pixel_shader(mp_obj_t self_in, mp_obj_t pixel_shader) {
|
||||
displayio_tilegrid_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
if (!MP_OBJ_IS_TYPE(pixel_shader, &displayio_palette_type) && !MP_OBJ_IS_TYPE(pixel_shader, &displayio_colorconverter_type)) {
|
||||
mp_raise_TypeError(translate("pixel_shader must be displayio.Palette or displayio.ColorConverter"));
|
||||
}
|
||||
|
||||
common_hal_displayio_tilegrid_set_pixel_shader(self, pixel_shader);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(displayio_tilegrid_set_pixel_shader_obj, displayio_tilegrid_obj_set_pixel_shader);
|
||||
|
||||
const mp_obj_property_t displayio_tilegrid_pixel_shader_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&displayio_tilegrid_get_pixel_shader_obj,
|
||||
(mp_obj_t)&displayio_tilegrid_set_pixel_shader_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t displayio_tilegrid_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_position), MP_ROM_PTR(&displayio_tilegrid_position_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_tilegrid_pixel_shader_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(displayio_tilegrid_locals_dict, displayio_tilegrid_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t displayio_tilegrid_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Sprite,
|
||||
.make_new = displayio_tilegrid_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&displayio_tilegrid_locals_dict,
|
||||
};
|
44
shared-bindings/displayio/TileGrid.h
Normal file
44
shared-bindings/displayio/TileGrid.h
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_TILEGRID_H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_TILEGRID_H
|
||||
|
||||
#include "shared-module/displayio/TileGrid.h"
|
||||
|
||||
extern const mp_obj_type_t displayio_tilegrid_type;
|
||||
|
||||
void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_t bitmap,
|
||||
mp_obj_t pixel_shader, uint16_t width, uint16_t height,
|
||||
uint16_t tile_width, uint16_t tile_height, uint16_t x, uint16_t y, uint8_t default_tile);
|
||||
|
||||
void common_hal_displayio_tilegrid_get_position(displayio_tilegrid_t *self, int16_t* x, int16_t* y);
|
||||
void common_hal_displayio_tilegrid_set_position(displayio_tilegrid_t *self, int16_t x, int16_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);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_TILEGRID_H
|
@ -40,6 +40,7 @@
|
||||
#include "shared-bindings/displayio/ParallelBus.h"
|
||||
#include "shared-bindings/displayio/Shape.h"
|
||||
#include "shared-bindings/displayio/Sprite.h"
|
||||
#include "shared-bindings/displayio/TileGrid.h"
|
||||
|
||||
//| :mod:`displayio` --- Native display driving
|
||||
//| =========================================================================
|
||||
@ -68,6 +69,7 @@
|
||||
//| ParallelBus
|
||||
//| Shape
|
||||
//| Sprite
|
||||
//| TileGrid
|
||||
//|
|
||||
//| All libraries change hardware state but are never deinit
|
||||
//|
|
||||
@ -95,6 +97,7 @@ STATIC const mp_rom_map_elem_t displayio_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_Palette), MP_ROM_PTR(&displayio_palette_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Shape), MP_ROM_PTR(&displayio_shape_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Sprite), MP_ROM_PTR(&displayio_sprite_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_TileGrid), MP_ROM_PTR(&displayio_tilegrid_type) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_FourWire), MP_ROM_PTR(&displayio_fourwire_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ParallelBus), MP_ROM_PTR(&displayio_parallelbus_type) },
|
||||
|
@ -28,6 +28,7 @@
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/displayio/Sprite.h"
|
||||
#include "shared-bindings/displayio/TileGrid.h"
|
||||
|
||||
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size) {
|
||||
mp_obj_t* children = m_new(mp_obj_t, max_size);
|
||||
@ -43,7 +44,10 @@ void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer)
|
||||
native_layer = mp_instance_cast_to_native_base(layer, &displayio_sprite_type);
|
||||
}
|
||||
if (native_layer == MP_OBJ_NULL) {
|
||||
mp_raise_ValueError(translate("Layer must be a Group or Sprite subclass."));
|
||||
native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type);
|
||||
}
|
||||
if (native_layer == MP_OBJ_NULL) {
|
||||
mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass."));
|
||||
}
|
||||
self->children[self->size] = layer;
|
||||
self->size++;
|
||||
@ -77,7 +81,11 @@ bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, ui
|
||||
y /= self->scale;
|
||||
for (int32_t i = self->size - 1; i >= 0 ; i--) {
|
||||
mp_obj_t layer = self->children[i];
|
||||
if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) {
|
||||
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
|
||||
if (displayio_tilegrid_get_pixel(layer, x, y, pixel)) {
|
||||
return true;
|
||||
}
|
||||
} else if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) {
|
||||
if (displayio_sprite_get_pixel(layer, x, y, pixel)) {
|
||||
return true;
|
||||
}
|
||||
|
124
shared-module/displayio/TileGrid.c
Normal file
124
shared-module/displayio/TileGrid.c
Normal file
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "shared-bindings/displayio/TileGrid.h"
|
||||
|
||||
#include "shared-bindings/displayio/Bitmap.h"
|
||||
#include "shared-bindings/displayio/ColorConverter.h"
|
||||
#include "shared-bindings/displayio/OnDiskBitmap.h"
|
||||
#include "shared-bindings/displayio/Palette.h"
|
||||
#include "shared-bindings/displayio/Shape.h"
|
||||
|
||||
void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_t bitmap,
|
||||
mp_obj_t pixel_shader, uint16_t width, uint16_t height,
|
||||
uint16_t tile_width, uint16_t tile_height, uint16_t x, uint16_t y, uint8_t default_tile) {
|
||||
uint32_t total_tiles = width * height;
|
||||
// Sprites will only have one tile so save a little memory by inlining values in the pointer.
|
||||
uint8_t inline_tiles = sizeof(uint8_t*);
|
||||
if (total_tiles <= inline_tiles) {
|
||||
self->inline_tiles = true;
|
||||
} else {
|
||||
self->tiles = (uint8_t*) m_malloc(total_tiles, false);
|
||||
self->inline_tiles = false;
|
||||
}
|
||||
self->width_in_tiles = width;
|
||||
self->total_width = width * tile_width;
|
||||
self->total_height = height * tile_height;
|
||||
self->tile_width = tile_width;
|
||||
self->tile_height = tile_height;
|
||||
self->bitmap = bitmap;
|
||||
self->pixel_shader = pixel_shader;
|
||||
self->x = x;
|
||||
self->y = y;
|
||||
}
|
||||
|
||||
void common_hal_displayio_tilegrid_get_position(displayio_tilegrid_t *self, int16_t* x, int16_t* y) {
|
||||
*x = self->x;
|
||||
*y = self->y;
|
||||
}
|
||||
|
||||
void common_hal_displayio_tilegrid_set_position(displayio_tilegrid_t *self, int16_t x, int16_t y) {
|
||||
self->x = x;
|
||||
self->y = y;
|
||||
self->needs_refresh = true;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *self) {
|
||||
return self->pixel_shader;
|
||||
}
|
||||
|
||||
void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self, mp_obj_t pixel_shader) {
|
||||
self->pixel_shader = pixel_shader;
|
||||
self->needs_refresh = true;
|
||||
}
|
||||
|
||||
bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t y, uint16_t* pixel) {
|
||||
x -= self->x;
|
||||
y -= self->y;
|
||||
if (y < 0 || y >= self->total_height || x >= self->total_width || x < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t* tiles = self->tiles;
|
||||
if (self->inline_tiles) {
|
||||
tiles = (uint8_t*) &self->tiles;
|
||||
}
|
||||
uint16_t tile_location = (y / self->tile_height) * self->width_in_tiles + x / self->tile_width;
|
||||
uint8_t tile = tiles[tile_location];
|
||||
uint16_t tile_x = tile_x = (tile % self->width_in_tiles) * self->tile_width + x % self->tile_width;
|
||||
uint16_t tile_y = tile_y = (tile / self->width_in_tiles) * self->tile_height + y % self->tile_height;
|
||||
|
||||
uint32_t value = 0;
|
||||
if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
|
||||
value = common_hal_displayio_bitmap_get_pixel(self->bitmap, tile_x, tile_y);
|
||||
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) {
|
||||
value = common_hal_displayio_shape_get_pixel(self->bitmap, tile_x, tile_y);
|
||||
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
|
||||
value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y);
|
||||
}
|
||||
|
||||
if (self->pixel_shader == mp_const_none) {
|
||||
*pixel = value;
|
||||
return true;
|
||||
} else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type) && displayio_palette_get_color(self->pixel_shader, value, pixel)) {
|
||||
return true;
|
||||
} else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) && common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool displayio_tilegrid_needs_refresh(displayio_tilegrid_t *self) {
|
||||
return self->needs_refresh || displayio_palette_needs_refresh(self->pixel_shader);
|
||||
}
|
||||
|
||||
void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) {
|
||||
self->needs_refresh = false;
|
||||
displayio_palette_finish_refresh(self->pixel_shader);
|
||||
// TODO(tannewt): We could double buffer changes to position and move them over here.
|
||||
// That way they won't change during a refresh and tear.
|
||||
}
|
55
shared-module/displayio/TileGrid.h
Normal file
55
shared-module/displayio/TileGrid.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H
|
||||
#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
mp_obj_t bitmap;
|
||||
mp_obj_t pixel_shader;
|
||||
uint16_t x;
|
||||
uint16_t y;
|
||||
uint16_t width_in_tiles;
|
||||
uint16_t total_width;
|
||||
uint16_t total_height;
|
||||
uint16_t tile_width;
|
||||
uint16_t tile_height;
|
||||
uint8_t* tiles;
|
||||
bool needs_refresh;
|
||||
bool inline_tiles;
|
||||
} displayio_tilegrid_t;
|
||||
|
||||
bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t y, uint16_t *pixel);
|
||||
bool displayio_tilegrid_needs_refresh(displayio_tilegrid_t *self);
|
||||
void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H
|
Loading…
Reference in New Issue
Block a user