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/Bitmap.c | |
| 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/Bitmap.c')
| -rw-r--r-- | shared-bindings/displayio/Bitmap.c | 111 |
1 files changed, 92 insertions, 19 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, }; |
