Skip to content

Commit 35f23d9

Browse files
committed
pbio/sys/light: stop animation when drawing on screen
Stop the "running" animation on the first drawing to the screen, and clear screen when the user program exits. Refs: pybricks/support#2154
1 parent 9759e90 commit 35f23d9

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

lib/pbio/include/pbsys/light.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ extern pbio_color_light_t *pbsys_status_light_main;
1717
#endif
1818

1919
#if PBSYS_CONFIG_HUB_LIGHT_MATRIX
20+
2021
#include <pbio/light_matrix.h>
22+
2123
extern pbio_light_matrix_t *pbsys_hub_light_matrix;
24+
25+
void pbsys_hub_light_matrix_free_display(void);
26+
27+
#else // PBSYS_CONFIG_HUB_LIGHT_MATRIX
28+
29+
static inline void pbsys_hub_light_matrix_free_display(void) {
30+
}
31+
2232
#endif
2333

2434
#endif // _PBSYS_LIGHT_H_

lib/pbio/sys/light_matrix.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ static pbio_error_t pbsys_hub_light_matrix_set_pixel(pbio_light_matrix_t *light_
5454
return PBIO_ERROR_NOT_SUPPORTED;
5555
}
5656

57+
static void pbsys_hub_light_matrix_clear_display(void) {
58+
#if PBSYS_CONFIG_HUB_LIGHT_MATRIX_DISPLAY
59+
pbio_image_t *display = pbdrv_display_get_image();
60+
pbio_image_fill(display, 0);
61+
pbdrv_display_update();
62+
#endif
63+
}
64+
5765
static const pbio_light_matrix_funcs_t pbsys_hub_light_matrix_funcs = {
5866
.set_pixel = pbsys_hub_light_matrix_set_pixel,
5967
};
@@ -166,8 +174,22 @@ void pbsys_hub_light_matrix_handle_user_program_start(bool start) {
166174
pbio_light_animation_start(&pbsys_hub_light_matrix->animation);
167175
} else {
168176
// If the user program has ended, show stop sign and selected slot.
177+
// Clear display if the user program drawn to it.
178+
if (!pbio_light_animation_is_started(&pbsys_hub_light_matrix->animation)) {
179+
pbsys_hub_light_matrix_clear_display();
180+
}
169181
pbsys_hub_light_matrix_show_idle_ui(100);
170182
}
171183
}
172184

185+
/**
186+
* Free display for user program, stop running animation.
187+
*/
188+
void pbsys_hub_light_matrix_free_display(void) {
189+
if (pbio_light_animation_is_started(&pbsys_hub_light_matrix->animation)) {
190+
pbio_light_animation_stop(&pbsys_hub_light_matrix->animation);
191+
pbsys_hub_light_matrix_clear_display();
192+
}
193+
}
194+
173195
#endif // PBSYS_CONFIG_HUB_LIGHT_MATRIX

pybricks/parameters/pb_type_image.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <pbio/image.h>
2222
#include <pbio/int_math.h>
2323

24+
#include <pbsys/light.h>
25+
2426
#include <pbdrv/display.h>
2527

2628
extern 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+
5165
mp_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) {
175189
static 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

Comments
 (0)