From 90fadc58fdb0a47d2d85839607ed5d877ff356e9 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 18 Mar 2022 18:28:29 -0500 Subject: [PATCH] implement color_number argument for vectorio.Rectangle --- shared-bindings/vectorio/Rectangle.c | 33 +++++++++++++++++++++++++--- shared-bindings/vectorio/Rectangle.h | 4 +++- shared-module/vectorio/Rectangle.c | 18 +++++++++++++-- shared-module/vectorio/Rectangle.h | 1 + 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index 67367eb538..51cbad1cb8 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -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_number: Initial color_number 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_number }; 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_number, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} }, }; 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); @@ -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); + int32_t color_number = args[ARG_color_number].u_int; + common_hal_vectorio_rectangle_construct(self, width, height, color_number); // VectorShape parts mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; @@ -106,6 +109,29 @@ const mp_obj_property_t vectorio_rectangle_height_obj = { MP_ROM_NONE}, }; +//| color_number : int +//| """The color_number of the rectangle in 1 based index of the palette.""" +//| +STATIC mp_obj_t vectorio_rectangle_obj_get_color_number(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_number(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_get_color_number_obj, vectorio_rectangle_obj_get_color_number); + +STATIC mp_obj_t vectorio_rectangle_obj_set_color_number(mp_obj_t self_in, mp_obj_t color_number) { + vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_vectorio_rectangle_set_color_number(self, mp_obj_get_int(color_number)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_set_color_number_obj, vectorio_rectangle_obj_set_color_number); + +const mp_obj_property_t vectorio_rectangle_color_number_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&vectorio_rectangle_get_color_number_obj, + (mp_obj_t)&vectorio_rectangle_set_color_number_obj, + MP_ROM_NONE}, +}; + // Documentation for properties inherited from VectorShape. //| x : int @@ -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_number), MP_ROM_PTR(&vectorio_rectangle_color_number_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) }, diff --git a/shared-bindings/vectorio/Rectangle.h b/shared-bindings/vectorio/Rectangle.h index d50b811624..b9436d3930 100644 --- a/shared-bindings/vectorio/Rectangle.h +++ b/shared-bindings/vectorio/Rectangle.h @@ -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, uint32_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); @@ -18,6 +18,8 @@ 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); +int16_t common_hal_vectorio_rectangle_get_color_number(void *obj); +void common_hal_vectorio_rectangle_set_color_number(void *obj, int16_t color_number); int16_t common_hal_vectorio_rectangle_get_height(void *obj); void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height); diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c index 9092a2e078..040ec12334 100644 --- a/shared-module/vectorio/Rectangle.c +++ b/shared-module/vectorio/Rectangle.c @@ -6,9 +6,10 @@ #include "stdlib.h" -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, uint32_t color_number) { self->width = width; self->height = height; + self->color_number = color_number; } void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vectorio_event_t on_dirty) { @@ -21,7 +22,7 @@ void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vect uint32_t common_hal_vectorio_rectangle_get_pixel(void *obj, int16_t x, int16_t y) { vectorio_rectangle_t *self = obj; if (x >= 0 && y >= 0 && x < self->width && y < self->height) { - return 1; + return self->color_number; } return 0; } @@ -66,3 +67,16 @@ void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height) { self->on_dirty.event(self->on_dirty.obj); } } + +int16_t common_hal_vectorio_rectangle_get_color_number(void *obj) { + vectorio_rectangle_t *self = obj; + return self->color_number; +} + +void common_hal_vectorio_rectangle_set_color_number(void *obj, int16_t color_number) { + vectorio_rectangle_t *self = obj; + self->color_number = abs(color_number); + if (self->on_dirty.obj != NULL) { + self->on_dirty.event(self->on_dirty.obj); + } +} diff --git a/shared-module/vectorio/Rectangle.h b/shared-module/vectorio/Rectangle.h index ec6d2107be..464dbabda7 100644 --- a/shared-module/vectorio/Rectangle.h +++ b/shared-module/vectorio/Rectangle.h @@ -10,6 +10,7 @@ typedef struct { mp_obj_base_t base; uint16_t width; uint16_t height; + uint16_t color_number; vectorio_event_t on_dirty; mp_obj_t draw_protocol_instance; } vectorio_rectangle_t;