|
| 1 | +/* |
| 2 | + * This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Rose Hooper |
| 7 | + * Copyright (c) 2022 Jeff Epler for Adafruit Industries |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/objproperty.h" |
| 29 | +#include "py/objtype.h" |
| 30 | +#include "py/runtime.h" |
| 31 | + |
| 32 | +#include "shared-bindings/adafruit_pixelmap/PixelMap.h" |
| 33 | +#include "shared-bindings/adafruit_pixelbuf/PixelBuf.h" |
| 34 | +#include "shared-module/adafruit_pixelmap/PixelMap.h" |
| 35 | + |
| 36 | +//| class PixelMap: |
| 37 | +//| def __init__(self, pixelbuf: PixelBuf, indices: Tuple[Union[int, Tuple[int]]]) -> None: |
| 38 | +//| """Construct a PixelMap object that uses the given indices of the underlying pixelbuf""" |
| 39 | + |
| 40 | +STATIC mp_obj_t pixelmap_pixelmap_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 41 | + enum { ARG_pixelbuf, ARG_indices }; |
| 42 | + static const mp_arg_t allowed_args[] = { |
| 43 | + { MP_QSTR_pixelbuf, MP_ARG_REQUIRED }, |
| 44 | + { MP_QSTR_indices, MP_ARG_REQUIRED }, |
| 45 | + }; |
| 46 | + |
| 47 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 48 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 49 | + |
| 50 | + mp_obj_t pixelbuf = args[ARG_pixelbuf].u_obj; |
| 51 | + |
| 52 | + mp_obj_t native_pixelbuf = mp_obj_cast_to_native_base(pixelbuf, &pixelbuf_pixelbuf_type); |
| 53 | + if (!native_pixelbuf) { |
| 54 | + (void)mp_arg_validate_type(args[ARG_pixelbuf].u_obj, &pixelbuf_pixelbuf_type, MP_QSTR_pixelbuf); |
| 55 | + } |
| 56 | + mp_obj_assert_native_inited(native_pixelbuf); |
| 57 | + |
| 58 | + size_t buflen = common_hal_adafruit_pixelbuf_pixelbuf_get_len(pixelbuf); |
| 59 | + |
| 60 | + mp_obj_t indices = mp_arg_validate_type(args[ARG_indices].u_obj, &mp_type_tuple, MP_QSTR_indices); |
| 61 | + |
| 62 | + // validate indices |
| 63 | + size_t len; |
| 64 | + mp_obj_t *items; |
| 65 | + mp_obj_tuple_get(indices, &len, &items); |
| 66 | + mp_arg_validate_length_min(len, 1, MP_QSTR_items); |
| 67 | + |
| 68 | + for (size_t i = 0; i < len; i++) { |
| 69 | + mp_obj_t item = items[i]; |
| 70 | + if (mp_obj_is_small_int(item)) { |
| 71 | + mp_arg_validate_index_range(MP_OBJ_SMALL_INT_VALUE(item), 0, buflen - 1, MP_QSTR_index); |
| 72 | + } else if (mp_obj_is_tuple_compatible(item)) { |
| 73 | + size_t len1; |
| 74 | + mp_obj_t *items1; |
| 75 | + mp_obj_tuple_get(item, &len1, &items1); |
| 76 | + for (size_t j = 0; j < len1; j++) { |
| 77 | + mp_obj_t item1 = items1[j]; |
| 78 | + if (!mp_obj_is_small_int(item1)) { |
| 79 | + mp_raise_TypeError(translate("nested index must be int")); |
| 80 | + } |
| 81 | + mp_arg_validate_index_range(MP_OBJ_SMALL_INT_VALUE(item1), 0, buflen - 1, MP_QSTR_index); |
| 82 | + } |
| 83 | + } else { |
| 84 | + mp_raise_TypeError(translate("index must be tuple or int")); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + pixelmap_pixelmap_obj_t *self = m_new_obj(pixelmap_pixelmap_obj_t); |
| 89 | + self->base.type = &pixelmap_pixelmap_type; |
| 90 | + shared_module_pixelmap_pixelmap_construct(self, pixelbuf, indices); |
| 91 | + |
| 92 | + return MP_OBJ_FROM_PTR(self); |
| 93 | +} |
| 94 | + |
| 95 | +//| auto_write: bool |
| 96 | +//| """True if updates should be automatically written""" |
| 97 | +STATIC mp_obj_t pixelmap_pixelmap_auto_write_get(const mp_obj_t self_in) { |
| 98 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 99 | + return mp_obj_new_bool(shared_module_pixelmap_pixelmap_auto_write_get(self)); |
| 100 | +} |
| 101 | +MP_DEFINE_CONST_FUN_OBJ_1(pixelmap_pixelmap_auto_write_get_obj, pixelmap_pixelmap_auto_write_get); |
| 102 | + |
| 103 | +STATIC mp_obj_t pixelmap_pixelmap_auto_write_set(const mp_obj_t self_in, const mp_obj_t arg) { |
| 104 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 105 | + shared_module_pixelmap_pixelmap_auto_write_set(self, mp_obj_is_true(arg)); |
| 106 | + return mp_const_none; |
| 107 | +} |
| 108 | +MP_DEFINE_CONST_FUN_OBJ_2(pixelmap_pixelmap_auto_write_set_obj, pixelmap_pixelmap_auto_write_set); |
| 109 | + |
| 110 | +MP_PROPERTY_GETSET(pixelmap_pixelmap_auto_write_obj, |
| 111 | + (mp_obj_t)&pixelmap_pixelmap_auto_write_get_obj, |
| 112 | + (mp_obj_t)&pixelmap_pixelmap_auto_write_set_obj); |
| 113 | + |
| 114 | +//| bpp: int |
| 115 | +//| """The number of bytes per pixel in the buffer (read-only)""" |
| 116 | +STATIC mp_obj_t pixelmap_pixelmap_obj_get_bpp(mp_obj_t self_in) { |
| 117 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 118 | + return MP_OBJ_NEW_SMALL_INT(common_hal_adafruit_pixelbuf_pixelbuf_get_bpp(self->pixelbuf)); |
| 119 | +} |
| 120 | +MP_DEFINE_CONST_FUN_OBJ_1(pixelmap_pixelmap_get_bpp_obj, pixelmap_pixelmap_obj_get_bpp); |
| 121 | + |
| 122 | +MP_PROPERTY_GETTER(pixelmap_pixelmap_bpp_obj, |
| 123 | + (mp_obj_t)&pixelmap_pixelmap_get_bpp_obj); |
| 124 | + |
| 125 | +//| byteorder: str |
| 126 | +//| """byteorder string for the buffer (read-only)""" |
| 127 | +STATIC mp_obj_t pixelmap_pixelmap_obj_get_byteorder(mp_obj_t self_in) { |
| 128 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 129 | + return common_hal_adafruit_pixelbuf_pixelbuf_get_byteorder_string(self->pixelbuf); |
| 130 | +} |
| 131 | +MP_DEFINE_CONST_FUN_OBJ_1(pixelmap_pixelmap_get_byteorder, pixelmap_pixelmap_obj_get_byteorder); |
| 132 | +MP_PROPERTY_GETTER(pixelmap_pixelmap_byteorder_obj, |
| 133 | + (mp_obj_t)&pixelmap_pixelmap_get_byteorder); |
| 134 | + |
| 135 | +//| |
| 136 | +//| def fill(self, color: PixelType, /) -> None: |
| 137 | +//| """Fill all the pixels in the map with the given color""" |
| 138 | +STATIC mp_obj_t pixelmap_pixelmap_fill(const mp_obj_t self_in, const mp_obj_t color) { |
| 139 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 140 | + |
| 141 | + shared_module_pixelmap_pixelmap_fill(self, color); |
| 142 | + return mp_const_none; |
| 143 | +} |
| 144 | +MP_DEFINE_CONST_FUN_OBJ_2(pixelmap_pixelmap_fill_obj, pixelmap_pixelmap_fill); |
| 145 | + |
| 146 | +//| |
| 147 | +//| def indices(self, index: int, /) -> Tuple[int]: |
| 148 | +//| """Return the PixelBuf indices for a PixelMap index""" |
| 149 | +STATIC mp_obj_t pixelmap_pixelmap_indices(const mp_obj_t self_in, const mp_obj_t index) { |
| 150 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 151 | + |
| 152 | + return shared_module_pixelmap_pixelmap_indices(self, mp_obj_get_int(index)); |
| 153 | + return mp_const_none; |
| 154 | +} |
| 155 | +MP_DEFINE_CONST_FUN_OBJ_2(pixelmap_pixelmap_indices_obj, pixelmap_pixelmap_indices); |
| 156 | + |
| 157 | + |
| 158 | +//| @overload |
| 159 | +//| def __getitem__(self, index: int) -> PixelReturnType: |
| 160 | +//| """Retrieve the value of one of the underlying pixels at 'index'. |
| 161 | +//| |
| 162 | +//| Note that slices are not supported by PixelMap.__getitem__""" |
| 163 | +//| @overload |
| 164 | +//| def __setitem__(self, index: slice, value: PixelSequence) -> None: ... |
| 165 | +//| @overload |
| 166 | +//| def __setitem__(self, index: int, value: PixelType) -> None: |
| 167 | +//| """Sets the pixel value at the given index. Value can either be a tuple or integer. Tuples are |
| 168 | +//| The individual (Red, Green, Blue[, White]) values between 0 and 255. If given an integer, the |
| 169 | +//| red, green and blue values are packed into the lower three bytes (0xRRGGBB). |
| 170 | +//| For RGBW byteorders, if given only RGB values either as an int or as a tuple, the white value |
| 171 | +//| is used instead when the red, green, and blue values are the same.""" |
| 172 | +//| ... |
| 173 | +STATIC mp_obj_t pixelmap_pixelmap_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { |
| 174 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 175 | + if (value == MP_OBJ_NULL) { |
| 176 | + // delete |
| 177 | + return MP_OBJ_NULL; // op not supported |
| 178 | + } else if (value == MP_OBJ_SENTINEL) { |
| 179 | + int index = mp_obj_get_int(index_in); |
| 180 | + return shared_module_pixelmap_pixelmap_getitem(self, index); |
| 181 | + } |
| 182 | + |
| 183 | + // get |
| 184 | + if (0) { |
| 185 | + #if MICROPY_PY_BUILTINS_SLICE |
| 186 | + } else if (mp_obj_is_type(index_in, &mp_type_slice)) { |
| 187 | + shared_module_pixelmap_pixelmap_setslice(self, index_in, value); |
| 188 | + #endif |
| 189 | + } else { |
| 190 | + shared_module_pixelmap_pixelmap_setitem(self, mp_obj_get_int(index_in), value); |
| 191 | + } |
| 192 | + return mp_const_none; |
| 193 | +} |
| 194 | + |
| 195 | +//| def __len__(self) -> int: |
| 196 | +//| """Length of the map""" |
| 197 | +STATIC mp_obj_t pixelmap_pixelmap_unary_op(mp_unary_op_t op, mp_obj_t self_in) { |
| 198 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 199 | + switch (op) { |
| 200 | + case MP_UNARY_OP_BOOL: |
| 201 | + return mp_const_true; |
| 202 | + case MP_UNARY_OP_LEN: |
| 203 | + return MP_OBJ_NEW_SMALL_INT(self->len); |
| 204 | + default: |
| 205 | + return MP_OBJ_NULL; // op not supported |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +//| def show(self) -> None: |
| 210 | +//| """Transmits the color data to the pixels so that they are shown. This is done automatically |
| 211 | +//| when `auto_write` is True.""" |
| 212 | +//| ... |
| 213 | +//| |
| 214 | + |
| 215 | +STATIC mp_obj_t pixelmap_pixelmap_show(mp_obj_t self_in) { |
| 216 | + pixelmap_pixelmap_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 217 | + common_hal_adafruit_pixelbuf_pixelbuf_show(self->pixelbuf); |
| 218 | + return mp_const_none; |
| 219 | +} |
| 220 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelmap_pixelmap_show_obj, pixelmap_pixelmap_show); |
| 221 | + |
| 222 | +STATIC const mp_rom_map_elem_t pixelmap_pixelmap_locals_dict_table[] = { |
| 223 | + { MP_ROM_QSTR(MP_QSTR_auto_write), MP_ROM_PTR(&pixelmap_pixelmap_auto_write_obj) }, |
| 224 | + { MP_ROM_QSTR(MP_QSTR_bpp), MP_ROM_PTR(&pixelmap_pixelmap_bpp_obj) }, |
| 225 | + { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_PTR(&pixelmap_pixelmap_byteorder_obj) }, |
| 226 | + { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&pixelmap_pixelmap_fill_obj) }, |
| 227 | + { MP_ROM_QSTR(MP_QSTR_indices), MP_ROM_PTR(&pixelmap_pixelmap_indices_obj) }, |
| 228 | + { MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&pixelmap_pixelmap_show_obj) }, |
| 229 | +}; |
| 230 | + |
| 231 | +STATIC MP_DEFINE_CONST_DICT(pixelmap_pixelmap_locals_dict, pixelmap_pixelmap_locals_dict_table); |
| 232 | + |
| 233 | + |
| 234 | +const mp_obj_type_t pixelmap_pixelmap_type = { |
| 235 | + { &mp_type_type }, |
| 236 | + .name = MP_QSTR_PixelMap, |
| 237 | + .flags = MP_TYPE_FLAG_EXTENDED, |
| 238 | + .locals_dict = (mp_obj_t)&pixelmap_pixelmap_locals_dict, |
| 239 | + .make_new = pixelmap_pixelmap_make_new, |
| 240 | + MP_TYPE_EXTENDED_FIELDS( |
| 241 | + .subscr = pixelmap_pixelmap_subscr, |
| 242 | + .unary_op = pixelmap_pixelmap_unary_op, |
| 243 | + ), |
| 244 | +}; |
0 commit comments