Skip to content

Commit c87d90e

Browse files
authored
Merge pull request #5090 from WarriorOfWire/draw_protocol
Draw protocol
2 parents 052c53e + cf2712d commit c87d90e

File tree

22 files changed

+620
-208
lines changed

22 files changed

+620
-208
lines changed

docs/redirects.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ shared-bindings/ustack/__init__.rst shared-bindings/ustack/
152152
shared-bindings/vectorio/Circle.rst shared-bindings/vectorio/#vectorio.Circle
153153
shared-bindings/vectorio/Polygon.rst shared-bindings/vectorio/#vectorio.Polygon
154154
shared-bindings/vectorio/Rectangle.rst shared-bindings/vectorio/#vectorio.Rectangle
155-
shared-bindings/vectorio/VectorShape.rst shared-bindings/vectorio/#vectorio.VectorShape
156155
shared-bindings/vectorio/__init__.rst shared-bindings/vectorio/
157156
shared-bindings/watchdog/WatchDogMode.rst shared-bindings/watchdog/#watchdog.WatchDogMode
158157
shared-bindings/watchdog/WatchDogTimer.rst shared-bindings/watchdog/#watchdog.WatchDogTimer

locale/circuitpython.pot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ msgstr ""
318318
msgid "'yield' outside function"
319319
msgstr ""
320320

321+
#: shared-module/vectorio/VectorShape.c
322+
msgid "(x,y) integers required"
323+
msgstr ""
324+
321325
#: py/compile.c
322326
msgid "*x must be assignment target"
323327
msgstr ""
@@ -4310,7 +4314,7 @@ msgid "unreadable attribute"
43104314
msgstr ""
43114315

43124316
#: shared-bindings/displayio/TileGrid.c shared-bindings/vectorio/VectorShape.c
4313-
#: shared-module/vectorio/Polygon.c
4317+
#: shared-module/vectorio/Polygon.c shared-module/vectorio/VectorShape.c
43144318
msgid "unsupported %q type"
43154319
msgstr ""
43164320

shared-bindings/displayio/Group.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const mp_obj_property_t displayio_group_y_obj = {
183183
MP_ROM_NONE},
184184
};
185185

186-
//| def append(self, layer: Union[vectorio.VectorShape, Group, TileGrid]) -> None:
186+
//| def append(self, layer: Union[vectorio.Circle, vectorio.Rectangle, vectorio.Polygon, Group, TileGrid]) -> None:
187187
//| """Append a layer to the group. It will be drawn above other layers."""
188188
//| ...
189189
//|
@@ -194,7 +194,7 @@ STATIC mp_obj_t displayio_group_obj_append(mp_obj_t self_in, mp_obj_t layer) {
194194
}
195195
MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_append_obj, displayio_group_obj_append);
196196

197-
//| def insert(self, index: int, layer: Union[vectorio.VectorShape, Group, TileGrid]) -> None:
197+
//| def insert(self, index: int, layer: Union[vectorio.Circle, vectorio.Rectangle, vectorio.Polygon, Group, TileGrid]) -> None:
198198
//| """Insert a layer into the group."""
199199
//| ...
200200
//|
@@ -210,7 +210,7 @@ STATIC mp_obj_t displayio_group_obj_insert(mp_obj_t self_in, mp_obj_t index_obj,
210210
MP_DEFINE_CONST_FUN_OBJ_3(displayio_group_insert_obj, displayio_group_obj_insert);
211211

212212

213-
//| def index(self, layer: Union[vectorio.VectorShape, Group, TileGrid]) -> int:
213+
//| def index(self, layer: Union[vectorio.Circle, vectorio.Rectangle, vectorio.Polygon, Group, TileGrid]) -> int:
214214
//| """Returns the index of the first copy of layer. Raises ValueError if not found."""
215215
//| ...
216216
//|
@@ -224,7 +224,7 @@ STATIC mp_obj_t displayio_group_obj_index(mp_obj_t self_in, mp_obj_t layer) {
224224
}
225225
MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_index_obj, displayio_group_obj_index);
226226

227-
//| def pop(self, i: int = -1) -> Union[vectorio.VectorShape, Group, TileGrid]:
227+
//| def pop(self, i: int = -1) -> Union[vectorio.Circle, vectorio.Rectangle, vectorio.Polygon, Group, TileGrid]:
228228
//| """Remove the ith item and return it."""
229229
//| ...
230230
//|
@@ -247,7 +247,7 @@ STATIC mp_obj_t displayio_group_obj_pop(size_t n_args, const mp_obj_t *pos_args,
247247
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_pop_obj, 1, displayio_group_obj_pop);
248248

249249

250-
//| def remove(self, layer: Union[vectorio.VectorShape, Group, TileGrid]) -> None:
250+
//| def remove(self, layer: Union[vectorio.Circle, vectorio.Rectangle, vectorio.Polygon, Group, TileGrid]) -> None:
251251
//| """Remove the first copy of layer. Raises ValueError if it is not present."""
252252
//| ...
253253
//|
@@ -280,15 +280,15 @@ STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
280280
}
281281
}
282282

283-
//| def __getitem__(self, index: int) -> Union[vectorio.VectorShape, Group, TileGrid]:
283+
//| def __getitem__(self, index: int) -> Union[vectorio.Circle, vectorio.Rectangle, vectorio.Polygon, Group, TileGrid]:
284284
//| """Returns the value at the given index.
285285
//|
286286
//| This allows you to::
287287
//|
288288
//| print(group[0])"""
289289
//| ...
290290
//|
291-
//| def __setitem__(self, index: int, value: Union[vectorio.VectorShape, Group, TileGrid]) -> None:
291+
//| def __setitem__(self, index: int, value: Union[vectorio.Circle, vectorio.Rectangle, vectorio.Polygon, Group, TileGrid]) -> None:
292292
//| """Sets the value at the given index.
293293
//|
294294
//| This allows you to::

shared-bindings/vectorio/Circle.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
1+
#include "shared-bindings/vectorio/__init__.h"
22
#include "shared-bindings/vectorio/Circle.h"
3+
#include "shared-bindings/vectorio/VectorShape.h"
34

45

56
#include <stdint.h>
@@ -11,15 +12,21 @@
1112

1213
//| class Circle:
1314
//|
14-
//| def __init__(self, radius: int) -> None:
15+
//| def __init__(self, pixel_shader: Union[displayio.ColorConverter, displayio.Palette], radius: int, x: int, y: int) -> None:
1516
//| """Circle is positioned on screen by its center point.
1617
//|
17-
//| :param radius: The radius of the circle in pixels"""
18+
//| :param pixel_shader: The pixel shader that produces colors from values
19+
//| :param radius: The radius of the circle in pixels
20+
//| :param x: Initial x position of the axis.
21+
//| :param y: Initial y position of the axis."""
1822
//|
1923
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) {
20-
enum { ARG_radius };
24+
enum { ARG_pixel_shader, ARG_radius, ARG_x, ARG_y };
2125
static const mp_arg_t allowed_args[] = {
26+
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
2227
{ MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT },
28+
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
29+
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
2330
};
2431
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
2532
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -33,9 +40,22 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg
3340
self->base.type = &vectorio_circle_type;
3441
common_hal_vectorio_circle_construct(self, radius);
3542

43+
// VectorShape parts
44+
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
45+
int16_t x = args[ARG_x].u_int;
46+
int16_t y = args[ARG_y].u_int;
47+
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
48+
self->draw_protocol_instance = vector_shape;
49+
3650
return MP_OBJ_FROM_PTR(self);
3751
}
3852

53+
STATIC const vectorio_draw_protocol_t circle_draw_protocol = {
54+
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw)
55+
.draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_circle_get_draw_protocol,
56+
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
57+
};
58+
3959

4060
//| radius : int
4161
//| """The radius of the circle in pixels."""
@@ -62,13 +82,22 @@ const mp_obj_property_t vectorio_circle_radius_obj = {
6282

6383

6484
STATIC const mp_rom_map_elem_t vectorio_circle_locals_dict_table[] = {
85+
// Properties
6586
{ MP_ROM_QSTR(MP_QSTR_radius), MP_ROM_PTR(&vectorio_circle_radius_obj) },
87+
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
88+
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
89+
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
90+
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
6691
};
6792
STATIC MP_DEFINE_CONST_DICT(vectorio_circle_locals_dict, vectorio_circle_locals_dict_table);
6893

6994
const mp_obj_type_t vectorio_circle_type = {
7095
{ &mp_type_type },
7196
.name = MP_QSTR_Circle,
97+
.flags = MP_TYPE_FLAG_EXTENDED,
7298
.make_new = vectorio_circle_make_new,
7399
.locals_dict = (mp_obj_dict_t *)&vectorio_circle_locals_dict,
100+
MP_TYPE_EXTENDED_FIELDS(
101+
.protocol = &circle_draw_protocol,
102+
),
74103
};

shared-bindings/vectorio/Circle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ void common_hal_vectorio_circle_get_area(void *circle, displayio_area_t *out_are
1919
int16_t common_hal_vectorio_circle_get_radius(void *circle);
2020
void common_hal_vectorio_circle_set_radius(void *circle, int16_t radius);
2121

22+
mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle);
23+
2224
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_CIRCLE_H

shared-bindings/vectorio/Polygon.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
1+
#include "shared-bindings/vectorio/__init__.h"
22
#include "shared-module/vectorio/__init__.h"
33
#include "shared-bindings/vectorio/Polygon.h"
4+
#include "shared-bindings/vectorio/VectorShape.h"
45

56
#include <stdint.h>
67

@@ -16,15 +17,21 @@
1617

1718

1819
//| class Polygon:
19-
//| def __init__(self, points: List[Tuple[int, int]]) -> None:
20+
//| def __init__(self, pixel_shader: Union[displayio.ColorConverter, displayio.Palette], points: List[Tuple[int, int]], x: int, y: int) -> None:
2021
//| """Represents a closed shape by ordered vertices
2122
//|
22-
//| :param points: Vertices for the polygon"""
23+
//| :param pixel_shader: The pixel shader that produces colors from values
24+
//| :param points: Vertices for the polygon
25+
//| :param x: Initial screen x position of the 0,0 origin in the points list.
26+
//| :param y: Initial screen y position of the 0,0 origin in the points list."""
2327
//|
2428
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) {
25-
enum { ARG_points_list };
29+
enum { ARG_pixel_shader, ARG_points_list, ARG_x, ARG_y };
2630
static const mp_arg_t allowed_args[] = {
31+
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
2732
{ MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
33+
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
34+
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
2835
};
2936
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
3037
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -38,9 +45,22 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar
3845

3946
common_hal_vectorio_polygon_construct(self, args[ARG_points_list].u_obj);
4047

48+
// VectorShape parts
49+
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
50+
int16_t x = args[ARG_x].u_int;
51+
int16_t y = args[ARG_y].u_int;
52+
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
53+
self->draw_protocol_instance = vector_shape;
54+
4155
return MP_OBJ_FROM_PTR(self);
4256
}
4357

58+
STATIC const vectorio_draw_protocol_t polygon_draw_protocol = {
59+
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw)
60+
.draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_polygon_get_draw_protocol,
61+
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
62+
};
63+
4464

4565
//| points: List[Tuple[int, int]]
4666
//| """Set a new look and shape for this polygon"""
@@ -67,13 +87,22 @@ const mp_obj_property_t vectorio_polygon_points_obj = {
6787
};
6888

6989
STATIC const mp_rom_map_elem_t vectorio_polygon_locals_dict_table[] = {
90+
// Properties
7091
{ MP_ROM_QSTR(MP_QSTR_points), MP_ROM_PTR(&vectorio_polygon_points_obj) },
92+
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
93+
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
94+
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
95+
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
7196
};
7297
STATIC MP_DEFINE_CONST_DICT(vectorio_polygon_locals_dict, vectorio_polygon_locals_dict_table);
7398

7499
const mp_obj_type_t vectorio_polygon_type = {
75100
{ &mp_type_type },
76101
.name = MP_QSTR_Polygon,
102+
.flags = MP_TYPE_FLAG_EXTENDED,
77103
.make_new = vectorio_polygon_make_new,
78104
.locals_dict = (mp_obj_dict_t *)&vectorio_polygon_locals_dict,
105+
MP_TYPE_EXTENDED_FIELDS(
106+
.protocol = &polygon_draw_protocol,
107+
),
79108
};

shared-bindings/vectorio/Polygon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ void common_hal_vectorio_polygon_get_area(void *polygon, displayio_area_t *out_a
2020
mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self);
2121
void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list);
2222

23+
mp_obj_t common_hal_vectorio_polygon_get_draw_protocol(void *polygon);
24+
2325

2426
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_POLYGON_H

shared-bindings/vectorio/Rectangle.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
1+
#include "shared-bindings/vectorio/__init__.h"
22
#include "shared-bindings/vectorio/Rectangle.h"
3+
#include "shared-module/vectorio/VectorShape.h"
4+
#include "shared-bindings/vectorio/VectorShape.h"
35

46
#include <stdint.h>
57

@@ -8,17 +10,23 @@
810
#include "supervisor/shared/translate.h"
911

1012
//| class Rectangle:
11-
//| def __init__(self, width: int, height: int) -> None:
13+
//| def __init__(self, pixel_shader: Union[displayio.ColorConverter, displayio.Palette], width: int, height: int, x: int, y: int) -> None:
1214
//| """Represents a rectangle by defining its bounds
1315
//|
16+
//| :param pixel_shader: The pixel shader that produces colors from values
1417
//| :param width: The number of pixels wide
15-
//| :param height: The number of pixels high"""
18+
//| :param height: The number of pixels high
19+
//| :param x: Initial x position of the top left corner.
20+
//| :param y: Initial y position of the top left corner."""
1621
//|
1722
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) {
18-
enum { ARG_width, ARG_height };
23+
enum { ARG_pixel_shader, ARG_width, ARG_height, ARG_x, ARG_y };
1924
static const mp_arg_t allowed_args[] = {
25+
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
2026
{ MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
2127
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
28+
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
29+
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
2230
};
2331
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
2432
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -36,17 +44,38 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_
3644
self->base.type = &vectorio_rectangle_type;
3745
common_hal_vectorio_rectangle_construct(self, width, height);
3846

47+
// VectorShape parts
48+
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
49+
int16_t x = args[ARG_x].u_int;
50+
int16_t y = args[ARG_y].u_int;
51+
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
52+
self->draw_protocol_instance = vector_shape;
53+
3954
return MP_OBJ_FROM_PTR(self);
4055
}
4156

57+
STATIC const vectorio_draw_protocol_t rectangle_draw_protocol = {
58+
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw)
59+
.draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_rectangle_get_draw_protocol,
60+
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
61+
};
4262

4363
STATIC const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = {
64+
// Properties
65+
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
66+
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
67+
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
68+
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
4469
};
4570
STATIC MP_DEFINE_CONST_DICT(vectorio_rectangle_locals_dict, vectorio_rectangle_locals_dict_table);
4671

4772
const mp_obj_type_t vectorio_rectangle_type = {
4873
{ &mp_type_type },
4974
.name = MP_QSTR_Rectangle,
75+
.flags = MP_TYPE_FLAG_EXTENDED,
5076
.make_new = vectorio_rectangle_make_new,
5177
.locals_dict = (mp_obj_dict_t *)&vectorio_rectangle_locals_dict,
78+
MP_TYPE_EXTENDED_FIELDS(
79+
.protocol = &rectangle_draw_protocol,
80+
),
5281
};

shared-bindings/vectorio/Rectangle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ uint32_t common_hal_vectorio_rectangle_get_pixel(void *rectangle, int16_t x, int
1212

1313
void common_hal_vectorio_rectangle_get_area(void *rectangle, displayio_area_t *out_area);
1414

15+
mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle);
16+
1517
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_RECTANGLE_H

0 commit comments

Comments
 (0)