Skip to content

Commit 90fadc5

Browse files
committed
implement color_number argument for vectorio.Rectangle
1 parent 047afab commit 90fadc5

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

shared-bindings/vectorio/Rectangle.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
//| :param int width: The number of pixels wide
1818
//| :param int height: The number of pixels high
1919
//| :param int x: Initial x position of the top left corner.
20-
//| :param int y: Initial y position of the top left corner."""
20+
//| :param int y: Initial y position of the top left corner.
21+
//| :param int color_number: Initial color_number to use when selecting color from the palette."""
2122
//|
2223
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) {
23-
enum { ARG_pixel_shader, ARG_width, ARG_height, ARG_x, ARG_y };
24+
enum { ARG_pixel_shader, ARG_width, ARG_height, ARG_x, ARG_y, ARG_color_number };
2425
static const mp_arg_t allowed_args[] = {
2526
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
2627
{ MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
2728
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
2829
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
2930
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
31+
{ MP_QSTR_color_number, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
3032
};
3133
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
3234
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_
4244

4345
vectorio_rectangle_t *self = m_new_obj(vectorio_rectangle_t);
4446
self->base.type = &vectorio_rectangle_type;
45-
common_hal_vectorio_rectangle_construct(self, width, height);
47+
int32_t color_number = args[ARG_color_number].u_int;
48+
common_hal_vectorio_rectangle_construct(self, width, height, color_number);
4649

4750
// VectorShape parts
4851
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
@@ -106,6 +109,29 @@ const mp_obj_property_t vectorio_rectangle_height_obj = {
106109
MP_ROM_NONE},
107110
};
108111

112+
//| color_number : int
113+
//| """The color_number of the rectangle in 1 based index of the palette."""
114+
//|
115+
STATIC mp_obj_t vectorio_rectangle_obj_get_color_number(mp_obj_t self_in) {
116+
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
117+
return mp_obj_new_int(common_hal_vectorio_rectangle_get_color_number(self));
118+
}
119+
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_get_color_number_obj, vectorio_rectangle_obj_get_color_number);
120+
121+
STATIC mp_obj_t vectorio_rectangle_obj_set_color_number(mp_obj_t self_in, mp_obj_t color_number) {
122+
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
123+
common_hal_vectorio_rectangle_set_color_number(self, mp_obj_get_int(color_number));
124+
return mp_const_none;
125+
}
126+
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_set_color_number_obj, vectorio_rectangle_obj_set_color_number);
127+
128+
const mp_obj_property_t vectorio_rectangle_color_number_obj = {
129+
.base.type = &mp_type_property,
130+
.proxy = {(mp_obj_t)&vectorio_rectangle_get_color_number_obj,
131+
(mp_obj_t)&vectorio_rectangle_set_color_number_obj,
132+
MP_ROM_NONE},
133+
};
134+
109135
// Documentation for properties inherited from VectorShape.
110136

111137
//| x : int
@@ -127,6 +153,7 @@ STATIC const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = {
127153
// Properties
128154
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
129155
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
156+
{ MP_ROM_QSTR(MP_QSTR_color_number), MP_ROM_PTR(&vectorio_rectangle_color_number_obj) },
130157
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) },
131158
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) },
132159
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },

shared-bindings/vectorio/Rectangle.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
extern const mp_obj_type_t vectorio_rectangle_type;
99

10-
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height);
10+
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height, uint32_t color_index);
1111
void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vectorio_event_t on_dirty);
1212

1313
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);
1818

1919
int16_t common_hal_vectorio_rectangle_get_width(void *obj);
2020
void common_hal_vectorio_rectangle_set_width(void *obj, int16_t width);
21+
int16_t common_hal_vectorio_rectangle_get_color_number(void *obj);
22+
void common_hal_vectorio_rectangle_set_color_number(void *obj, int16_t color_number);
2123

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

shared-module/vectorio/Rectangle.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
#include "stdlib.h"
77

88

9-
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height) {
9+
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height, uint32_t color_number) {
1010
self->width = width;
1111
self->height = height;
12+
self->color_number = color_number;
1213
}
1314

1415
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
2122
uint32_t common_hal_vectorio_rectangle_get_pixel(void *obj, int16_t x, int16_t y) {
2223
vectorio_rectangle_t *self = obj;
2324
if (x >= 0 && y >= 0 && x < self->width && y < self->height) {
24-
return 1;
25+
return self->color_number;
2526
}
2627
return 0;
2728
}
@@ -66,3 +67,16 @@ void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height) {
6667
self->on_dirty.event(self->on_dirty.obj);
6768
}
6869
}
70+
71+
int16_t common_hal_vectorio_rectangle_get_color_number(void *obj) {
72+
vectorio_rectangle_t *self = obj;
73+
return self->color_number;
74+
}
75+
76+
void common_hal_vectorio_rectangle_set_color_number(void *obj, int16_t color_number) {
77+
vectorio_rectangle_t *self = obj;
78+
self->color_number = abs(color_number);
79+
if (self->on_dirty.obj != NULL) {
80+
self->on_dirty.event(self->on_dirty.obj);
81+
}
82+
}

shared-module/vectorio/Rectangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct {
1010
mp_obj_base_t base;
1111
uint16_t width;
1212
uint16_t height;
13+
uint16_t color_number;
1314
vectorio_event_t on_dirty;
1415
mp_obj_t draw_protocol_instance;
1516
} vectorio_rectangle_t;

0 commit comments

Comments
 (0)