diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index ec8e6c3e986c2..210c407ee3047 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -350,6 +350,7 @@ SRC_SHARED_MODULE_ALL = \ displayio/I2CDisplay.c \ displayio/OnDiskBitmap.c \ displayio/Palette.c \ + displayio/Rectangle.c\ displayio/Shape.c \ displayio/TileGrid.c \ displayio/__init__.c \ diff --git a/shared-bindings/board/__init__.c b/shared-bindings/board/__init__.c index 3dda59fb8ef28..28d3f7c91b31a 100644 --- a/shared-bindings/board/__init__.c +++ b/shared-bindings/board/__init__.c @@ -113,7 +113,7 @@ mp_obj_t board_uart(void) { } #else mp_obj_t board_uart(void) { - mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_SPI); + mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_UART); return NULL; } #endif diff --git a/shared-bindings/displayio/Rectangle.c b/shared-bindings/displayio/Rectangle.c new file mode 100644 index 0000000000000..cbd4d3c157b92 --- /dev/null +++ b/shared-bindings/displayio/Rectangle.c @@ -0,0 +1,56 @@ + +#include "shared-bindings/displayio/Rectangle.h" + +#include + +#include "py/objtype.h" +#include "py/runtime.h" +#include "supervisor/shared/translate.h" + + +//| .. currentmodule:: displayio +//| +//| :class:`Rectangle` -- Represents a rectangle by defining its bounds +//| ========================================================================== +//| +//| .. class:: Rectangle(width, height) +//| +//| :param int width: The number of pixels wide +//| :param int height: The number of pixels high +//| +static mp_obj_t displayio_rectangle_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_width, ARG_height, ARG_rotation_radians }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT }, + }; + 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 width = args[ARG_width].u_int; + if (width < 1) { + mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_width); + } + mp_int_t height = args[ARG_height].u_int; + if (height < 1) { + mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_height); + } + + displayio_rectangle_t *self = m_new_obj(displayio_rectangle_t); + self->base.type = &displayio_rectangle_type; + common_hal_displayio_rectangle_construct(self, width, height); + + return MP_OBJ_FROM_PTR(self); +} + + +STATIC const mp_rom_map_elem_t displayio_rectangle_locals_dict_table[] = { +}; +STATIC MP_DEFINE_CONST_DICT(displayio_rectangle_locals_dict, displayio_rectangle_locals_dict_table); + +const mp_obj_type_t displayio_rectangle_type = { + { &mp_type_type }, + .name = MP_QSTR_Rectangle, + .make_new = displayio_rectangle_make_new, + .locals_dict = (mp_obj_dict_t*)&displayio_rectangle_locals_dict, +}; diff --git a/shared-bindings/displayio/Rectangle.h b/shared-bindings/displayio/Rectangle.h new file mode 100644 index 0000000000000..590d26d7c9475 --- /dev/null +++ b/shared-bindings/displayio/Rectangle.h @@ -0,0 +1,12 @@ +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_RECTANGLE_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_RECTANGLE_H + +#include "shared-module/displayio/Rectangle.h" + +extern const mp_obj_type_t displayio_rectangle_type; + +void common_hal_displayio_rectangle_construct(displayio_rectangle_t *self, uint32_t width, uint32_t height); + +uint32_t common_hal_displayio_rectangle_get_pixel(void *shape, int16_t x, int16_t y); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_RECTANGLE_H diff --git a/shared-bindings/displayio/TileGrid.c b/shared-bindings/displayio/TileGrid.c index 288eb4b236930..99af232a4b2dd 100644 --- a/shared-bindings/displayio/TileGrid.c +++ b/shared-bindings/displayio/TileGrid.c @@ -37,6 +37,7 @@ #include "shared-bindings/displayio/ColorConverter.h" #include "shared-bindings/displayio/OnDiskBitmap.h" #include "shared-bindings/displayio/Palette.h" +#include "shared-bindings/displayio/Rectangle.h" #include "shared-bindings/displayio/Shape.h" #include "supervisor/shared/translate.h" @@ -93,6 +94,10 @@ STATIC mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_ displayio_shape_t* bmp = MP_OBJ_TO_PTR(native); bitmap_width = bmp->width; bitmap_height = bmp->height; + } else if (MP_OBJ_IS_TYPE(bitmap, &displayio_rectangle_type)) { + displayio_rectangle_t* rectangle = MP_OBJ_TO_PTR(bitmap); + bitmap_width = rectangle->width; + bitmap_height = rectangle->height; } else if (MP_OBJ_IS_TYPE(bitmap, &displayio_bitmap_type)) { displayio_bitmap_t* bmp = MP_OBJ_TO_PTR(bitmap); native = bitmap; diff --git a/shared-bindings/displayio/__init__.c b/shared-bindings/displayio/__init__.c index f7832559307c5..ee25c0838c277 100644 --- a/shared-bindings/displayio/__init__.c +++ b/shared-bindings/displayio/__init__.c @@ -40,6 +40,7 @@ #include "shared-bindings/displayio/OnDiskBitmap.h" #include "shared-bindings/displayio/Palette.h" #include "shared-bindings/displayio/ParallelBus.h" +#include "shared-bindings/displayio/Rectangle.h" #include "shared-bindings/displayio/Shape.h" #include "shared-bindings/displayio/TileGrid.h" @@ -69,6 +70,7 @@ //| Palette //| ParallelBus //| Shape +//| Rectangle //| TileGrid //| @@ -97,6 +99,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_Rectangle), MP_ROM_PTR(&displayio_rectangle_type) }, { MP_ROM_QSTR(MP_QSTR_Shape), MP_ROM_PTR(&displayio_shape_type) }, { MP_ROM_QSTR(MP_QSTR_TileGrid), MP_ROM_PTR(&displayio_tilegrid_type) }, diff --git a/shared-module/displayio/Rectangle.c b/shared-module/displayio/Rectangle.c new file mode 100644 index 0000000000000..00c5fe6529e78 --- /dev/null +++ b/shared-module/displayio/Rectangle.c @@ -0,0 +1,16 @@ +#include "shared-bindings/displayio/Rectangle.h" + +#include "py/runtime.h" + +void common_hal_displayio_rectangle_construct(displayio_rectangle_t *self, uint32_t width, uint32_t height) { + self->width = width; + self->height = height; +} + +uint32_t common_hal_displayio_rectangle_get_pixel(void *obj, int16_t x, int16_t y) { + displayio_rectangle_t *self = obj; + if (x < 0 || x >= self->width || y >= self->height || y < 0) { + return 0; + } + return 1; +} diff --git a/shared-module/displayio/Rectangle.h b/shared-module/displayio/Rectangle.h new file mode 100644 index 0000000000000..2693d4dfcfdb0 --- /dev/null +++ b/shared-module/displayio/Rectangle.h @@ -0,0 +1,14 @@ +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_RECTANGLE_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_RECTANGLE_H + +#include + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; +} displayio_rectangle_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_RECTANGLE_H