Add support for rendering a shape.

Fixes #1171
This commit is contained in:
Scott Shawcroft 2019-01-12 00:44:34 -08:00
parent 738e8f0028
commit a14762a16c
No known key found for this signature in database
GPG Key ID: FD0EDC4B6C53CA59
9 changed files with 307 additions and 1 deletions

View File

@ -382,6 +382,7 @@ SRC_SHARED_MODULE = \
displayio/Group.c \
displayio/OnDiskBitmap.c \
displayio/Palette.c \
displayio/Shape.c \
displayio/Sprite.c \
gamepad/__init__.c \
gamepad/GamePad.c \

View File

@ -70,7 +70,7 @@ bool common_hal_displayio_fourwire_begin_transaction(displayio_fourwire_obj_t* s
return false;
}
// TODO(tannewt): Stop hardcoding SPI frequency, polarity and phase.
common_hal_busio_spi_configure(&self->bus, 12000000, 0, 0, 8);
common_hal_busio_spi_configure(&self->bus, 48000000, 0, 0, 8);
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
return true;
}

View File

@ -0,0 +1,117 @@
/*
* 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.
*/
#include "shared-bindings/displayio/Shape.h"
#include <stdint.h>
#include "py/binary.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/util.h"
#include "supervisor/shared/translate.h"
//| .. currentmodule:: displayio
//|
//| :class:`Shape` -- Represents a shape by defining its bounds on each row
//| ==========================================================================
//|
//| Represents any shape made by defining boundaries that may be mirrored.
//|
//| .. warning:: This will likely be changed before 4.0.0. Consider it very experimental.
//|
//| .. class:: Shape(width, height, *, mirror_x=False, mirrored_y=False)
//|
//| Create a Shape object with the given fixed size. Each pixel is one bit and is stored by the
//| column boundaries of the shape on each row. Each row's boundary defaults to the full row.
//|
//| :param int width: The number of pixels wide
//| :param int height: The number of pixels high
//| :param bool mirror_x: When true the left boundary is mirrored to the right.
//| :param bool mirror_y: When true the top boundary is mirrored to the bottom.
//|
STATIC mp_obj_t displayio_shape_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
mp_arg_check_num(n_args, n_kw, 2, 4, true);
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
enum { ARG_width, ARG_height, ARG_mirror_x, ARG_mirror_y };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_mirror_x, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
{ MP_QSTR_mirror_y, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
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);
displayio_shape_t *self = m_new_obj(displayio_shape_t);
self->base.type = &displayio_shape_type;
common_hal_displayio_shape_construct(self,
args[ARG_width].u_int,
args[ARG_height].u_int,
args[ARG_mirror_x].u_bool,
args[ARG_mirror_y].u_bool);
return MP_OBJ_FROM_PTR(self);
}
//| .. method:: set_boundary(y, start_x, end_x)
//|
//| Loads pre-packed data into the given row.
//|
STATIC mp_obj_t displayio_shape_obj_set_boundary(size_t n_args, const mp_obj_t *args) {
(void) n_args;
displayio_shape_t *self = MP_OBJ_TO_PTR(args[0]);
mp_int_t y;
if (!mp_obj_get_int_maybe(args[1], &y)) {
mp_raise_ValueError(translate("y should be an int"));
}
mp_int_t start_x;
if (!mp_obj_get_int_maybe(args[2], &start_x)) {
mp_raise_ValueError(translate("start_x should be an int"));
}
mp_int_t end_x;
if (!mp_obj_get_int_maybe(args[3], &end_x)) {
mp_raise_ValueError(translate("end_x should be an int"));
}
common_hal_displayio_shape_set_boundary(self, y, start_x, end_x);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(displayio_shape_set_boundary_obj, 4, 4, displayio_shape_obj_set_boundary);
STATIC const mp_rom_map_elem_t displayio_shape_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_set_boundary), MP_ROM_PTR(&displayio_shape_set_boundary_obj) },
};
STATIC MP_DEFINE_CONST_DICT(displayio_shape_locals_dict, displayio_shape_locals_dict_table);
const mp_obj_type_t displayio_shape_type = {
{ &mp_type_type },
.name = MP_QSTR_Shape,
.make_new = displayio_shape_make_new,
.locals_dict = (mp_obj_dict_t*)&displayio_shape_locals_dict,
};

View File

@ -0,0 +1,41 @@
/*
* 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_SHAPE_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_SHAPE_H
#include "shared-module/displayio/Shape.h"
extern const mp_obj_type_t displayio_shape_type;
void common_hal_displayio_shape_construct(displayio_shape_t *self, uint32_t width,
uint32_t height, bool mirror_x, bool mirror_y);
void common_hal_displayio_shape_set_boundary(displayio_shape_t *self, uint16_t y, uint16_t start_x,
uint16_t end_x);
uint32_t common_hal_displayio_shape_get_pixel(void *shape, int16_t x, int16_t y);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_SHAPE_H

View File

@ -36,6 +36,7 @@
#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"
void unpack_position(mp_obj_t position_obj, int16_t* x, int16_t* y) {
@ -93,6 +94,10 @@ STATIC mp_obj_t displayio_sprite_make_new(const mp_obj_type_t *type, size_t n_ar
displayio_ondiskbitmap_t* bmp = MP_OBJ_TO_PTR(bitmap);
width = bmp->width;
height = bmp->height;
} else if (MP_OBJ_IS_TYPE(bitmap, &displayio_shape_type)) {
displayio_shape_t* bmp = MP_OBJ_TO_PTR(bitmap);
width = bmp->width;
height = bmp->height;
} else {
mp_raise_TypeError(translate("unsupported bitmap type"));
}

View File

@ -36,6 +36,7 @@
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/displayio/OnDiskBitmap.h"
#include "shared-bindings/displayio/Palette.h"
#include "shared-bindings/displayio/Shape.h"
#include "shared-bindings/displayio/Sprite.h"
//| :mod:`displayio` --- Native display driving
@ -64,6 +65,7 @@
//| Group
//| OnDiskBitmap
//| Palette
//| Shape
//| Sprite
//|
//| All libraries change hardware state but are never deinit
@ -76,6 +78,7 @@ STATIC const mp_rom_map_elem_t displayio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_Group), MP_ROM_PTR(&displayio_group_type) },
{ MP_ROM_QSTR(MP_QSTR_OnDiskBitmap), MP_ROM_PTR(&displayio_ondiskbitmap_type) },
{ 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_FourWire), MP_ROM_PTR(&displayio_fourwire_type) },

View File

@ -0,0 +1,90 @@
/*
* 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.
*/
#include "shared-bindings/displayio/Shape.h"
#include <string.h>
#include "py/runtime.h"
void common_hal_displayio_shape_construct(displayio_shape_t *self, uint32_t width,
uint32_t height, bool mirror_x, bool mirror_y) {
self->mirror_x = mirror_x;
self->mirror_y = mirror_y;
self->width = width;
if (self->mirror_x) {
width /= 2;
width += self->width % 2 - 1;
}
self->half_width = width;
self->height = height;
if (self->mirror_y) {
height /= 2;
height += self->height % 2 - 1;
}
self->half_height = height;
self->data = m_malloc(height * sizeof(uint32_t), false);
for (uint16_t i = 0; i < height; i++) {
self->data[2 * i] = 0;
self->data[2 * i + 1] = width;
}
}
void common_hal_displayio_shape_set_boundary(displayio_shape_t *self, uint16_t y, uint16_t start_x, uint16_t end_x) {
if (y < 0 || y >= self->height || (self->mirror_y && y > self->half_height)) {
mp_raise_ValueError(translate("y value out of bounds"));
}
if (start_x < 0 || start_x > self->width || end_x < 0 || end_x > self->height) {
mp_raise_ValueError(translate("x value out of bounds"));
}
uint16_t half_width = self->width / 2 - 1 + self->width % 2;
if (self->mirror_x && (start_x > half_width || end_x > half_width)) {
mp_raise_ValueError_varg(translate("Maximum x value when mirrored is %d"), half_width);
}
self->data[2 * y] = start_x;
self->data[2 * y + 1] = end_x;
}
uint32_t common_hal_displayio_shape_get_pixel(void *obj, int16_t x, int16_t y) {
displayio_shape_t *self = obj;
if (x >= self->width || x < 0 || y >= self->height || y < 0) {
return 0;
}
if (self->mirror_x && x > self->half_width) {
x = self->width - 1 - x;
}
if (self->mirror_y && y > self->half_height) {
y = self->height - y - 1;
}
uint16_t start_x = self->data[2 * y];
uint16_t end_x = self->data[2 * y + 1];
if (x < start_x || x > end_x) {
return 0;
}
return 1;
}

View File

@ -0,0 +1,46 @@
/*
* 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_SHAPE_H
#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H
#include <stdbool.h>
#include <stdint.h>
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
uint16_t width;
uint16_t height;
uint16_t half_width;
uint16_t half_height;
uint16_t* data;
bool mirror_x;
bool mirror_y;
} displayio_shape_t;
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H

View File

@ -30,6 +30,7 @@
#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_sprite_construct(displayio_sprite_t *self, mp_obj_t bitmap,
mp_obj_t pixel_shader, uint16_t width, uint16_t height, uint16_t x, uint16_t y) {
@ -71,6 +72,8 @@ bool displayio_sprite_get_pixel(displayio_sprite_t *self, int16_t x, int16_t y,
uint32_t value = 0;
if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
value = common_hal_displayio_bitmap_get_pixel(self->bitmap, x, y);
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) {
value = common_hal_displayio_shape_get_pixel(self->bitmap, x, y);
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, x, y);
}