Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorio color index #6176

Merged
merged 5 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions shared-bindings/vectorio/Circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
//| :param Union[~displayio.ColorConverter,~displayio.Palette] pixel_shader: The pixel shader that produces colors from values
//| :param int radius: The radius of the circle in pixels
//| :param int x: Initial x position of the axis.
//| :param int y: Initial y position of the axis."""
//| :param int y: Initial y position of the axis.
//| :param int color_index: Initial color_index to use when selecting color from the palette."""
//|
static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_pixel_shader, ARG_radius, ARG_x, ARG_y };
enum { ARG_pixel_shader, ARG_radius, ARG_x, ARG_y, ARG_color_index };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
{ MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_color_index, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
Expand All @@ -38,7 +40,8 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg

vectorio_circle_t *self = m_new_obj(vectorio_circle_t);
self->base.type = &vectorio_circle_type;
common_hal_vectorio_circle_construct(self, radius);
uint16_t color_index = args[ARG_color_index].u_int;
common_hal_vectorio_circle_construct(self, radius, color_index);

// VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
Expand Down Expand Up @@ -80,6 +83,29 @@ const mp_obj_property_t vectorio_circle_radius_obj = {
MP_ROM_NONE},
};

//| color_index : int
//| """The color_index of the circle as 0 based index of the palette."""
//|
STATIC mp_obj_t vectorio_circle_obj_get_color_index(mp_obj_t self_in) {
vectorio_circle_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(common_hal_vectorio_circle_get_color_index(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_circle_get_color_index_obj, vectorio_circle_obj_get_color_index);

STATIC mp_obj_t vectorio_circle_obj_set_color_index(mp_obj_t self_in, mp_obj_t color_index) {
vectorio_circle_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_vectorio_circle_set_color_index(self, mp_obj_get_int(color_index));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_circle_set_color_index_obj, vectorio_circle_obj_set_color_index);

const mp_obj_property_t vectorio_circle_color_index_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&vectorio_circle_get_color_index_obj,
(mp_obj_t)&vectorio_circle_set_color_index_obj,
MP_ROM_NONE},
};


// Documentation for properties inherited from VectorShape.

Expand All @@ -103,6 +129,7 @@ STATIC const mp_rom_map_elem_t vectorio_circle_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_radius), MP_ROM_PTR(&vectorio_circle_radius_obj) },
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
{ MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_circle_color_index_obj) },
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
};
Expand Down
5 changes: 4 additions & 1 deletion shared-bindings/vectorio/Circle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

extern const mp_obj_type_t vectorio_circle_type;

void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius);
void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius, uint16_t color_index);

void common_hal_vectorio_circle_set_on_dirty(vectorio_circle_t *self, vectorio_event_t notification);

Expand All @@ -19,6 +19,9 @@ void common_hal_vectorio_circle_get_area(void *circle, displayio_area_t *out_are
int16_t common_hal_vectorio_circle_get_radius(void *circle);
void common_hal_vectorio_circle_set_radius(void *circle, int16_t radius);

uint16_t common_hal_vectorio_circle_get_color_index(void *obj);
void common_hal_vectorio_circle_set_color_index(void *obj, uint16_t color_index);

mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle);

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_CIRCLE_H
33 changes: 30 additions & 3 deletions shared-bindings/vectorio/Polygon.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
//| shader that produces colors from values
//| :param List[Tuple[int,int]] points: Vertices for the polygon
//| :param int x: Initial screen x position of the 0,0 origin in the points list.
//| :param int y: Initial screen y position of the 0,0 origin in the points list."""
//| :param int y: Initial screen y position of the 0,0 origin in the points list.
//| :param int color_index: Initial color_index to use when selecting color from the palette."""
//|
gamblor21 marked this conversation as resolved.
Show resolved Hide resolved
static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_pixel_shader, ARG_points_list, ARG_x, ARG_y };
enum { ARG_pixel_shader, ARG_points_list, ARG_x, ARG_y, ARG_color_index };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
{ MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_color_index, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
Expand All @@ -43,7 +45,8 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar
vectorio_polygon_t *self = m_new_obj(vectorio_polygon_t);
self->base.type = &vectorio_polygon_type;

common_hal_vectorio_polygon_construct(self, points_list);
uint16_t color_index = args[ARG_color_index].u_int;
common_hal_vectorio_polygon_construct(self, points_list, color_index);

// VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
Expand Down Expand Up @@ -86,6 +89,29 @@ const mp_obj_property_t vectorio_polygon_points_obj = {
MP_ROM_NONE},
};

//| color_index : int
//| """The color_index of the polygon as 0 based index of the palette."""
//|
STATIC mp_obj_t vectorio_polygon_obj_get_color_index(mp_obj_t self_in) {
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(common_hal_vectorio_polygon_get_color_index(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_polygon_get_color_index_obj, vectorio_polygon_obj_get_color_index);

STATIC mp_obj_t vectorio_polygon_obj_set_color_index(mp_obj_t self_in, mp_obj_t color_index) {
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_vectorio_polygon_set_color_index(self, mp_obj_get_int(color_index));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_polygon_set_color_index_obj, vectorio_polygon_obj_set_color_index);

const mp_obj_property_t vectorio_polygon_color_index_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&vectorio_polygon_get_color_index_obj,
(mp_obj_t)&vectorio_polygon_set_color_index_obj,
MP_ROM_NONE},
};


// Documentation for properties inherited from VectorShape.

Expand All @@ -109,6 +135,7 @@ 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) },
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
{ MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_polygon_color_index_obj) },
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
};
Expand Down
5 changes: 4 additions & 1 deletion shared-bindings/vectorio/Polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

extern const mp_obj_type_t vectorio_polygon_type;

void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list);
void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list, uint16_t color_index);
void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio_event_t notification);


Expand All @@ -20,6 +20,9 @@ void common_hal_vectorio_polygon_get_area(void *polygon, displayio_area_t *out_a
mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self);
void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list);

uint16_t common_hal_vectorio_polygon_get_color_index(void *obj);
void common_hal_vectorio_polygon_set_color_index(void *obj, uint16_t color_index);

mp_obj_t common_hal_vectorio_polygon_get_draw_protocol(void *polygon);


Expand Down
33 changes: 30 additions & 3 deletions shared-bindings/vectorio/Rectangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
//| :param int width: The number of pixels wide
//| :param int height: The number of pixels high
//| :param int x: Initial x position of the top left corner.
//| :param int y: Initial y position of the top left corner."""
//| :param int y: Initial y position of the top left corner.
//| :param int color_index: Initial color_index to use when selecting color from the palette."""
//|
static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_pixel_shader, ARG_width, ARG_height, ARG_x, ARG_y };
enum { ARG_pixel_shader, ARG_width, ARG_height, ARG_x, ARG_y, ARG_color_index };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
{ MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_color_index, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
Expand All @@ -42,7 +44,8 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_

vectorio_rectangle_t *self = m_new_obj(vectorio_rectangle_t);
self->base.type = &vectorio_rectangle_type;
common_hal_vectorio_rectangle_construct(self, width, height);
uint16_t color_index = args[ARG_color_index].u_int;
common_hal_vectorio_rectangle_construct(self, width, height, color_index);

// VectorShape parts
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
Expand Down Expand Up @@ -106,6 +109,29 @@ const mp_obj_property_t vectorio_rectangle_height_obj = {
MP_ROM_NONE},
};

//| color_index : int
//| """The color_index of the rectangle in 1 based index of the palette."""
//|
STATIC mp_obj_t vectorio_rectangle_obj_get_color_index(mp_obj_t self_in) {
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(common_hal_vectorio_rectangle_get_color_index(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_get_color_index_obj, vectorio_rectangle_obj_get_color_index);

STATIC mp_obj_t vectorio_rectangle_obj_set_color_index(mp_obj_t self_in, mp_obj_t color_index) {
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_vectorio_rectangle_set_color_index(self, mp_obj_get_int(color_index));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_set_color_index_obj, vectorio_rectangle_obj_set_color_index);

const mp_obj_property_t vectorio_rectangle_color_index_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&vectorio_rectangle_get_color_index_obj,
(mp_obj_t)&vectorio_rectangle_set_color_index_obj,
MP_ROM_NONE},
};

// Documentation for properties inherited from VectorShape.

//| x : int
Expand All @@ -127,6 +153,7 @@ STATIC const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = {
// Properties
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
{ MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_rectangle_color_index_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
Expand Down
5 changes: 4 additions & 1 deletion shared-bindings/vectorio/Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

extern const mp_obj_type_t vectorio_rectangle_type;

void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height);
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height, uint16_t color_index);
void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vectorio_event_t on_dirty);

uint32_t common_hal_vectorio_rectangle_get_pixel(void *rectangle, int16_t x, int16_t y);
Expand All @@ -19,6 +19,9 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle);
int16_t common_hal_vectorio_rectangle_get_width(void *obj);
void common_hal_vectorio_rectangle_set_width(void *obj, int16_t width);

uint16_t common_hal_vectorio_rectangle_get_color_index(void *obj);
void common_hal_vectorio_rectangle_set_color_index(void *obj, uint16_t color_index);

int16_t common_hal_vectorio_rectangle_get_height(void *obj);
void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height);

Expand Down
20 changes: 17 additions & 3 deletions shared-module/vectorio/Circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include "stdlib.h"


void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius) {
void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius, uint16_t color_index) {
self->radius = radius;
self->on_dirty.obj = NULL;
self->color_index = color_index + 1;
}

void common_hal_vectorio_circle_set_on_dirty(vectorio_circle_t *self, vectorio_event_t on_dirty) {
Expand All @@ -26,7 +27,7 @@ uint32_t common_hal_vectorio_circle_get_pixel(void *obj, int16_t x, int16_t y) {
x = abs(x);
y = abs(y);
if (x + y <= radius) {
return 1;
return self->color_index;
}
if (x > radius) {
return 0;
Expand All @@ -35,7 +36,7 @@ uint32_t common_hal_vectorio_circle_get_pixel(void *obj, int16_t x, int16_t y) {
return 0;
}
const bool pythagorasSmallerThanRadius = (int32_t)x * x + (int32_t)y * y <= (int32_t)radius * radius;
return pythagorasSmallerThanRadius ? 1 : 0;
return pythagorasSmallerThanRadius ? self->color_index : 0;
}


Expand All @@ -60,6 +61,19 @@ void common_hal_vectorio_circle_set_radius(void *obj, int16_t radius) {
}
}

uint16_t common_hal_vectorio_circle_get_color_index(void *obj) {
vectorio_circle_t *self = obj;
return self->color_index - 1;
}

void common_hal_vectorio_circle_set_color_index(void *obj, uint16_t color_index) {
vectorio_circle_t *self = obj;
self->color_index = abs(color_index + 1);
if (self->on_dirty.obj != NULL) {
self->on_dirty.event(self->on_dirty.obj);
}
}

mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle) {
vectorio_circle_t *self = circle;
return self->draw_protocol_instance;
Expand Down
1 change: 1 addition & 0 deletions shared-module/vectorio/Circle.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
typedef struct {
mp_obj_base_t base;
uint16_t radius;
uint16_t color_index;
vectorio_event_t on_dirty;
mp_obj_t draw_protocol_instance;
} vectorio_circle_t;
Expand Down
18 changes: 16 additions & 2 deletions shared-module/vectorio/Polygon.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple



void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list) {
void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list, uint16_t color_index) {
VECTORIO_POLYGON_DEBUG("%p polygon_construct: ", self);
self->points_list = NULL;
self->len = 0;
self->on_dirty.obj = NULL;
self->color_index = color_index + 1;
_clobber_points_list(self, points_list);
VECTORIO_POLYGON_DEBUG("\n");
}
Expand Down Expand Up @@ -181,10 +182,23 @@ uint32_t common_hal_vectorio_polygon_get_pixel(void *obj, int16_t x, int16_t y)
x1 = x2;
y1 = y2;
}
return winding_number == 0 ? 0 : 1;
return winding_number == 0 ? 0 : self->color_index;
}

mp_obj_t common_hal_vectorio_polygon_get_draw_protocol(void *polygon) {
vectorio_polygon_t *self = polygon;
return self->draw_protocol_instance;
}

uint16_t common_hal_vectorio_polygon_get_color_index(void *obj) {
vectorio_polygon_t *self = obj;
return self->color_index - 1;
}

void common_hal_vectorio_polygon_set_color_index(void *obj, uint16_t color_index) {
vectorio_polygon_t *self = obj;
self->color_index = abs(color_index + 1);
if (self->on_dirty.obj != NULL) {
self->on_dirty.event(self->on_dirty.obj);
}
}
1 change: 1 addition & 0 deletions shared-module/vectorio/Polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ typedef struct {
// An int array[ x, y, ... ]
int16_t *points_list;
uint16_t len;
uint16_t color_index;
vectorio_event_t on_dirty;
mp_obj_t draw_protocol_instance;
} vectorio_polygon_t;
Expand Down
Loading