2121#include <pbio/image.h>
2222#include <pbio/int_math.h>
2323
24+ #include <pbsys/light.h>
25+
2426#include <pbdrv/display.h>
2527
2628extern const mp_obj_type_t pb_type_Image ;
@@ -48,6 +50,18 @@ static int get_color(mp_obj_t obj) {
4850 return max - v * max / 100 ;
4951}
5052
53+ static void display_stop_running_animation (const pb_type_Image_obj_t * img ) {
54+ if (img -> is_display ) {
55+ pbsys_hub_light_matrix_free_display ();
56+ }
57+ }
58+
59+ static void display_update (const pb_type_Image_obj_t * img ) {
60+ if (img -> is_display ) {
61+ pbdrv_display_update ();
62+ }
63+ }
64+
5165mp_obj_t pb_type_Image_display_obj_new (void ) {
5266 pb_type_Image_obj_t * self = mp_obj_malloc (pb_type_Image_obj_t , & pb_type_Image );
5367 self -> owner = MP_OBJ_NULL ;
@@ -175,11 +189,11 @@ static void pb_type_Image_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
175189static mp_obj_t pb_type_Image_clear (mp_obj_t self_in ) {
176190 pb_type_Image_obj_t * self = MP_OBJ_TO_PTR (self_in );
177191
192+ display_stop_running_animation (self );
193+
178194 pbio_image_fill (& self -> image , 0 );
179195
180- if (self -> is_display ) {
181- pbdrv_display_update ();
182- }
196+ display_update (self );
183197
184198 return mp_const_none ;
185199}
@@ -196,12 +210,12 @@ static mp_obj_t pb_type_Image_load_image(size_t n_args, const mp_obj_t *pos_args
196210 int x = (self -> image .width - source -> image .width ) / 2 ;
197211 int y = (self -> image .height - source -> image .height ) / 2 ;
198212
213+ display_stop_running_animation (self );
214+
199215 pbio_image_fill (& self -> image , 0 );
200216 pbio_image_draw_image (& self -> image , & source -> image , x , y );
201217
202- if (self -> is_display ) {
203- pbdrv_display_update ();
204- }
218+ display_update (self );
205219
206220 return mp_const_none ;
207221}
@@ -220,6 +234,8 @@ static mp_obj_t pb_type_Image_draw_image(size_t n_args, const mp_obj_t *pos_args
220234 pb_assert_type (source_in , & pb_type_Image );
221235 pb_type_Image_obj_t * source = MP_OBJ_TO_PTR (source_in );
222236
237+ display_stop_running_animation (self );
238+
223239 if (transparent_in == mp_const_none ) {
224240 pbio_image_draw_image (& self -> image , & source -> image , x , y );
225241 } else {
@@ -228,9 +244,7 @@ static mp_obj_t pb_type_Image_draw_image(size_t n_args, const mp_obj_t *pos_args
228244 pbio_image_draw_image_transparent (& self -> image , & source -> image , x , y , transparent_value );
229245 }
230246
231- if (self -> is_display ) {
232- pbdrv_display_update ();
233- }
247+ display_update (self );
234248
235249 return mp_const_none ;
236250}
@@ -247,11 +261,11 @@ static mp_obj_t pb_type_Image_draw_pixel(size_t n_args, const mp_obj_t *pos_args
247261 mp_int_t y = pb_obj_get_int (y_in );
248262 int color = get_color (color_in );
249263
264+ display_stop_running_animation (self );
265+
250266 pbio_image_draw_pixel (& self -> image , x , y , color );
251267
252- if (self -> is_display ) {
253- pbdrv_display_update ();
254- }
268+ display_update (self );
255269
256270 return mp_const_none ;
257271}
@@ -274,11 +288,11 @@ static mp_obj_t pb_type_Image_draw_line(size_t n_args, const mp_obj_t *pos_args,
274288 mp_int_t width = pb_obj_get_int (width_in );
275289 int color = get_color (color_in );
276290
291+ display_stop_running_animation (self );
292+
277293 pbio_image_draw_thick_line (& self -> image , x1 , y1 , x2 , y2 , width , color );
278294
279- if (self -> is_display ) {
280- pbdrv_display_update ();
281- }
295+ display_update (self );
282296
283297 return mp_const_none ;
284298}
@@ -303,6 +317,8 @@ static mp_obj_t pb_type_Image_draw_box(size_t n_args, const mp_obj_t *pos_args,
303317 bool fill = mp_obj_is_true (fill_in );
304318 int color = get_color (color_in );
305319
320+ display_stop_running_animation (self );
321+
306322 int width = x2 - x1 + 1 ;
307323 int height = y2 - y1 + 1 ;
308324 if (fill ) {
@@ -311,9 +327,7 @@ static mp_obj_t pb_type_Image_draw_box(size_t n_args, const mp_obj_t *pos_args,
311327 pbio_image_draw_rounded_rect (& self -> image , x1 , y1 , width , height , r , color );
312328 }
313329
314- if (self -> is_display ) {
315- pbdrv_display_update ();
316- }
330+ display_update (self );
317331
318332 return mp_const_none ;
319333}
@@ -334,15 +348,15 @@ static mp_obj_t pb_type_Image_draw_circle(size_t n_args, const mp_obj_t *pos_arg
334348 bool fill = mp_obj_is_true (fill_in );
335349 int color = get_color (color_in );
336350
351+ display_stop_running_animation (self );
352+
337353 if (fill ) {
338354 pbio_image_fill_circle (& self -> image , x , y , r , color );
339355 } else {
340356 pbio_image_draw_circle (& self -> image , x , y , r , color );
341357 }
342358
343- if (self -> is_display ) {
344- pbdrv_display_update ();
345- }
359+ display_update (self );
346360
347361 return mp_const_none ;
348362}
0 commit comments