mp_obj_t microbit_display_show_func(mp_uint_t n_args, const mp_obj_t *args) {
    // TODO: Support async mode.

    microbit_display_obj_t *self = (microbit_display_obj_t*)args[0];

    // Cancel any animations.
    MP_STATE_PORT(async_data)[0] = NULL;
    MP_STATE_PORT(async_data)[1] = NULL;
    MP_STATE_PORT(async_data)[2] = NULL;

    if (MP_OBJ_IS_STR(args[1])) {
        // arg is a string object
        mp_uint_t len;
        const char *str = mp_obj_str_get_data(args[1], &len);
        if (len == 0) {
            // There are no chars; do nothing.
        } else if (len == 1) {
            // A single char; convert to an image and print that.
            microbit_display_show(self, microbit_image_for_char(str[0]));
        } else {
            mp_int_t delay;
            if (n_args == 3) {
                delay = mp_obj_get_int(args[2]);
            } else {
                delay = MICROBIT_DEFAULT_PRINT_SPEED;
            }
            microbit_display_animate(self, args[1], delay, false, false);
        }
    } else if (mp_obj_get_type(args[1]) == &microbit_image_type) {
        microbit_display_show(self, (microbit_image_obj_t *)args[1]);
    } else {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "expecting an image or a string."));
    }
    return mp_const_none;
}
static void microbit_display_update(void) {
    async_tick += FIBER_TICK_PERIOD_MS;
    if (async_tick < async_delay) {
        return;
    }
    async_tick = 0;
    switch (async_mode) {
        case ASYNC_MODE_ANIMATION:
        {
            if (MP_STATE_PORT(async_data)[0] == NULL || MP_STATE_PORT(async_data)[1] == NULL) {
                async_stop();
                break;
            }
            microbit_display_obj_t *display = (microbit_display_obj_t*)MP_STATE_PORT(async_data)[0];
            /* WARNING: We are executing in an interrupt handler.
             * If an exception is raised here, then a reset is the only way to recover. */
            mp_obj_t obj = mp_iternext(async_iterator);
            if (obj == MP_OBJ_STOP_ITERATION) {
                if (async_repeat_iterable) {
                    async_iterator = mp_getiter(async_repeat_iterable);
                } else {
                    microbit_display_show(display, BLANK_IMAGE);
                    async_stop();
                }
            } else if (mp_obj_get_type(obj) == &microbit_image_type) {
                microbit_display_show(display, (microbit_image_obj_t *)obj);
            } else if (MP_OBJ_IS_STR(obj)) {
                mp_uint_t len;
                const char *str = mp_obj_str_get_data(obj, &len);
                if (len == 1) {
                    microbit_display_show(display, microbit_image_for_char(str[0]));
                } else {
                    async_error = true;
                    async_stop();
                }
            } else {
                async_error = true;
                async_stop();
            }
            break;
        }
        case ASYNC_MODE_CLEAR:
            microbit_display_show(&microbit_display_obj, BLANK_IMAGE);
            async_stop();
            break;
    }
}
示例#3
0
STATIC mp_obj_t microbit_panic(mp_uint_t n_args, const mp_obj_t *args) {
    while(true) {
        microbit_display_show(&microbit_display_obj, (microbit_image_obj_t*)&panic);
        mp_hal_delay_ms(1000);
        char num[4];
        int code;
        if (n_args) {
            code = mp_obj_get_int(args[0]);
        } else {
            code = 0;
        }
        num[2] = code%10 + '0';
        code /= 10;
        num[1] = code%10 + '0';
        code /= 10;
        num[0] = code%10 + '0';
        for (int i = 0; i < 3; i++) {
            microbit_display_show(&microbit_display_obj, microbit_image_for_char(num[i]));
            mp_hal_delay_ms(1000);
        }
    }
    return mp_const_none;
}