Skip to content

Commit 9b4e293

Browse files
markshannonntoll
authored andcommitted
Add 'clear' parameter.
1 parent 42f691f commit 9b4e293

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

inc/microbit/microbitdisplay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C" {
3333

3434
void microbit_display_show(microbit_display_obj_t *display, microbit_image_obj_t *image);
3535

36-
void microbit_display_animate(microbit_display_obj_t *display, mp_obj_t iterable, mp_int_t delay, bool wait);
36+
void microbit_display_animate(microbit_display_obj_t *display, mp_obj_t iterable, mp_int_t delay, bool clear, bool wait);
3737

3838
void microbit_display_scroll(microbit_display_obj_t *display, const char* str, mp_int_t len, bool wait);
3939

source/microbit/microbitdisplay.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ mp_obj_t microbit_display_show_func(mp_uint_t n_args, const mp_obj_t *pos_args,
6767

6868
static const mp_arg_t show_allowed_args[] = {
6969
{ MP_QSTR_image, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
70-
{ MP_QSTR_delay, MP_ARG_INT, {.u_int = 0} },
70+
{ MP_QSTR_delay, MP_ARG_INT, {.u_int = MICROBIT_DEFAULT_PRINT_SPEED} },
71+
{ MP_QSTR_clear, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
7172
{ MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
7273
};
7374

@@ -78,7 +79,8 @@ mp_obj_t microbit_display_show_func(mp_uint_t n_args, const mp_obj_t *pos_args,
7879

7980
mp_obj_t image = args[0].u_obj;
8081
mp_int_t delay = args[1].u_int;
81-
bool wait = args[2].u_bool;
82+
bool clear = args[2].u_bool;
83+
bool wait = args[3].u_bool;
8284

8385
if (MP_OBJ_IS_STR(image)) {
8486
// arg is a string object
@@ -88,32 +90,23 @@ mp_obj_t microbit_display_show_func(mp_uint_t n_args, const mp_obj_t *pos_args,
8890
// There are no chars; do nothing.
8991
return mp_const_none;
9092
} else if (len == 1) {
91-
if (delay == 0) {
93+
if (!clear) {
9294
// A single char; convert to an image and print that.
9395
image = microbit_image_for_char(str[0]);
94-
goto single_image;
95-
}
96-
} else {
97-
if (delay == 0) {
98-
delay = MICROBIT_DEFAULT_PRINT_SPEED;
96+
goto single_image_immediate;
9997
}
10098
}
10199
} else if (mp_obj_get_type(image) == &microbit_image_type) {
102-
if (delay == 0) {
103-
goto single_image;
100+
if (!clear) {
101+
goto single_image_immediate;
104102
}
105103
image = mp_obj_new_tuple(1, &image);
106104
}
107-
else {
108-
if (delay == 0) {
109-
delay = MICROBIT_DEFAULT_PRINT_SPEED;
110-
}
111-
}
112105
// iterable:
113106
microbit_display_animate(self, image, delay, wait);
114107
return mp_const_none;
115108

116-
single_image:
109+
single_image_immediate:
117110
microbit_display_show(self, (microbit_image_obj_t *)image);
118111
return mp_const_none;
119112
}
@@ -122,10 +115,10 @@ MP_DEFINE_CONST_FUN_OBJ_KW(microbit_display_show_obj, 1, microbit_display_show_f
122115
static uint8_t async_mode;
123116
static mp_obj_t async_iterator = NULL;
124117
// Record if an error occurs in async animation. Unfortunately there is no way to report this.
125-
static bool async_error = false;
126118
static volatile bool wakeup_event = false;
127119
static mp_uint_t async_delay = 1000;
128120
static mp_uint_t async_tick = 0;
121+
static bool async_clear = false;
129122

130123
STATIC void wait_for_event() {
131124
while (!wakeup_event)
@@ -138,6 +131,7 @@ STATIC void async_stop(void) {
138131
async_mode = ASYNC_MODE_STOPPED;
139132
async_tick = 0;
140133
async_delay = 1000;
134+
async_clear = false;
141135
MP_STATE_PORT(async_data)[0] = NULL;
142136
MP_STATE_PORT(async_data)[1] = NULL;
143137
wakeup_event = true;
@@ -272,6 +266,7 @@ static void microbit_display_update(void) {
272266
* If an exception is raised here, then a reset is the only way to recover. */
273267
mp_obj_t obj = mp_iternext(async_iterator);
274268
if (obj == MP_OBJ_STOP_ITERATION) {
269+
if (async_clear)
275270
async_stop();
276271
} else if (mp_obj_get_type(obj) == &microbit_image_type) {
277272
microbit_display_show(display, (microbit_image_obj_t *)obj);
@@ -311,7 +306,7 @@ void microbit_display_tick(void) {
311306
}
312307

313308

314-
void microbit_display_animate(microbit_display_obj_t *self, mp_obj_t iterable, mp_int_t delay, bool wait) {
309+
void microbit_display_animate(microbit_display_obj_t *self, mp_obj_t iterable, mp_int_t delay, bool clear, bool wait) {
315310
// Reset the repeat state.
316311
MP_STATE_PORT(async_data)[0] = NULL;
317312
MP_STATE_PORT(async_data)[1] = NULL;

0 commit comments

Comments
 (0)