2018-08-15 14:01:01 -04:00
|
|
|
/*
|
|
|
|
* 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/Group.h"
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2019-01-24 16:43:39 -05:00
|
|
|
#include "shared-bindings/displayio/TileGrid.h"
|
2018-08-15 14:01:01 -04:00
|
|
|
|
2019-03-12 17:39:46 -04:00
|
|
|
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) {
|
2019-02-15 17:29:59 -05:00
|
|
|
displayio_group_child_t* children = m_new(displayio_group_child_t, max_size);
|
2019-03-12 17:39:46 -04:00
|
|
|
displayio_group_construct(self, children, max_size, scale, x, y);
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
|
2019-02-06 15:13:17 -05:00
|
|
|
uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self) {
|
|
|
|
return self->scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_displayio_group_set_scale(displayio_group_t* self, uint32_t scale) {
|
2019-02-12 17:18:53 -05:00
|
|
|
self->needs_refresh = self->scale != scale;
|
2019-02-06 15:13:17 -05:00
|
|
|
self->scale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_int_t common_hal_displayio_group_get_x(displayio_group_t* self) {
|
|
|
|
return self->x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_displayio_group_set_x(displayio_group_t* self, mp_int_t x) {
|
2019-02-12 17:18:53 -05:00
|
|
|
self->needs_refresh = self->x != x;
|
2019-02-06 15:13:17 -05:00
|
|
|
self->x = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_int_t common_hal_displayio_group_get_y(displayio_group_t* self) {
|
|
|
|
return self->y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_displayio_group_set_y(displayio_group_t* self, mp_int_t y) {
|
2019-02-12 17:18:53 -05:00
|
|
|
self->needs_refresh = self->y != y;
|
2019-02-06 15:13:17 -05:00
|
|
|
self->y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer) {
|
2018-08-15 14:01:01 -04:00
|
|
|
if (self->size == self->max_size) {
|
|
|
|
mp_raise_RuntimeError(translate("Group full"));
|
|
|
|
}
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_obj_t native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
|
|
|
|
if (native_layer == MP_OBJ_NULL) {
|
2019-01-24 16:43:39 -05:00
|
|
|
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."));
|
2019-01-14 20:26:36 -05:00
|
|
|
}
|
2019-02-06 15:13:17 -05:00
|
|
|
// Shift everything right.
|
2019-04-03 12:21:34 -04:00
|
|
|
for (size_t i = self->size; i > index; i--) {
|
|
|
|
self->children[i] = self->children[i - 1];
|
2019-02-06 15:13:17 -05:00
|
|
|
}
|
2019-02-15 17:29:59 -05:00
|
|
|
self->children[index].native = native_layer;
|
|
|
|
self->children[index].original = layer;
|
2018-08-15 14:01:01 -04:00
|
|
|
self->size++;
|
2018-09-11 01:00:21 -04:00
|
|
|
self->needs_refresh = true;
|
|
|
|
}
|
|
|
|
|
2019-02-06 15:13:17 -05:00
|
|
|
mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {
|
2018-09-11 01:00:21 -04:00
|
|
|
self->size--;
|
2019-02-15 17:29:59 -05:00
|
|
|
mp_obj_t item = self->children[index].original;
|
2019-02-06 15:13:17 -05:00
|
|
|
// Shift everything left.
|
|
|
|
for (size_t i = index; i < self->size; i++) {
|
|
|
|
self->children[i] = self->children[i + 1];
|
|
|
|
}
|
2019-02-15 17:29:59 -05:00
|
|
|
self->children[self->size].native = NULL;
|
|
|
|
self->children[self->size].original = NULL;
|
2018-09-11 01:00:21 -04:00
|
|
|
self->needs_refresh = true;
|
|
|
|
return item;
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
|
2019-02-06 15:13:17 -05:00
|
|
|
size_t common_hal_displayio_group_get_len(displayio_group_t* self) {
|
|
|
|
return self->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_displayio_group_get(displayio_group_t* self, size_t index) {
|
2019-02-15 17:29:59 -05:00
|
|
|
return self->children[index].original;
|
2019-02-06 15:13:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_displayio_group_set(displayio_group_t* self, size_t index, mp_obj_t layer) {
|
2019-02-15 17:29:59 -05:00
|
|
|
mp_obj_t native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
|
|
|
|
if (native_layer == MP_OBJ_NULL) {
|
|
|
|
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[index].native = native_layer;
|
|
|
|
self->children[index].original = layer;
|
2019-02-06 15:13:17 -05:00
|
|
|
self->needs_refresh = true;
|
|
|
|
}
|
|
|
|
|
2019-03-12 17:39:46 -04:00
|
|
|
void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* child_array, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) {
|
|
|
|
self->x = x;
|
|
|
|
self->y = y;
|
2018-08-15 14:01:01 -04:00
|
|
|
self->children = child_array;
|
|
|
|
self->max_size = max_size;
|
2018-09-13 13:42:12 -04:00
|
|
|
self->needs_refresh = false;
|
2019-02-06 15:13:17 -05:00
|
|
|
self->scale = scale;
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, uint16_t* pixel) {
|
|
|
|
x -= self->x;
|
|
|
|
y -= self->y;
|
2019-05-09 14:38:30 -04:00
|
|
|
// When we are scaled we need to substract all but one to ensure -scale to 0 divide down to -1.
|
|
|
|
// Normally -scale to scale both divide down to 0 because 0 is unsigned.
|
|
|
|
if (x < 0) {
|
|
|
|
x -= self->scale - 1;
|
|
|
|
}
|
|
|
|
if (y < 0) {
|
|
|
|
y -= self->scale - 1;
|
|
|
|
}
|
2019-01-17 13:57:05 -05:00
|
|
|
x /= self->scale;
|
|
|
|
y /= self->scale;
|
2018-08-15 14:01:01 -04:00
|
|
|
for (int32_t i = self->size - 1; i >= 0 ; i--) {
|
2019-02-15 17:29:59 -05:00
|
|
|
mp_obj_t layer = self->children[i].native;
|
2019-01-24 16:43:39 -05:00
|
|
|
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
|
|
|
|
if (displayio_tilegrid_get_pixel(layer, x, y, pixel)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-10 20:22:08 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
|
|
|
|
if (displayio_group_get_pixel(layer, x, y, pixel)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool displayio_group_needs_refresh(displayio_group_t *self) {
|
2018-09-11 01:00:21 -04:00
|
|
|
if (self->needs_refresh) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-15 14:01:01 -04:00
|
|
|
for (int32_t i = self->size - 1; i >= 0 ; i--) {
|
2019-02-15 17:29:59 -05:00
|
|
|
mp_obj_t layer = self->children[i].native;
|
2019-01-25 19:59:18 -05:00
|
|
|
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
|
|
|
|
if (displayio_tilegrid_needs_refresh(layer)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
|
|
|
|
if (displayio_group_needs_refresh(layer)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void displayio_group_finish_refresh(displayio_group_t *self) {
|
2018-09-11 01:00:21 -04:00
|
|
|
self->needs_refresh = false;
|
2018-08-15 14:01:01 -04:00
|
|
|
for (int32_t i = self->size - 1; i >= 0 ; i--) {
|
2019-02-15 17:29:59 -05:00
|
|
|
mp_obj_t layer = self->children[i].native;
|
2019-01-25 19:59:18 -05:00
|
|
|
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
|
|
|
|
displayio_tilegrid_finish_refresh(layer);
|
|
|
|
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
|
|
|
|
displayio_group_finish_refresh(layer);
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|