From 1f446916c3e053e33d389adf7a7cc90fb992d64f Mon Sep 17 00:00:00 2001 From: James Carr Date: Fri, 10 Sep 2021 09:24:33 +0100 Subject: [PATCH 1/5] Improve the bounds checking on the location (the x, y co-ordinates) of a VectorShape object so that it is consistent no matter where it is set from: * the constructor * the x and y setters * the location setter --- shared-bindings/vectorio/Circle.c | 4 +-- shared-bindings/vectorio/Polygon.c | 4 +-- shared-bindings/vectorio/Rectangle.c | 4 +-- shared-bindings/vectorio/VectorShape.c | 12 ++++++--- shared-bindings/vectorio/VectorShape.h | 8 +++--- shared-module/vectorio/VectorShape.c | 36 +++++++++++++++----------- 6 files changed, 40 insertions(+), 28 deletions(-) diff --git a/shared-bindings/vectorio/Circle.c b/shared-bindings/vectorio/Circle.c index c0cc0bbcc2..cdf8965e75 100644 --- a/shared-bindings/vectorio/Circle.c +++ b/shared-bindings/vectorio/Circle.c @@ -42,8 +42,8 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg // VectorShape parts mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; - int16_t x = args[ARG_x].u_int; - int16_t y = args[ARG_y].u_int; + int32_t x = args[ARG_x].u_int; + int32_t y = args[ARG_y].u_int; mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y); self->draw_protocol_instance = vector_shape; diff --git a/shared-bindings/vectorio/Polygon.c b/shared-bindings/vectorio/Polygon.c index 5d493a30b6..17d293c7a3 100644 --- a/shared-bindings/vectorio/Polygon.c +++ b/shared-bindings/vectorio/Polygon.c @@ -47,8 +47,8 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar // VectorShape parts mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; - int16_t x = args[ARG_x].u_int; - int16_t y = args[ARG_y].u_int; + int32_t x = args[ARG_x].u_int; + int32_t y = args[ARG_y].u_int; mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y); self->draw_protocol_instance = vector_shape; diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index aa2c166a6d..c6f45874bd 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -46,8 +46,8 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_ // VectorShape parts mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj; - int16_t x = args[ARG_x].u_int; - int16_t y = args[ARG_y].u_int; + int32_t x = args[ARG_x].u_int; + int32_t y = args[ARG_y].u_int; mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y); self->draw_protocol_instance = vector_shape; diff --git a/shared-bindings/vectorio/VectorShape.c b/shared-bindings/vectorio/VectorShape.c index 6e4299a6d3..51c2679b83 100644 --- a/shared-bindings/vectorio/VectorShape.c +++ b/shared-bindings/vectorio/VectorShape.c @@ -23,7 +23,7 @@ // pixel_shader: The pixel shader that produces colors from values. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0. // x: Initial x position of the center axis of the shape within the parent. // y: Initial y position of the center axis of the shape within the parent.""" -mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y) { +mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int32_t x, int32_t y) { if (!mp_obj_is_type(pixel_shader, &displayio_colorconverter_type) && !mp_obj_is_type(pixel_shader, &displayio_palette_type)) { mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_pixel_shader); @@ -99,7 +99,10 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); mp_int_t x = mp_obj_get_int(x_obj); - common_hal_vectorio_vector_shape_set_x(self, x); + bool dirty = common_hal_vectorio_vector_shape_set_x(self, x); + if (dirty) { + common_hal_vectorio_vector_shape_set_dirty(self); + } return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_x_obj, vectorio_vector_shape_obj_set_x); @@ -130,7 +133,10 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); mp_int_t y = mp_obj_get_int(y_obj); - common_hal_vectorio_vector_shape_set_y(self, y); + bool dirty = common_hal_vectorio_vector_shape_set_y(self, y); + if (dirty) { + common_hal_vectorio_vector_shape_set_dirty(self); + } return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_y_obj, vectorio_vector_shape_obj_set_y); diff --git a/shared-bindings/vectorio/VectorShape.h b/shared-bindings/vectorio/VectorShape.h index 9ac6906000..d11384e3eb 100644 --- a/shared-bindings/vectorio/VectorShape.h +++ b/shared-bindings/vectorio/VectorShape.h @@ -11,23 +11,23 @@ extern const mp_obj_type_t vectorio_vector_shape_type; // Python shared bindings constructor -mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y); +mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int32_t x, int32_t y); // C data constructor void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, vectorio_ishape_t ishape, - mp_obj_t pixel_shader, uint16_t x, uint16_t y); + mp_obj_t pixel_shader, int32_t x, int32_t y); void common_hal_vectorio_vector_shape_set_dirty(void *self); mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self); -void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x); +bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x); mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self); void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self, mp_obj_t xy); mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self); -void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y); +bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y); mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self); void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader); diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c index 9d0279cf14..eae2ea57b4 100644 --- a/shared-module/vectorio/VectorShape.c +++ b/shared-module/vectorio/VectorShape.c @@ -164,10 +164,10 @@ void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) { void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, vectorio_ishape_t ishape, - mp_obj_t pixel_shader, uint16_t x, uint16_t y) { + mp_obj_t pixel_shader, int32_t x, int32_t y) { VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y); - self->x = x; - self->y = y; + common_hal_vectorio_vector_shape_set_x(self, x); + common_hal_vectorio_vector_shape_set_y(self, y); self->pixel_shader = pixel_shader; self->ishape = ishape; self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area. @@ -184,13 +184,16 @@ mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) { } -void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) { +bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) { VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x); if (self->x == x) { - return; + return false; // it's not dirty + } + if (x < SHRT_MIN || x > SHRT_MAX) { + mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); } self->x = x; - common_hal_vectorio_vector_shape_set_dirty(self); + return true; // it's dirty } @@ -200,13 +203,16 @@ mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) { } -void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) { +bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) { VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y); if (self->y == y) { - return; + return false; // it's not dirty + } + if (y < SHRT_MIN || y > SHRT_MAX) { + mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); } self->y = y; - common_hal_vectorio_vector_shape_set_dirty(self); + return true; // it's dirty } mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self) { @@ -230,14 +236,14 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self mp_int_t x; mp_int_t y; if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x) - || !mp_obj_get_int_maybe(tuple_items[ 1 ], &y) - || x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX - ) { + || !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) { mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); } - self->x = (int16_t)x; - self->y = (int16_t)y; - common_hal_vectorio_vector_shape_set_dirty(self); + bool dirty = common_hal_vectorio_vector_shape_set_x(self, x); + dirty |= common_hal_vectorio_vector_shape_set_y(self, y); + if (dirty) { + common_hal_vectorio_vector_shape_set_dirty(self); + } } From c6f2dae5912faf7bb298aa62070ca6d835d5ddda Mon Sep 17 00:00:00 2001 From: James Carr Date: Fri, 10 Sep 2021 09:34:20 +0100 Subject: [PATCH 2/5] Remove unused varg part of Error calls. --- shared-module/vectorio/Polygon.c | 2 +- shared-module/vectorio/VectorShape.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-module/vectorio/Polygon.c b/shared-module/vectorio/Polygon.c index f7199c7f83..f0b241e351 100644 --- a/shared-module/vectorio/Polygon.c +++ b/shared-module/vectorio/Polygon.c @@ -22,7 +22,7 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple VECTORIO_POLYGON_DEBUG(" self.len: %d, len: %d, ", self->len, len); if (len < 3) { - mp_raise_TypeError_varg(translate("Polygon needs at least 3 points")); + mp_raise_TypeError(translate("Polygon needs at least 3 points")); } if (self->len < 2 * len) { diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c index eae2ea57b4..8fc62c43c5 100644 --- a/shared-module/vectorio/VectorShape.c +++ b/shared-module/vectorio/VectorShape.c @@ -230,7 +230,7 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self mp_obj_t *tuple_items; mp_obj_tuple_get(xy, &tuple_len, &tuple_items); if (tuple_len != 2) { - mp_raise_TypeError_varg(translate("(x,y) integers required")); + mp_raise_TypeError(translate("(x,y) integers required")); } mp_int_t x; From aa1d089cdb87a832d5cc46b2ca83aee62a2119e5 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 10 Sep 2021 14:50:09 -0400 Subject: [PATCH 3/5] proxlight: Freeze adafruit_adps9960 instead of adafruit_hid; enable usb_midi --- .gitmodules | 3 +++ frozen/Adafruit_CircuitPython_APDS9960 | 1 + .../boards/adafruit_proxlight_trinkey_m0/mpconfigboard.mk | 3 +-- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 160000 frozen/Adafruit_CircuitPython_APDS9960 diff --git a/.gitmodules b/.gitmodules index d1abf760d0..6089411280 100644 --- a/.gitmodules +++ b/.gitmodules @@ -191,3 +191,6 @@ [submodule "lib/quirc"] path = lib/quirc url = https://github.com/adafruit/quirc.git +[submodule "frozen/Adafruit_CircuitPython_APDS9960"] + path = frozen/Adafruit_CircuitPython_APDS9960 + url = https://github.com/adafruit/Adafruit_CircuitPython_APDS9960 diff --git a/frozen/Adafruit_CircuitPython_APDS9960 b/frozen/Adafruit_CircuitPython_APDS9960 new file mode 160000 index 0000000000..ee411d34df --- /dev/null +++ b/frozen/Adafruit_CircuitPython_APDS9960 @@ -0,0 +1 @@ +Subproject commit ee411d34dfa2fb70a35aa99945eca77f16456619 diff --git a/ports/atmel-samd/boards/adafruit_proxlight_trinkey_m0/mpconfigboard.mk b/ports/atmel-samd/boards/adafruit_proxlight_trinkey_m0/mpconfigboard.mk index 10a2f4ed37..688d3c0db4 100644 --- a/ports/atmel-samd/boards/adafruit_proxlight_trinkey_m0/mpconfigboard.mk +++ b/ports/atmel-samd/boards/adafruit_proxlight_trinkey_m0/mpconfigboard.mk @@ -19,7 +19,6 @@ CIRCUITPY_PULSEIO = 0 CIRCUITPY_PWMIO = 0 CIRCUITPY_ROTARYIO = 0 CIRCUITPY_RTC = 0 -CIRCUITPY_USB_MIDI = 0 CIRCUITPY_GETPASS = 0 CIRCUITPY_TRACEBACK = 0 @@ -28,5 +27,5 @@ CIRCUITPY_PIXELBUF = 1 CIRCUITPY_BUSDEVICE = 1 # Include these Python libraries in firmware. -FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_APDS9960 FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel From 2bc260a102fd5943352f257ff2bbf22cbe6e2368 Mon Sep 17 00:00:00 2001 From: James Carr Date: Fri, 10 Sep 2021 21:48:01 +0100 Subject: [PATCH 4/5] Rework of changes to bounds checking of location in VectorShape, moving most of the code into shared-module. --- shared-bindings/vectorio/VectorShape.c | 10 +---- shared-bindings/vectorio/VectorShape.h | 4 +- shared-module/vectorio/VectorShape.c | 53 ++++++++++++++++++-------- shared-module/vectorio/VectorShape.h | 3 ++ 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/shared-bindings/vectorio/VectorShape.c b/shared-bindings/vectorio/VectorShape.c index 51c2679b83..c9cbf0a114 100644 --- a/shared-bindings/vectorio/VectorShape.c +++ b/shared-bindings/vectorio/VectorShape.c @@ -99,10 +99,7 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); mp_int_t x = mp_obj_get_int(x_obj); - bool dirty = common_hal_vectorio_vector_shape_set_x(self, x); - if (dirty) { - common_hal_vectorio_vector_shape_set_dirty(self); - } + common_hal_vectorio_vector_shape_set_x(self, x); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_x_obj, vectorio_vector_shape_obj_set_x); @@ -133,10 +130,7 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); mp_int_t y = mp_obj_get_int(y_obj); - bool dirty = common_hal_vectorio_vector_shape_set_y(self, y); - if (dirty) { - common_hal_vectorio_vector_shape_set_dirty(self); - } + common_hal_vectorio_vector_shape_set_y(self, y); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_y_obj, vectorio_vector_shape_obj_set_y); diff --git a/shared-bindings/vectorio/VectorShape.h b/shared-bindings/vectorio/VectorShape.h index d11384e3eb..12c7e628ad 100644 --- a/shared-bindings/vectorio/VectorShape.h +++ b/shared-bindings/vectorio/VectorShape.h @@ -21,13 +21,13 @@ void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, void common_hal_vectorio_vector_shape_set_dirty(void *self); mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self); -bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x); +void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x); mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self); void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self, mp_obj_t xy); mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self); -bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y); +void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y); mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self); void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader); diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c index 8fc62c43c5..d90e2715b3 100644 --- a/shared-module/vectorio/VectorShape.c +++ b/shared-module/vectorio/VectorShape.c @@ -166,8 +166,10 @@ void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, vectorio_ishape_t ishape, mp_obj_t pixel_shader, int32_t x, int32_t y) { VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y); - common_hal_vectorio_vector_shape_set_x(self, x); - common_hal_vectorio_vector_shape_set_y(self, y); + vectorio_vector_shape_validate_x_bounds(x); + self->x = x; + vectorio_vector_shape_validate_y_bounds(y); + self->y = y; self->pixel_shader = pixel_shader; self->ishape = ishape; self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area. @@ -184,16 +186,14 @@ mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) { } -bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) { +void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) { VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x); if (self->x == x) { - return false; // it's not dirty - } - if (x < SHRT_MIN || x > SHRT_MAX) { - mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); + return; } + vectorio_vector_shape_validate_x_bounds(x); self->x = x; - return true; // it's dirty + common_hal_vectorio_vector_shape_set_dirty(self); } @@ -203,16 +203,14 @@ mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) { } -bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) { +void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) { VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y); if (self->y == y) { - return false; // it's not dirty - } - if (y < SHRT_MIN || y > SHRT_MAX) { - mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); + return; } + vectorio_vector_shape_validate_y_bounds(y); self->y = y; - return true; // it's dirty + common_hal_vectorio_vector_shape_set_dirty(self); } mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self) { @@ -239,14 +237,37 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self || !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) { mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); } - bool dirty = common_hal_vectorio_vector_shape_set_x(self, x); - dirty |= common_hal_vectorio_vector_shape_set_y(self, y); + bool dirty = false; + if (self->x != x) { + vectorio_vector_shape_validate_x_bounds(x); + self->x = x; + dirty = true; + } + if (self->y != y) { + vectorio_vector_shape_validate_y_bounds(y); + self->y = y; + dirty = true; + } if (dirty) { common_hal_vectorio_vector_shape_set_dirty(self); } } +void vectorio_vector_shape_validate_x_bounds(mp_int_t x) { + if (x < SHRT_MIN || x > SHRT_MAX) { + mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_x, SHRT_MIN, SHRT_MAX); + } +} + + +void vectorio_vector_shape_validate_y_bounds(mp_int_t y) { + if (y < SHRT_MIN || y > SHRT_MAX) { + mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_y, SHRT_MIN, SHRT_MAX); + } +} + + mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self) { VECTORIO_SHAPE_DEBUG("%p get_pixel_shader\n", self); return self->pixel_shader; diff --git a/shared-module/vectorio/VectorShape.h b/shared-module/vectorio/VectorShape.h index fdbae964a8..cb12590e35 100644 --- a/shared-module/vectorio/VectorShape.h +++ b/shared-module/vectorio/VectorShape.h @@ -51,4 +51,7 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ bool vectorio_vector_shape_get_previous_area(vectorio_vector_shape_t *self, displayio_area_t *out_area); void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self); +void vectorio_vector_shape_validate_x_bounds(mp_int_t x); +void vectorio_vector_shape_validate_y_bounds(mp_int_t y); + #endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H From 145836e7e5160eaba68b91712b6c083d8b7dec84 Mon Sep 17 00:00:00 2001 From: James Carr Date: Sat, 11 Sep 2021 17:10:21 +0100 Subject: [PATCH 5/5] Make the x and y bounds checking functions static --- shared-module/vectorio/VectorShape.c | 50 +++++++++++++--------------- shared-module/vectorio/VectorShape.h | 3 -- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c index d90e2715b3..7899e2710c 100644 --- a/shared-module/vectorio/VectorShape.c +++ b/shared-module/vectorio/VectorShape.c @@ -125,6 +125,24 @@ static void _get_screen_area(vectorio_vector_shape_t *self, displayio_area_t *ou } +STATIC +void check_bounds_and_set_x(vectorio_vector_shape_t *self, mp_int_t x) { + if (x < SHRT_MIN || x > SHRT_MAX) { + mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_x, SHRT_MIN, SHRT_MAX); + } + self->x = x; +} + + +STATIC +void check_bounds_and_set_y(vectorio_vector_shape_t *self, mp_int_t y) { + if (y < SHRT_MIN || y > SHRT_MAX) { + mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_y, SHRT_MIN, SHRT_MAX); + } + self->y = y; +} + + // For use by Group to know where it needs to redraw on layer removal. bool vectorio_vector_shape_get_dirty_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) { out_area->x1 = out_area->x2; @@ -166,10 +184,8 @@ void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, vectorio_ishape_t ishape, mp_obj_t pixel_shader, int32_t x, int32_t y) { VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y); - vectorio_vector_shape_validate_x_bounds(x); - self->x = x; - vectorio_vector_shape_validate_y_bounds(y); - self->y = y; + check_bounds_and_set_x(self, x); + check_bounds_and_set_y(self, y); self->pixel_shader = pixel_shader; self->ishape = ishape; self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area. @@ -191,8 +207,7 @@ void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_in if (self->x == x) { return; } - vectorio_vector_shape_validate_x_bounds(x); - self->x = x; + check_bounds_and_set_x(self, x); common_hal_vectorio_vector_shape_set_dirty(self); } @@ -208,8 +223,7 @@ void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_in if (self->y == y) { return; } - vectorio_vector_shape_validate_y_bounds(y); - self->y = y; + check_bounds_and_set_y(self, y); common_hal_vectorio_vector_shape_set_dirty(self); } @@ -239,13 +253,11 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self } bool dirty = false; if (self->x != x) { - vectorio_vector_shape_validate_x_bounds(x); - self->x = x; + check_bounds_and_set_x(self, x); dirty = true; } if (self->y != y) { - vectorio_vector_shape_validate_y_bounds(y); - self->y = y; + check_bounds_and_set_y(self, y); dirty = true; } if (dirty) { @@ -254,20 +266,6 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self } -void vectorio_vector_shape_validate_x_bounds(mp_int_t x) { - if (x < SHRT_MIN || x > SHRT_MAX) { - mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_x, SHRT_MIN, SHRT_MAX); - } -} - - -void vectorio_vector_shape_validate_y_bounds(mp_int_t y) { - if (y < SHRT_MIN || y > SHRT_MAX) { - mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_y, SHRT_MIN, SHRT_MAX); - } -} - - mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self) { VECTORIO_SHAPE_DEBUG("%p get_pixel_shader\n", self); return self->pixel_shader; diff --git a/shared-module/vectorio/VectorShape.h b/shared-module/vectorio/VectorShape.h index cb12590e35..fdbae964a8 100644 --- a/shared-module/vectorio/VectorShape.h +++ b/shared-module/vectorio/VectorShape.h @@ -51,7 +51,4 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ bool vectorio_vector_shape_get_previous_area(vectorio_vector_shape_t *self, displayio_area_t *out_area); void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self); -void vectorio_vector_shape_validate_x_bounds(mp_int_t x); -void vectorio_vector_shape_validate_y_bounds(mp_int_t y); - #endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H