|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/displayio/Shape.h" |
| 28 | + |
| 29 | +#include <stdint.h> |
| 30 | + |
| 31 | +#include "py/binary.h" |
| 32 | +#include "py/objproperty.h" |
| 33 | +#include "py/runtime.h" |
| 34 | +#include "shared-bindings/util.h" |
| 35 | +#include "supervisor/shared/translate.h" |
| 36 | + |
| 37 | +//| .. currentmodule:: displayio |
| 38 | +//| |
| 39 | +//| :class:`Shape` -- Represents a shape by defining its bounds on each row |
| 40 | +//| ========================================================================== |
| 41 | +//| |
| 42 | +//| Represents any shape made by defining boundaries that may be mirrored. |
| 43 | +//| |
| 44 | +//| .. warning:: This will likely be changed before 4.0.0. Consider it very experimental. |
| 45 | +//| |
| 46 | +//| .. class:: Shape(width, height, *, mirror_x=False, mirrored_y=False) |
| 47 | +//| |
| 48 | +//| Create a Shape object with the given fixed size. Each pixel is one bit and is stored by the |
| 49 | +//| column boundaries of the shape on each row. Each row's boundary defaults to the full row. |
| 50 | +//| |
| 51 | +//| :param int width: The number of pixels wide |
| 52 | +//| :param int height: The number of pixels high |
| 53 | +//| :param bool mirror_x: When true the left boundary is mirrored to the right. |
| 54 | +//| :param bool mirror_y: When true the top boundary is mirrored to the bottom. |
| 55 | +//| |
| 56 | +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) { |
| 57 | + mp_arg_check_num(n_args, n_kw, 2, 4, true); |
| 58 | + mp_map_t kw_args; |
| 59 | + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); |
| 60 | + enum { ARG_width, ARG_height, ARG_mirror_x, ARG_mirror_y }; |
| 61 | + static const mp_arg_t allowed_args[] = { |
| 62 | + { MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 63 | + { MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 64 | + { MP_QSTR_mirror_x, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, |
| 65 | + { MP_QSTR_mirror_y, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, |
| 66 | + }; |
| 67 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 68 | + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 69 | + |
| 70 | + displayio_shape_t *self = m_new_obj(displayio_shape_t); |
| 71 | + self->base.type = &displayio_shape_type; |
| 72 | + common_hal_displayio_shape_construct(self, |
| 73 | + args[ARG_width].u_int, |
| 74 | + args[ARG_height].u_int, |
| 75 | + args[ARG_mirror_x].u_bool, |
| 76 | + args[ARG_mirror_y].u_bool); |
| 77 | + |
| 78 | + return MP_OBJ_FROM_PTR(self); |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +//| .. method:: set_boundary(y, start_x, end_x) |
| 83 | +//| |
| 84 | +//| Loads pre-packed data into the given row. |
| 85 | +//| |
| 86 | +STATIC mp_obj_t displayio_shape_obj_set_boundary(size_t n_args, const mp_obj_t *args) { |
| 87 | + (void) n_args; |
| 88 | + displayio_shape_t *self = MP_OBJ_TO_PTR(args[0]); |
| 89 | + mp_int_t y; |
| 90 | + if (!mp_obj_get_int_maybe(args[1], &y)) { |
| 91 | + mp_raise_ValueError(translate("y should be an int")); |
| 92 | + } |
| 93 | + mp_int_t start_x; |
| 94 | + if (!mp_obj_get_int_maybe(args[2], &start_x)) { |
| 95 | + mp_raise_ValueError(translate("start_x should be an int")); |
| 96 | + } |
| 97 | + mp_int_t end_x; |
| 98 | + if (!mp_obj_get_int_maybe(args[3], &end_x)) { |
| 99 | + mp_raise_ValueError(translate("end_x should be an int")); |
| 100 | + } |
| 101 | + common_hal_displayio_shape_set_boundary(self, y, start_x, end_x); |
| 102 | + |
| 103 | + return mp_const_none; |
| 104 | +} |
| 105 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(displayio_shape_set_boundary_obj, 4, 4, displayio_shape_obj_set_boundary); |
| 106 | + |
| 107 | +STATIC const mp_rom_map_elem_t displayio_shape_locals_dict_table[] = { |
| 108 | + { MP_ROM_QSTR(MP_QSTR_set_boundary), MP_ROM_PTR(&displayio_shape_set_boundary_obj) }, |
| 109 | +}; |
| 110 | +STATIC MP_DEFINE_CONST_DICT(displayio_shape_locals_dict, displayio_shape_locals_dict_table); |
| 111 | + |
| 112 | +const mp_obj_type_t displayio_shape_type = { |
| 113 | + { &mp_type_type }, |
| 114 | + .name = MP_QSTR_Shape, |
| 115 | + .make_new = displayio_shape_make_new, |
| 116 | + .locals_dict = (mp_obj_dict_t*)&displayio_shape_locals_dict, |
| 117 | +}; |
0 commit comments