diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2019-02-06 12:13:17 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2019-02-11 20:55:05 -0800 |
| commit | c17f147be95e7490e5207c747add2da1cc8b167f (patch) | |
| tree | 90abe0fad90920415ae4f1e118854cf56e8dfe04 /shared-bindings/displayio | |
| parent | 1a6ad209439e0c8b0d01701b76195f9761949e31 (diff) | |
A variety of displayio improvements
This changes a number of things in displayio:
* Introduces BuiltinFont and Glyph so the built in font can be used by libraries. For boards with
a font it is available as board.TERMINAL_FONT. Fixes #1172
* Remove _load_row from Bitmap in favor of bitmap[] access. Index can be x/y tuple or overall index. Fixes #1191
* Add width and height properties to Bitmap.
* Add insert and [] access to Group. Fixes #1518
* Add index param to pop on Group.
* Terminal no longer takes unicode character info. It takes a BuiltinFont instead.
* Fix Terminal's handling of [###D vt100 commands used when up arrowing into repl history.
* Add x and y positions to Group plus scale as well.
* Add bitmap accessor for BuiltinFont
Diffstat (limited to 'shared-bindings/displayio')
| -rw-r--r-- | shared-bindings/displayio/Bitmap.c | 111 | ||||
| -rw-r--r-- | shared-bindings/displayio/Bitmap.h | 4 | ||||
| -rw-r--r-- | shared-bindings/displayio/BuiltinFont.c | 110 | ||||
| -rw-r--r-- | shared-bindings/displayio/BuiltinFont.h | 38 | ||||
| -rw-r--r-- | shared-bindings/displayio/ColorConverter.c | 2 | ||||
| -rw-r--r-- | shared-bindings/displayio/Display.c | 2 | ||||
| -rw-r--r-- | shared-bindings/displayio/FourWire.c | 2 | ||||
| -rw-r--r-- | shared-bindings/displayio/Glyph.c | 75 | ||||
| -rw-r--r-- | shared-bindings/displayio/Glyph.h | 34 | ||||
| -rw-r--r-- | shared-bindings/displayio/Group.c | 205 | ||||
| -rw-r--r-- | shared-bindings/displayio/Group.h | 14 | ||||
| -rw-r--r-- | shared-bindings/displayio/OnDiskBitmap.c | 2 | ||||
| -rw-r--r-- | shared-bindings/displayio/Palette.c | 2 | ||||
| -rw-r--r-- | shared-bindings/displayio/ParallelBus.c | 2 | ||||
| -rw-r--r-- | shared-bindings/displayio/TileGrid.c | 2 | ||||
| -rw-r--r-- | shared-bindings/displayio/__init__.c | 8 |
16 files changed, 564 insertions, 49 deletions
diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index cee3998dc..3612b8d63 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -43,8 +43,6 @@ //| //| Stores values of a certain size in a 2D array //| -//| .. warning:: This will likely be changed before 4.0.0. Consider it very experimental. -//| //| .. class:: Bitmap(width, height, value_count) //| //| Create a Bitmap object with the given fixed size. Each pixel stores a value that is used to @@ -71,34 +69,110 @@ STATIC mp_obj_t displayio_bitmap_make_new(const mp_obj_type_t *type, size_t n_ar return MP_OBJ_FROM_PTR(self); } +//| .. attribute:: width +//| +//| Width of the bitmap. (read only) +//| +STATIC mp_obj_t displayio_bitmap_obj_get_width(mp_obj_t self_in) { + displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in); + + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_bitmap_get_width(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_bitmap_get_width_obj, displayio_bitmap_obj_get_width); + +const mp_obj_property_t displayio_bitmap_width_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_bitmap_get_width_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; -//| .. method:: _load_row(y, data) +//| .. attribute:: height //| -//| Loads pre-packed data into the given row. +//| Height of the bitmap. (read only) //| -STATIC mp_obj_t displayio_bitmap_obj__load_row(mp_obj_t self_in, mp_obj_t y_in, mp_obj_t data_in) { +STATIC mp_obj_t displayio_bitmap_obj_get_height(mp_obj_t self_in) { displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in); - mp_int_t y; - if (!mp_obj_get_int_maybe(y_in, &y)) { - mp_raise_ValueError(translate("y should be an int")); + + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_bitmap_get_height(self)); +} + +MP_DEFINE_CONST_FUN_OBJ_1(displayio_bitmap_get_height_obj, displayio_bitmap_obj_get_height); + +const mp_obj_property_t displayio_bitmap_height_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_bitmap_get_height_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. method:: __getitem__(index) +//| +//| Returns the value at 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(bitmap[0,1]) +//| +//| .. method:: __setitem__(index, value) +//| +//| Sets the value at the given index. The index can either be an x,y tuple or an int equal +//| to ``y * width + x``. +//| +//| This allows you to:: +//| +//| bitmap[0,1] = 3 +//| +STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value_obj) { + if (value_obj == mp_const_none) { + // delete item + mp_raise_AttributeError(translate("Cannot delete values")); + return mp_const_none; } - mp_buffer_info_t bufinfo; - if (mp_get_buffer(data_in, &bufinfo, MP_BUFFER_READ)) { - if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { - mp_raise_ValueError(translate("row buffer must be a bytearray or array of type 'b' or 'B'")); + + displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in); + + if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { + // TODO(tannewt): Implement subscr after slices support start, stop and step tuples. + mp_raise_NotImplementedError(translate("Slices not supported")); + return mp_const_none; + } + + 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_displayio_bitmap_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_displayio_bitmap_get_width(self) || y >= common_hal_displayio_bitmap_get_height(self)) { + mp_raise_IndexError(translate("pixel coordinates out of bounds")); } - uint8_t* buf = bufinfo.buf; - common_hal_displayio_bitmap_load_row(self, y, buf, bufinfo.len); + } + + if (value_obj == MP_OBJ_SENTINEL) { + // load + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_bitmap_get_pixel(self, x, y)); } else { - mp_raise_TypeError(translate("row data must be a buffer")); + mp_int_t value = mp_obj_get_int(value_obj); + if (value >= 1 << common_hal_displayio_bitmap_get_bits_per_value(self)) { + mp_raise_ValueError(translate("pixel value requires too many bits")); + } + common_hal_displayio_bitmap_set_pixel(self, x, y, value); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_3(displayio_bitmap__load_row_obj, displayio_bitmap_obj__load_row); STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR__load_row), MP_ROM_PTR(&displayio_bitmap__load_row_obj) }, + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) }, + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) }, }; STATIC MP_DEFINE_CONST_DICT(displayio_bitmap_locals_dict, displayio_bitmap_locals_dict_table); @@ -106,7 +180,6 @@ const mp_obj_type_t displayio_bitmap_type = { { &mp_type_type }, .name = MP_QSTR_Bitmap, .make_new = displayio_bitmap_make_new, - // TODO(tannewt): Implement subscr after slices support start, stop and step tuples. - // .subscr = displayio_bitmap_subscr, + .subscr = bitmap_subscr, .locals_dict = (mp_obj_dict_t*)&displayio_bitmap_locals_dict, }; diff --git a/shared-bindings/displayio/Bitmap.h b/shared-bindings/displayio/Bitmap.h index b736f9459..90694951f 100644 --- a/shared-bindings/displayio/Bitmap.h +++ b/shared-bindings/displayio/Bitmap.h @@ -36,6 +36,10 @@ void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t wi void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t* data, uint16_t len); +uint16_t common_hal_displayio_bitmap_get_height(displayio_bitmap_t *self); +uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self); +uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self); +void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value); uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BITMAP_H diff --git a/shared-bindings/displayio/BuiltinFont.c b/shared-bindings/displayio/BuiltinFont.c new file mode 100644 index 000000000..7ad85b5d2 --- /dev/null +++ b/shared-bindings/displayio/BuiltinFont.c @@ -0,0 +1,110 @@ +/* + * 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/BuiltinFont.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/microcontroller/Pin.h" +#include "shared-bindings/util.h" +#include "supervisor/shared/translate.h" + +//| .. currentmodule:: displayio +//| +//| :class:`BuiltinFont` -- A font built into CircuitPython +//| ========================================================================================= +//| +//| A font built into CircuitPython. +//| +//| .. class:: BuiltinFont() +//| +//| Creation not supported. Available fonts are defined when CircuitPython is built. See the +//| `Adafruit_CircuitPython_Bitmap_Font <https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font>`_ +//| library for dynamically loaded fonts. +//| + +//| .. attribute:: bitmap +//| +//| Bitmap containing all font glyphs starting with ASCII and followed by unicode. Use +//| `get_glyph` in most cases. This is useful for use with `displayio.TileGrid` and +//| `displayio.Terminal`. +//| +STATIC mp_obj_t displayio_builtinfont_obj_get_bitmap(mp_obj_t self_in) { + displayio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); + return common_hal_displayio_builtinfont_get_bitmap(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_builtinfont_get_bitmap_obj, displayio_builtinfont_obj_get_bitmap); + +const mp_obj_property_t displayio_builtinfont_bitmap_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_builtinfont_get_bitmap_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. method:: get_bounding_box() +//| +//| Returns the maximum bounds of all glyphs in the font in a tuple of two values: width, height. +//| +STATIC mp_obj_t displayio_builtinfont_obj_get_bounding_box(mp_obj_t self_in) { + displayio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); + + return common_hal_displayio_builtinfont_get_bounding_box(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_builtinfont_get_bounding_box_obj, displayio_builtinfont_obj_get_bounding_box); + + +//| .. method:: get_glyph(codepoint) +//| +//| Returns a `displayio.Glyph` for the given codepoint or None if no glyph is available. +//| +STATIC mp_obj_t displayio_builtinfont_obj_get_glyph(mp_obj_t self_in, mp_obj_t codepoint_obj) { + displayio_builtinfont_t *self = MP_OBJ_TO_PTR(self_in); + + mp_int_t codepoint; + if (!mp_obj_get_int_maybe(codepoint_obj, &codepoint)) { + mp_raise_ValueError_varg(translate("%q should be an int"), MP_QSTR_codepoint); + } + return common_hal_displayio_builtinfont_get_glyph(self, codepoint); +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_builtinfont_get_glyph_obj, displayio_builtinfont_obj_get_glyph); + +STATIC const mp_rom_map_elem_t displayio_builtinfont_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_builtinfont_bitmap_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_bounding_box), MP_ROM_PTR(&displayio_builtinfont_get_bounding_box_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_glyph), MP_ROM_PTR(&displayio_builtinfont_get_glyph_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(displayio_builtinfont_locals_dict, displayio_builtinfont_locals_dict_table); + +const mp_obj_type_t displayio_builtinfont_type = { + { &mp_type_type }, + .name = MP_QSTR_BuiltinFont, + .locals_dict = (mp_obj_dict_t*)&displayio_builtinfont_locals_dict, +}; diff --git a/shared-bindings/displayio/BuiltinFont.h b/shared-bindings/displayio/BuiltinFont.h new file mode 100644 index 000000000..766013158 --- /dev/null +++ b/shared-bindings/displayio/BuiltinFont.h @@ -0,0 +1,38 @@ +/* + * 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_BUILTINFONT_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BUILTINFONT_H + +#include "shared-module/displayio/BuiltinFont.h" + +extern const mp_obj_type_t displayio_builtinfont_type; + +mp_obj_t common_hal_displayio_builtinfont_get_bitmap(const displayio_builtinfont_t *self); +mp_obj_t common_hal_displayio_builtinfont_get_bounding_box(const displayio_builtinfont_t *self); +mp_obj_t common_hal_displayio_builtinfont_get_glyph(const displayio_builtinfont_t *self, mp_uint_t codepoint); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BUILTINFONT_H diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c index f146784e9..561883810 100644 --- a/shared-bindings/displayio/ColorConverter.c +++ b/shared-bindings/displayio/ColorConverter.c @@ -43,8 +43,6 @@ //| //| Converts one color format to another. //| -//| .. warning:: This will be changed before 4.0.0. Consider it very experimental. -//| //| .. class:: ColorConverter() //| //| Create a ColorConverter object to convert color formats. Only supports RGB888 to RGB565 diff --git a/shared-bindings/displayio/Display.c b/shared-bindings/displayio/Display.c index adc121bc5..ca72e407a 100644 --- a/shared-bindings/displayio/Display.c +++ b/shared-bindings/displayio/Display.c @@ -50,8 +50,6 @@ //| Most people should not use this class directly. Use a specific display driver instead that will //| contain the initialization sequence at minimum. //| -//| .. warning:: This will be changed before 4.0.0. Consider it very experimental. -//| //| .. class:: Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0, color_depth=16, set_column_command=0x2a, set_row_command=0x2b, write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None) //| //| Create a Display object on the given display bus (`displayio.FourWire` or `displayio.ParallelBus`). diff --git a/shared-bindings/displayio/FourWire.c b/shared-bindings/displayio/FourWire.c index dee12eaf6..56baa534b 100644 --- a/shared-bindings/displayio/FourWire.c +++ b/shared-bindings/displayio/FourWire.c @@ -46,8 +46,6 @@ //| Manage updating a display over SPI four wire protocol in the background while Python code runs. //| It doesn't handle display initialization. //| -//| .. warning:: This will be changed before 4.0.0. Consider it very experimental. -//| //| .. class:: FourWire(spi_bus, *, command, chip_select, reset) //| //| Create a FourWire object associated with the given pins. diff --git a/shared-bindings/displayio/Glyph.c b/shared-bindings/displayio/Glyph.c new file mode 100644 index 000000000..1a4a75522 --- /dev/null +++ b/shared-bindings/displayio/Glyph.c @@ -0,0 +1,75 @@ +/* + * 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/Glyph.h" + +#include <stdint.h> + +//| .. currentmodule:: displayio +//| +//| :class:`Glyph` -- Storage of glyph info +//| ========================================================================== +//| +//| .. class:: Glyph(bitmap, tile_index, width, height, dx, dy, shift_x, shift_y) +//| +//| Named tuple used to capture a single glyph and its attributes. +//| +//| :param displayio.Bitmap bitmap: the bitmap including the glyph +//| :param int tile_index: the tile index within the bitmap +//| :param int width: the width of the glyph's bitmap +//| :param int height: the height of the glyph's bitmap +//| :param int dx: x adjustment to the bitmap's position +//| :param int dy: y adjustment to the bitmap's position +//| :param int shift_x: the x difference to the next glyph +//| :param int shift_y: the y difference to the next glyph +//| +const mp_obj_namedtuple_type_t displayio_glyph_type = { + .base = { + .base = { + .type = &mp_type_type + }, + .name = MP_QSTR_Glyph, + .print = namedtuple_print, + .make_new = namedtuple_make_new, + .unary_op = mp_obj_tuple_unary_op, + .binary_op = mp_obj_tuple_binary_op, + .attr = namedtuple_attr, + .subscr = mp_obj_tuple_subscr, + .getiter = mp_obj_tuple_getiter, + .parent = &mp_type_tuple, + }, + .n_fields = 8, + .fields = { + MP_QSTR_bitmap, + MP_QSTR_tile_index, + MP_QSTR_width, + MP_QSTR_height, + MP_QSTR_dx, + MP_QSTR_dy, + MP_QSTR_shift_x, + MP_QSTR_shift_y + }, +}; diff --git a/shared-bindings/displayio/Glyph.h b/shared-bindings/displayio/Glyph.h new file mode 100644 index 000000000..d26a4b9e4 --- /dev/null +++ b/shared-bindings/displayio/Glyph.h @@ -0,0 +1,34 @@ +/* + * 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_GLYPH_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_GLYPH_H + +#include "py/objnamedtuple.h" + +extern const mp_obj_namedtuple_type_t displayio_glyph_type; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_GLYPH_H diff --git a/shared-bindings/displayio/Group.c b/shared-bindings/displayio/Group.c index 253812e00..f8013050d 100644 --- a/shared-bindings/displayio/Group.c +++ b/shared-bindings/displayio/Group.c @@ -41,57 +41,236 @@ //| //| Manage a group of sprites and groups and how they are inter-related. //| -//| .. warning:: This will be changed before 4.0.0. Consider it very experimental. -//| -//| .. class:: Group(*, max_size=4) +//| .. class:: Group(*, max_size=4, scale=1) //| //| Create a Group of a given size. //| //| :param int max_size: The maximum group size. //| STATIC mp_obj_t displayio_group_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_max_size }; + enum { ARG_max_size, ARG_scale }; static const mp_arg_t allowed_args[] = { { MP_QSTR_max_size, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 4} }, + { MP_QSTR_scale, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} }, }; 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_int_t max_size = args[ARG_max_size].u_int; if (max_size < 1) { - mp_raise_ValueError(translate("Group must have size at least 1")); + mp_raise_ValueError_varg(translate("Group must have %q at least 1"), MP_QSTR_max_size); + } + + mp_int_t scale = args[ARG_scale].u_int; + if (scale < 1) { + mp_raise_ValueError_varg(translate("Group must have %q at least 1"), MP_QSTR_scale); } displayio_group_t *self = m_new_obj(displayio_group_t); self->base.type = &displayio_group_type; - common_hal_displayio_group_construct(self, max_size); + common_hal_displayio_group_construct(self, max_size, scale); return MP_OBJ_FROM_PTR(self); } +// Helper to ensure we have the native super class instead of a subclass. +static displayio_group_t* native_group(mp_obj_t group_obj) { + mp_obj_t native_group = mp_instance_cast_to_native_base(group_obj, &displayio_group_type); + assert(native_group == MP_OBJ_NULL); + return MP_OBJ_TO_PTR(native_group); +} + +//| .. attribute:: scale +//| +//| Scales each pixel within the Group in both directions. For example, when scale=2 each pixel +//| will be represented by 2x2 pixels. +//| +STATIC mp_obj_t displayio_group_obj_get_scale(mp_obj_t self_in) { + displayio_group_t *self = native_group(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_scale(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_get_scale_obj, displayio_group_obj_get_scale); + +STATIC mp_obj_t displayio_group_obj_set_scale(mp_obj_t self_in, mp_obj_t scale_obj) { + displayio_group_t *self = native_group(self_in); + + mp_int_t scale = mp_obj_get_int(scale_obj); + if (scale < 1) { + mp_raise_ValueError_varg(translate("Group must have %q at least 1"), MP_QSTR_scale); + } + common_hal_displayio_group_set_scale(self, scale); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_set_scale_obj, displayio_group_obj_set_scale); + +const mp_obj_property_t displayio_group_scale_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_group_get_scale_obj, + (mp_obj_t)&displayio_group_set_scale_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: x +//| +//| X position of the Group in the parent. +//| +STATIC mp_obj_t displayio_group_obj_get_x(mp_obj_t self_in) { + displayio_group_t *self = native_group(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_x(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_get_x_obj, displayio_group_obj_get_x); + +STATIC mp_obj_t displayio_group_obj_set_x(mp_obj_t self_in, mp_obj_t x_obj) { + displayio_group_t *self = native_group(self_in); + + mp_int_t x = mp_obj_get_int(x_obj); + common_hal_displayio_group_set_x(self, x); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_set_x_obj, displayio_group_obj_set_x); + +const mp_obj_property_t displayio_group_x_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_group_get_x_obj, + (mp_obj_t)&displayio_group_set_x_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: y +//| +//| Y position of the Group in the parent. +//| +STATIC mp_obj_t displayio_group_obj_get_y(mp_obj_t self_in) { + displayio_group_t *self = native_group(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_y(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_get_y_obj, displayio_group_obj_get_y); + +STATIC mp_obj_t displayio_group_obj_set_y(mp_obj_t self_in, mp_obj_t y_obj) { + displayio_group_t *self = native_group(self_in); + + mp_int_t y = mp_obj_get_int(y_obj); + common_hal_displayio_group_set_y(self, y); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_set_y_obj, displayio_group_obj_set_y); + +const mp_obj_property_t displayio_group_y_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_group_get_y_obj, + (mp_obj_t)&displayio_group_set_y_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + //| .. method:: append(layer) //| //| Append a layer to the group. It will be drawn above other layers. //| STATIC mp_obj_t displayio_group_obj_append(mp_obj_t self_in, mp_obj_t layer) { displayio_group_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_displayio_group_append(self, layer); + common_hal_displayio_group_insert(self, common_hal_displayio_group_get_len(self), layer); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_append_obj, displayio_group_obj_append); -//| .. method:: pop() +//| .. method:: insert(index, layer) //| -//| Remove the last item and return it. +//| Insert a layer into the group. //| -STATIC mp_obj_t displayio_group_obj_pop(mp_obj_t self_in) { +STATIC mp_obj_t displayio_group_obj_insert(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t layer) { displayio_group_t *self = MP_OBJ_TO_PTR(self_in); - return common_hal_displayio_group_pop(self); + size_t index = mp_get_index(&displayio_group_type, common_hal_displayio_group_get_len(self), index_obj, false); + common_hal_displayio_group_insert(self, index, layer); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_3(displayio_group_insert_obj, displayio_group_obj_insert); + +//| .. method:: pop(i=-1) +//| +//| Remove the ith item and return it. +//| +STATIC mp_obj_t displayio_group_obj_pop(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_i }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_i, MP_ARG_INT, {.u_int = -1} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + displayio_group_t *self = MP_OBJ_TO_PTR(pos_args[0]); + + size_t index = mp_get_index(&displayio_group_type, + common_hal_displayio_group_get_len(self), + MP_OBJ_NEW_SMALL_INT(args[ARG_i].u_int), + false); + return common_hal_displayio_group_pop(self, index); +} +MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_pop_obj, 1, displayio_group_obj_pop); + +//| .. method:: __len__() +//| +//| Returns the number of layers in a Group +//| +STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) { + displayio_group_t *self = MP_OBJ_TO_PTR(self_in); + uint16_t len = common_hal_displayio_group_get_len(self); + switch (op) { + case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); + case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len); + default: return MP_OBJ_NULL; // op not supported + } +} + +//| .. method:: __getitem__(index) +//| +//| Returns the value at the given index. +//| +//| This allows you to:: +//| +//| print(group[0]) +//| +//| .. method:: __setitem__(index, value) +//| +//| Sets the value at the given index. +//| +//| This allows you to:: +//| +//| group[0] = sprite +//| +//| .. method:: __delitem__(index) +//| +//| Deletes the value at the given index. +//| +//| This allows you to:: +//| +//| del group[0] +//| +STATIC mp_obj_t group_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) { + displayio_group_t *self = MP_OBJ_TO_PTR(self_in); + + if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { + mp_raise_NotImplementedError(translate("Slices not supported")); + } else { + size_t index = mp_get_index(&displayio_group_type, common_hal_displayio_group_get_len(self), index_obj, false); + + if (value == MP_OBJ_SENTINEL) { + // load + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get(self, index)); + } else if (value == mp_const_none) { + common_hal_displayio_group_pop(self, index); + } else { + common_hal_displayio_group_set(self, index, value); + } + } + return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_pop_obj, displayio_group_obj_pop); STATIC const mp_rom_map_elem_t displayio_group_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&displayio_group_scale_obj) }, + { MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&displayio_group_x_obj) }, + { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&displayio_group_y_obj) }, { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&displayio_group_append_obj) }, + { MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&displayio_group_insert_obj) }, { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&displayio_group_pop_obj) }, }; STATIC MP_DEFINE_CONST_DICT(displayio_group_locals_dict, displayio_group_locals_dict_table); @@ -100,5 +279,7 @@ const mp_obj_type_t displayio_group_type = { { &mp_type_type }, .name = MP_QSTR_Group, .make_new = displayio_group_make_new, + .subscr = group_subscr, + .unary_op = group_unary_op, .locals_dict = (mp_obj_dict_t*)&displayio_group_locals_dict, }; diff --git a/shared-bindings/displayio/Group.h b/shared-bindings/displayio/Group.h index a85098e6d..d326dbf4d 100644 --- a/shared-bindings/displayio/Group.h +++ b/shared-bindings/displayio/Group.h @@ -32,8 +32,18 @@ extern const mp_obj_type_t displayio_group_type; -void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size); +void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale); +uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self); +void common_hal_displayio_group_set_scale(displayio_group_t* self, uint32_t scale); +mp_int_t common_hal_displayio_group_get_x(displayio_group_t* self); +void common_hal_displayio_group_set_x(displayio_group_t* self, mp_int_t x); +mp_int_t common_hal_displayio_group_get_y(displayio_group_t* self); +void common_hal_displayio_group_set_y(displayio_group_t* self, mp_int_t y); void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer); -mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self); +void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer); +size_t common_hal_displayio_group_get_len(displayio_group_t* self); +mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index); +mp_obj_t common_hal_displayio_group_get(displayio_group_t* self, size_t index); +void common_hal_displayio_group_set(displayio_group_t* self, size_t index, mp_obj_t layer); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_GROUP_H diff --git a/shared-bindings/displayio/OnDiskBitmap.c b/shared-bindings/displayio/OnDiskBitmap.c index d6d3b9862..8b75eb00c 100644 --- a/shared-bindings/displayio/OnDiskBitmap.c +++ b/shared-bindings/displayio/OnDiskBitmap.c @@ -42,8 +42,6 @@ //| much slower pixel load times. These load times may result in frame tearing where only part of //| the image is visible. //| -//| .. warning:: This will likely be changed before 4.0.0. Consider it very experimental. -//| //| It's easiest to use on a board with a built in display such as the `Hallowing M0 Express //| <https://www.adafruit.com/product/3900>`_. //| diff --git a/shared-bindings/displayio/Palette.c b/shared-bindings/displayio/Palette.c index c412674e1..799d42bef 100644 --- a/shared-bindings/displayio/Palette.c +++ b/shared-bindings/displayio/Palette.c @@ -44,8 +44,6 @@ //| Map a pixel palette_index to a full color. Colors are transformed to the display's format internally to //| save memory. //| -//| .. warning:: This will be changed before 4.0.0. Consider it very experimental. -//| //| .. class:: Palette(color_count) //| //| Create a Palette object to store a set number of colors. diff --git a/shared-bindings/displayio/ParallelBus.c b/shared-bindings/displayio/ParallelBus.c index abae114e2..a54a84471 100644 --- a/shared-bindings/displayio/ParallelBus.c +++ b/shared-bindings/displayio/ParallelBus.c @@ -45,8 +45,6 @@ //| Manage updating a display over SPI four wire protocol in the background while Python code runs. //| It doesn't handle display initialization. //| -//| .. warning:: This will be changed before 4.0.0. Consider it very experimental. -//| //| .. class:: ParallelBus(*, data0, command, chip_select, write, read, reset) //| //| Create a ParallelBus object associated with the given pins. The bus is inferred from data0 diff --git a/shared-bindings/displayio/TileGrid.c b/shared-bindings/displayio/TileGrid.c index d0be43fbf..eeacd1918 100644 --- a/shared-bindings/displayio/TileGrid.c +++ b/shared-bindings/displayio/TileGrid.c @@ -60,8 +60,6 @@ static void unpack_position(mp_obj_t position_obj, int16_t* x, int16_t* y) { //| //| 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 diff --git a/shared-bindings/displayio/__init__.c b/shared-bindings/displayio/__init__.c index df7162cf1..1ee755014 100644 --- a/shared-bindings/displayio/__init__.c +++ b/shared-bindings/displayio/__init__.c @@ -31,9 +31,11 @@ #include "shared-bindings/displayio/__init__.h" #include "shared-bindings/displayio/Bitmap.h" +#include "shared-bindings/displayio/BuiltinFont.h" #include "shared-bindings/displayio/ColorConverter.h" #include "shared-bindings/displayio/Display.h" #include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/Glyph.h" #include "shared-bindings/displayio/Group.h" #include "shared-bindings/displayio/OnDiskBitmap.h" #include "shared-bindings/displayio/Palette.h" @@ -51,17 +53,17 @@ //| The `displayio` module contains classes to manage display output //| including synchronizing with refresh rates and partial updating. //| -//| .. warning:: This will be changed before 4.0.0. Consider it very experimental. -//| //| Libraries //| //| .. toctree:: //| :maxdepth: 3 //| //| Bitmap +//| BuiltinFont //| ColorConverter //| Display //| FourWire +//| Glyph //| Group //| OnDiskBitmap //| Palette @@ -88,8 +90,10 @@ MP_DEFINE_CONST_FUN_OBJ_0(displayio_release_displays_obj, displayio_release_disp STATIC const mp_rom_map_elem_t displayio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_displayio) }, { MP_ROM_QSTR(MP_QSTR_Bitmap), MP_ROM_PTR(&displayio_bitmap_type) }, + { MP_ROM_QSTR(MP_QSTR_BuiltinFont), MP_ROM_PTR(&displayio_builtinfont_type) }, { MP_ROM_QSTR(MP_QSTR_ColorConverter), MP_ROM_PTR(&displayio_colorconverter_type) }, { MP_ROM_QSTR(MP_QSTR_Display), MP_ROM_PTR(&displayio_display_type) }, + { MP_ROM_QSTR(MP_QSTR_Glyph), MP_ROM_PTR(&displayio_glyph_type) }, { 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) }, |
