camera: Pass width and height to take_picture()

This commit is contained in:
Kamil Tomaszewski 2020-09-16 13:55:57 +02:00
parent ce7ee58e92
commit 61687c81d8
4 changed files with 22 additions and 118 deletions

View File

@ -118,9 +118,7 @@ static void camera_start_preview() {
camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE);
}
extern uint32_t _ebss;
extern uint32_t _stext;
void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height) {
void common_hal_camera_construct(camera_obj_t *self) {
if (camera_dev.fd < 0) {
if (video_initialize(camera_dev.devpath) < 0) {
mp_raise_ValueError(translate("Could not initialize Camera"));
@ -131,17 +129,8 @@ void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t he
}
}
if (!camera_check_width_and_height(width, height)) {
mp_raise_ValueError(translate("Size not supported"));
}
self->width = width;
self->height = height;
camera_start_preview();
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height);
camera_start_streaming(V4L2_BUF_TYPE_STILL_CAPTURE);
sleep(1);
@ -162,18 +151,18 @@ bool common_hal_camera_deinited(camera_obj_t *self) {
return camera_dev.fd < 0;
}
size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, camera_imageformat_t format) {
if (!camera_check_width_and_height(self->width, self->height)) {
size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, uint16_t width, uint16_t height, camera_imageformat_t format) {
if (!camera_check_width_and_height(width, height)) {
mp_raise_ValueError(translate("Size not supported"));
}
if (!camera_check_buffer_length(self->width, self->height, format, len)) {
if (!camera_check_buffer_length(width, height, format, len)) {
mp_raise_ValueError(translate("Buffer is too small"));
}
if (!camera_check_format(format)) {
mp_raise_ValueError(translate("Format not supported"));
}
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, self->width, self->height);
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height);
v4l2_buffer_t buf;
@ -192,19 +181,3 @@ size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_
return (size_t)buf.bytesused;
}
uint16_t common_hal_camera_get_width(camera_obj_t *self) {
return self->width;
}
void common_hal_camera_set_width(camera_obj_t *self, uint16_t width) {
self->width = width;
}
uint16_t common_hal_camera_get_height(camera_obj_t *self) {
return self->height;
}
void common_hal_camera_set_height(camera_obj_t *self, uint16_t height) {
self->height = height;
}

View File

@ -31,8 +31,6 @@
typedef struct {
mp_obj_base_t base;
uint16_t width;
uint16_t height;
} camera_obj_t;
#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_CAMERA_CAMERA_H

View File

@ -48,34 +48,26 @@
//| vfs = storage.VfsFat(sd)
//| storage.mount(vfs, '/sd')
//|
//| cam = camera.Camera(1920, 1080)
//| cam = camera.Camera()
//|
//| buffer = bytearray(512 * 1024)
//| file = open("/sd/image.jpg","wb")
//| size = cam.take_picture(buffer, camera.ImageFormat.JPG)
//| size = cam.take_picture(buffer, width=1920, height=1080, format=camera.ImageFormat.JPG)
//| file.write(buffer, size)
//| file.close()"""
//|
//| def __init__(self, width: int, height: int) -> None:
//| """Initialize camera.
//|
//| :param int width: Width in pixels
//| :param int height: Height in pixels"""
//| def __init__(self) -> None:
//| """Initialize camera."""
//| ...
//|
STATIC mp_obj_t camera_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
camera_obj_t *self = m_new_obj(camera_obj_t);
self->base.type = &camera_type;
enum { ARG_width, ARG_height };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// No arguments
mp_arg_check_num(n_args, kw_args, 0, 0, false);
common_hal_camera_construct(self, args[ARG_width].u_int, args[ARG_height].u_int);
common_hal_camera_construct(self);
return MP_OBJ_FROM_PTR(self);
}
@ -97,17 +89,20 @@ STATIC void check_for_deinit(camera_obj_t *self) {
}
//| def take_picture(self, buf: WriteableBuffer, format: ImageFormat) -> int:
//| """Take picture and save to ``buf`` in the given ``format``
//| """Take picture and save to ``buf`` in the given ``format``. The size of the picture
//| taken is ``width`` by ``height`` in pixels.
//|
//| :return: the number of bytes written into buf
//| :rtype: int"""
//| ...
//|
STATIC mp_obj_t camera_obj_take_picture(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_format };
enum { ARG_buffer, ARG_width, ARG_height, ARG_format };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_format, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_width, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_height, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_format, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
};
camera_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
@ -119,70 +114,13 @@ STATIC mp_obj_t camera_obj_take_picture(size_t n_args, const mp_obj_t *pos_args,
camera_imageformat_t format = camera_imageformat_obj_to_type(args[ARG_format].u_obj);
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_take_picture(self, (uint8_t *)bufinfo.buf, bufinfo.len, format));
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_take_picture(self, (uint8_t *)bufinfo.buf, bufinfo.len, args[ARG_width].u_int, args[ARG_height].u_int, format));
}
MP_DEFINE_CONST_FUN_OBJ_KW(camera_take_picture_obj, 3, camera_obj_take_picture);
//| width: int
//| """Image width in pixels."""
//|
STATIC mp_obj_t camera_obj_get_width(mp_obj_t self_in) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_get_width(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(camera_get_width_obj, camera_obj_get_width);
STATIC mp_obj_t camera_obj_set_width(mp_obj_t self_in, mp_obj_t value) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_camera_set_width(self, mp_obj_get_int(value));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(camera_set_width_obj, camera_obj_set_width);
const mp_obj_property_t camera_width_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&camera_get_width_obj,
(mp_obj_t)&camera_set_width_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| height: int
//| """Image height in pixels."""
//|
STATIC mp_obj_t camera_obj_get_height(mp_obj_t self_in) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_get_height(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(camera_get_height_obj, camera_obj_get_height);
STATIC mp_obj_t camera_obj_set_height(mp_obj_t self_in, mp_obj_t value) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_camera_set_height(self, mp_obj_get_int(value));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(camera_set_height_obj, camera_obj_set_height);
const mp_obj_property_t camera_height_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&camera_get_height_obj,
(mp_obj_t)&camera_set_height_obj,
(mp_obj_t)&mp_const_none_obj},
};
MP_DEFINE_CONST_FUN_OBJ_KW(camera_take_picture_obj, 2, camera_obj_take_picture);
STATIC const mp_rom_map_elem_t camera_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&camera_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_take_picture), MP_ROM_PTR(&camera_take_picture_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&camera_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&camera_height_obj) },
};
STATIC MP_DEFINE_CONST_DICT(camera_locals_dict, camera_locals_dict_table);

View File

@ -32,14 +32,9 @@
extern const mp_obj_type_t camera_type;
void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height);
void common_hal_camera_construct(camera_obj_t *self);
void common_hal_camera_deinit(camera_obj_t *self);
bool common_hal_camera_deinited(camera_obj_t *self);
size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, camera_imageformat_t format);
uint16_t common_hal_camera_get_width(camera_obj_t *self);
void common_hal_camera_set_width(camera_obj_t *self, uint16_t width);
uint16_t common_hal_camera_get_height(camera_obj_t *self);
void common_hal_camera_set_height(camera_obj_t *self, uint16_t height);
size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, uint16_t width, uint16_t height, camera_imageformat_t format);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H