@@ -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) == µbit_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
122115static uint8_t async_mode;
123116static 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 ;
126118static volatile bool wakeup_event = false ;
127119static mp_uint_t async_delay = 1000 ;
128120static mp_uint_t async_tick = 0 ;
121+ static bool async_clear = false ;
129122
130123STATIC 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) == µbit_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