Example #1
0
static mp_obj_t py_image_save(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
{
    int res;
    image_t *image = py_image_cobj(args[0]);
    const char *path = mp_obj_str_get_str(args[1]);

    mp_map_elem_t *kw_subimage = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("subimage")), MP_MAP_LOOKUP);
    if (kw_subimage != NULL) {
        mp_obj_t *array;
        mp_obj_get_array_fixed_n(kw_subimage->value, 4, &array);

        rectangle_t r = {
            mp_obj_get_int(array[0]),
            mp_obj_get_int(array[1]),
            mp_obj_get_int(array[2]),
            mp_obj_get_int(array[3]),
        };

        res = imlib_save_image(image, path, &r);
    } else {
        res = imlib_save_image(image, path, NULL);
    }

    if (res != FR_OK) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
    }

    return mp_const_true;
}
Example #2
0
mp_obj_t py_imlib_save_template(mp_obj_t image_obj, mp_obj_t rectangle_obj, mp_obj_t path_obj)
{
    struct image t;
    struct image *image = NULL;

    struct rectangle r;
    mp_obj_t *array;

    const char *path = mp_obj_str_get_str(path_obj);

    array = mp_obj_get_array_fixed_n(rectangle_obj, 4);
    r.x = mp_obj_get_int(array[0]);
    r.y = mp_obj_get_int(array[1]);
    r.w = mp_obj_get_int(array[2]);
    r.h = mp_obj_get_int(array[3]);


    /* get C image pointer */
    image = py_image_cobj(image_obj);
    t.w = r.w;
    t.h = r.h;
    t.data = malloc(sizeof(*t.data)*t.w*t.h);

    imlib_subimage(image, &t, r.x, r.y);

    int res = imlib_save_template(&t, path);
    free(t.data);

    if (res != FR_OK) {
        nlr_jump(mp_obj_new_exception_msg(qstr_from_str("Imlib"), ffs_strerror(res)));
    }

    return mp_const_true;
}
Example #3
0
mp_obj_t py_file_open(mp_obj_t path, mp_obj_t mode_str)
{
    BYTE mode=0;
    FRESULT res;
    py_file_obj_t *o;

    switch (mp_obj_str_get_str(mode_str)[0]) {
        case 'r':
            /* Open file for reading, fail if the file is not existing. */
            mode = FA_READ|FA_OPEN_EXISTING;
            break;
        case 'w':
            /* Open file for reading/writing, create the file if not existing. */
            mode = FA_READ|FA_WRITE|FA_OPEN_ALWAYS;
            break;
        case 'a':
            /* Open file for reading/writing, fail if the file is not existing. */
            mode = FA_READ|FA_WRITE|FA_OPEN_EXISTING;
            break;
        default:
            nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid open mode"));
    }

    /* Create new python file obj */
    o = m_new_obj(py_file_obj_t);
    o->base.type = &py_file_type;

    /* Open underlying file handle */
    res = f_open(&o->fp, mp_obj_str_get_str(path), mode);
    if (res != FR_OK) {
        nlr_jump(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
    }
    return o;
}
Example #4
0
mp_obj_t py_imlib_load_cascade(mp_obj_t path_obj)
{
    py_cascade_obj_t *o =NULL;

    /* detection parameters */
    struct cascade cascade = {
        .step = 2,
        .scale_factor = 1.25f,
    };

    const char *path = mp_obj_str_get_str(path_obj);
    int res = imlib_load_cascade(&cascade, path);
    if (res != FR_OK) {
        nlr_jump(mp_obj_new_exception_msg(qstr_from_str("Imlib"), ffs_strerror(res)));
    }

    o = m_new_obj(py_cascade_obj_t);
    o->base.type = &py_cascade_type;
    o->_cobj = cascade;
    return o;
}

mp_obj_t py_imlib_load_template(mp_obj_t path_obj)
{
    mp_obj_t image_obj =NULL;
    struct image *image;
    const char *path = mp_obj_str_get_str(path_obj);
    image_obj = py_image(0, 0, 0, 0);

    /* get image pointer */
    image = py_image_cobj(image_obj);

    int res = imlib_load_template(image, path);
    if (res != FR_OK) {
        nlr_jump(mp_obj_new_exception_msg(qstr_from_str("Imlib"), ffs_strerror(res)));
    }

    return image_obj;
}
Example #5
0
mp_obj_t py_file_write(py_file_obj_t *file, mp_obj_t buf)
{
    uint len;
    const char *str;
    FRESULT res;

    str = mp_obj_str_get_data(buf, &len);

    res = f_write(&file->fp, str, len, &len);
    if (res != FR_OK) {
        nlr_jump(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
    }

    return mp_obj_new_int(len);
}
Example #6
0
mp_obj_t py_file_write(py_file_obj_t *file, mp_obj_t buf)
{
    uint len;
    const byte *str;
    FRESULT res;

    str = mp_obj_str_get_data(buf, &len);

    res = f_write(&file->fp, str, len, &len);
    if (res != FR_OK) {
        nlr_jump(mp_obj_new_exception_msg(qstr_from_str("File"), ffs_strerror(res)));
    }

    return mp_obj_new_int(len);
}
Example #7
0
NORETURN static void ff_fail(FIL *fp, FRESULT res)
{
    if (fp) f_close(fp);
    nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
}
Example #8
0
void usbdbg_data_out(void *buffer, int length)
{
    switch (cmd) {
        case USBDBG_SCRIPT_EXEC:
            // check if GC is locked before allocating memory for vstr. If GC was locked
            // at least once before the script is fully uploaded xfer_bytes will be less
            // than the total length (xfer_length) and the script will Not be executed.
            if (usbdbg_get_irq_enabled() && !gc_is_locked()) {
                vstr_add_strn(&script_buf, buffer, length);
                xfer_bytes += length;
                if (xfer_bytes == xfer_length) {
                    // Set script ready flag
                    script_ready = 1;
                    // Disable IDE IRQ (re-enabled by pyexec or main).
                    usbdbg_set_irq_enabled(false);
                    // interrupt running script/REPL
                    mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
                    pendsv_nlr_jump_hard(mp_const_ide_interrupt);
                }
            }
            break;

        case USBDBG_TEMPLATE_SAVE: {
            image_t image ={
                .w = fb->w,
                .h = fb->h,
                .bpp = fb->bpp,
                .pixels = fb->pixels
            };

            // null terminate the path
            length = (length == 64) ? 63:length; 
            ((char*)buffer)[length] = 0;

            rectangle_t *roi = (rectangle_t*)buffer;
            char *path = (char*)buffer+sizeof(rectangle_t);

            int res=imlib_save_image(&image, path, roi);
            if (res != FR_OK) {
                nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));
            }
            // raise a flash IRQ to flush image
            //NVIC->STIR = FLASH_IRQn;
            break;
        }

        case USBDBG_DESCRIPTOR_SAVE: {
            image_t image ={
                .w = fb->w,
                .h = fb->h,
                .bpp = fb->bpp,
                .pixels = fb->pixels
            };

            // null terminate the path
            length = (length == 64) ? 63:length; 
            ((char*)buffer)[length] = 0;

            rectangle_t *roi = (rectangle_t*)buffer;
            char *path = (char*)buffer+sizeof(rectangle_t);

            py_image_descriptor_from_roi(&image, path, roi);
            break;
        }
        default: /* error */
            break;
    }
}

void usbdbg_control(void *buffer, uint8_t request, uint32_t length)
{
    cmd = (enum usbdbg_cmd) request;
    switch (cmd) {
        case USBDBG_FW_VERSION:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_SIZE:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_DUMP:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_LOCK:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        case USBDBG_FRAME_UPDATE:
            sensor_snapshot(NULL);
            cmd = USBDBG_NONE;
            break;

        case USBDBG_SCRIPT_EXEC:
            xfer_bytes = 0;
            xfer_length =length;
            vstr_reset(&script_buf);
            break;

        case USBDBG_SCRIPT_STOP:
            if (usbdbg_get_irq_enabled()) {
                // Disable IDE IRQ (re-enabled by pyexec or main).
                usbdbg_set_irq_enabled(false);
                // interrupt running code by raising an exception
                mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
                pendsv_nlr_jump_hard(mp_const_ide_interrupt);
            }
            cmd = USBDBG_NONE;
            break;

        case USBDBG_SCRIPT_SAVE:
            /* save running script */
            break;

        case USBDBG_TEMPLATE_SAVE:
        case USBDBG_DESCRIPTOR_SAVE:
            /* save template */
            xfer_bytes = 0;
            xfer_length =length;
            break;

        case USBDBG_ATTR_WRITE: {
            /* write sensor attribute */
            int16_t attr= *((int16_t*)buffer);
            int16_t val = *((int16_t*)buffer+1);
            switch (attr) {
                case ATTR_CONTRAST:
                    sensor_set_contrast(val);
                    break;
                case ATTR_BRIGHTNESS:
                    sensor_set_brightness(val);
                    break;
                case ATTR_SATURATION:
                    sensor_set_saturation(val);
                    break;
                case ATTR_GAINCEILING:
                    sensor_set_gainceiling(val);
                    break;
                default:
                    break;
            }
            cmd = USBDBG_NONE;
            break;
        }

        case USBDBG_SYS_RESET:
            NVIC_SystemReset();
            break;

        case USBDBG_BOOT:
            *((uint32_t *)0x20002000) = 0xDEADBEEF;
            NVIC_SystemReset();
            break;

        case USBDBG_TX_BUF:
        case USBDBG_TX_BUF_LEN:
            xfer_bytes = 0;
            xfer_length = length;
            break;

        default: /* error */
            cmd = USBDBG_NONE;
            break;
    }
}