From 206d0e598adb66e2b23a39a7d11e2f5087513205 Mon Sep 17 00:00:00 2001 From: warriorofwire <3454741+WarriorOfWire@users.noreply.github.com> Date: Sat, 2 May 2020 02:21:35 -0700 Subject: Add vectorio: for drawing shapes vectorio builds on m4 express feather Concrete shapes are composed into a VectorShape which is put into a displayio Group for display. VectorShape provides transpose and x/y positioning for shape implementations. Included Shapes: * Circle - A radius; Circle is positioned at its axis in the VectorShape. - You can freely modify the radius to grow and shrink the circle in-place. * Polygon - An ordered list of points. - Beteween each successive point an edge is inferred. A final edge closing the shape is inferred between the last point and the first point. - You can modify the points in a Polygon. The points' coordinate system is relative to (0, 0) so if you'd like a top-center justified 10x20 rectangle you can do points [(-5, 0), (5, 0), (5, 20), (0, 20)] and your VectorShape x and y properties will position the rectangle relative to its top center point * Rectangle A width and a height. --- shared-bindings/vectorio/Rectangle.c | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 shared-bindings/vectorio/Rectangle.c (limited to 'shared-bindings/vectorio/Rectangle.c') diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c new file mode 100644 index 000000000..ea468f788 --- /dev/null +++ b/shared-bindings/vectorio/Rectangle.c @@ -0,0 +1,56 @@ + +#include "shared-bindings/vectorio/Rectangle.h" + +#include + +#include "py/objtype.h" +#include "py/runtime.h" +#include "supervisor/shared/translate.h" + + +//| .. currentmodule:: vectorio +//| +//| :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 vectorio_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 }; + 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); + } + + vectorio_rectangle_t *self = m_new_obj(vectorio_rectangle_t); + self->base.type = &vectorio_rectangle_type; + common_hal_vectorio_rectangle_construct(self, width, height); + + return MP_OBJ_FROM_PTR(self); +} + + +STATIC const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = { +}; +STATIC MP_DEFINE_CONST_DICT(vectorio_rectangle_locals_dict, vectorio_rectangle_locals_dict_table); + +const mp_obj_type_t vectorio_rectangle_type = { + { &mp_type_type }, + .name = MP_QSTR_Rectangle, + .make_new = vectorio_rectangle_make_new, + .locals_dict = (mp_obj_dict_t*)&vectorio_rectangle_locals_dict, +}; -- cgit v1.2.3 From 4e646ee6e4473195df3eba4bd7e006e44bfd1cdc Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 12 May 2020 18:40:02 -0700 Subject: Move vectorio to stubs --- shared-bindings/vectorio/Circle.c | 18 ++++++------------ shared-bindings/vectorio/Polygon.c | 23 ++++++++--------------- shared-bindings/vectorio/Rectangle.c | 14 +++++--------- shared-bindings/vectorio/VectorShape.c | 33 ++++++++++++++------------------- shared-bindings/vectorio/__init__.c | 21 +-------------------- 5 files changed, 34 insertions(+), 75 deletions(-) (limited to 'shared-bindings/vectorio/Rectangle.c') diff --git a/shared-bindings/vectorio/Circle.c b/shared-bindings/vectorio/Circle.c index 260e80d0f..6b5682e14 100644 --- a/shared-bindings/vectorio/Circle.c +++ b/shared-bindings/vectorio/Circle.c @@ -9,17 +9,12 @@ #include "py/runtime.h" #include "supervisor/shared/translate.h" - -//| .. currentmodule:: vectorio -//| -//| :class:`Circle` -- Represents a circle by its radius -//| ========================================================================== +//| class Circle: //| -//| .. class:: Circle(radius) +//| def __init__(self, radius: int): +//| """Circle is positioned on screen by its center point. //| -//| Circle is positioned on screen by its center point. -//| -//| :param int radius: The radius of the circle in pixels +//| :param radius: The radius of the circle in pixels""" //| static mp_obj_t vectorio_circle_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_radius }; @@ -42,9 +37,8 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg } -//| .. attribute:: radius -//| -//| The radius of the circle in pixels. +//| radius : int = ... +//| """The radius of the circle in pixels.""" //| STATIC mp_obj_t vectorio_circle_obj_get_radius(mp_obj_t self_in) { vectorio_circle_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/shared-bindings/vectorio/Polygon.c b/shared-bindings/vectorio/Polygon.c index b8bb377ac..5f4d85e21 100644 --- a/shared-bindings/vectorio/Polygon.c +++ b/shared-bindings/vectorio/Polygon.c @@ -49,17 +49,13 @@ static mp_obj_t _to_points_list(mp_obj_t points_tuple_list) { } return points_list; } - - - -//| .. currentmodule:: vectorio -//| -//| :class:`Polygon` -- Represents a closed shape by ordered vertices -//| ========================================================================== +//| from typing import List, Tuple //| -//| .. class:: Polygon( List[ Tuple[ x, y ], ... ] ) +//| class Polygon: +//| def __init__(self, points: List[ Tuple[ x, y ], ... ] ): +//| """Represents a closed shape by ordered vertices //| -//| :param [Point] points_array: Vertices for the polygon +//| :param points: Vertices for the polygon""" //| static mp_obj_t vectorio_polygon_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_points_list }; @@ -83,14 +79,13 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar } -//| .. attribute:: points -//| -//| Set a new look and shape for this polygon +//| points: List[ Tuple[ x, y ], ... ] = ... +//| """Set a new look and shape for this polygon""" //| STATIC mp_obj_t vectorio_polygon_obj_get_points(mp_obj_t self_in) { vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_t list = mp_obj_new_list(0, NULL); - + size_t len = 0; mp_obj_t *items; mp_obj_list_get(common_hal_vectorio_polygon_get_points(self), &len, &items); @@ -123,8 +118,6 @@ const mp_obj_property_t vectorio_polygon_points_obj = { (mp_obj_t)&mp_const_none_obj}, }; - - STATIC const mp_rom_map_elem_t vectorio_polygon_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_points), MP_ROM_PTR(&vectorio_polygon_points_obj) }, }; diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index ea468f788..f04a25c35 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -7,16 +7,12 @@ #include "py/runtime.h" #include "supervisor/shared/translate.h" - -//| .. currentmodule:: vectorio -//| -//| :class:`Rectangle` -- Represents a rectangle by defining its bounds -//| ========================================================================== -//| -//| .. class:: Rectangle(width, height) +//| class Rectangle: +//| def __init__(self, width: int, height: int): +//| """Represents a rectangle by defining its bounds //| -//| :param int width: The number of pixels wide -//| :param int height: The number of pixels high +//| :param width: The number of pixels wide +//| :param height: The number of pixels high""" //| static mp_obj_t vectorio_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 }; diff --git a/shared-bindings/vectorio/VectorShape.c b/shared-bindings/vectorio/VectorShape.c index 4b39ec996..c512bcd54 100644 --- a/shared-bindings/vectorio/VectorShape.c +++ b/shared-bindings/vectorio/VectorShape.c @@ -19,17 +19,15 @@ #include "supervisor/shared/translate.h" -//| .. currentmodule:: vectorio +//| class VectorShape: +//| def __init__(self, shape: vectorio.Polygon, pixel_shader: displayio.Palette, x: int=0, y: int=0): +//| """Binds a vector shape to a location and pixel color //| -//| :class:`VectorShape` -- Binds a vector shape to a location and pixel color -//| ========================================================================== -//| -//| .. class:: VectorShape( shape, pixel_shader, x=0, y=0) -//| -//| :param vectorio.Polygon shape: The shape to draw. -//| :param displayio.Palette pixel_shader: The pixel shader that produces colors from values -//| :param int x: Initial x position of the center axis of the shape within the parent. -//| :param int y: Initial y position of the center axis of the shape within the parent. +//| :param shape: The shape to draw. +//| :param pixel_shader: The pixel shader that produces colors from values +//| :param x: Initial x position of the center axis of the shape within the parent. +//| :param y: Initial y position of the center axis of the shape within the parent.""" +//| ... //| STATIC mp_obj_t vectorio_vector_shape_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_shape, ARG_pixel_shader, ARG_x, ARG_y }; @@ -95,9 +93,8 @@ STATIC mp_obj_t vectorio_vector_shape_make_new(const mp_obj_type_t *type, size_t } -//| .. attribute:: x -//| -//| X position of the center point of the shape in the parent. +//| x: int = ... +//| """X position of the center point of the shape in the parent.""" //| STATIC mp_obj_t vectorio_vector_shape_obj_get_x(mp_obj_t self_in) { vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in); @@ -122,9 +119,8 @@ const mp_obj_property_t vectorio_vector_shape_x_obj = { }; -//| .. attribute:: y -//| -//| Y position of the center point of the shape in the parent. +//| y: int = ... +//| """Y position of the center point of the shape in the parent.""" //| STATIC mp_obj_t vectorio_vector_shape_obj_get_y(mp_obj_t self_in) { vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in); @@ -149,9 +145,8 @@ const mp_obj_property_t vectorio_vector_shape_y_obj = { }; -//| .. attribute:: pixel_shader -//| -//| The pixel shader of the shape. +//| pixel_shader: displayio.Palette = ... +//| """The pixel shader of the shape.""" //| STATIC mp_obj_t vectorio_vector_shape_obj_get_pixel_shader(mp_obj_t self_in) { vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/shared-bindings/vectorio/__init__.c b/shared-bindings/vectorio/__init__.c index b9e3828bd..c74783426 100644 --- a/shared-bindings/vectorio/__init__.c +++ b/shared-bindings/vectorio/__init__.c @@ -8,27 +8,8 @@ #include "shared-bindings/vectorio/Rectangle.h" #include "shared-bindings/vectorio/VectorShape.h" -//| :mod:`vectorio` --- Lightweight 2d shapes for displays -//| ========================================================================= +//| """Lightweight 2d shapes for displays""" //| -//| .. module:: vectorio -//| :synopsis: Adds vector graphics to displayio -//| :platform: SAMD21, SAMD51, nRF52 -//| -//| The `vectorio` module contains classes to construct shapes -//| by describing their points rather than providing them in bitmaps. -//| -//| Libraries -//| -//| .. toctree:: -//| :maxdepth: 3 -//| -//| Circle -//| Polygon -//| Rectangle -//| VectorShape -//| - STATIC const mp_rom_map_elem_t vectorio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vectorio) }, -- cgit v1.2.3