summaryrefslogtreecommitdiff
path: root/shared-bindings/camera
diff options
context:
space:
mode:
authorKamil Tomaszewski <kamil.tomaszewski@sony.com>2020-09-16 13:55:57 +0200
committerKamil Tomaszewski <kamil.tomaszewski@sony.com>2020-09-16 13:55:57 +0200
commit61687c81d82572ee5c014a9f167b2d73db73e2f4 (patch)
treea42824a1b0e002d50360f8a700d5ecc23efac86b /shared-bindings/camera
parentce7ee58e92eca840f75c2fcf885fe862f1ebec13 (diff)
camera: Pass width and height to take_picture()
Diffstat (limited to 'shared-bindings/camera')
-rw-r--r--shared-bindings/camera/Camera.c92
-rw-r--r--shared-bindings/camera/Camera.h9
2 files changed, 17 insertions, 84 deletions
diff --git a/shared-bindings/camera/Camera.c b/shared-bindings/camera/Camera.c
index 5fe54e429..fd88ccab0 100644
--- a/shared-bindings/camera/Camera.c
+++ b/shared-bindings/camera/Camera.c
@@ -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));
-}
-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));
+ 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_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);
diff --git a/shared-bindings/camera/Camera.h b/shared-bindings/camera/Camera.h
index 7ef13cd07..8102672e4 100644
--- a/shared-bindings/camera/Camera.h
+++ b/shared-bindings/camera/Camera.h
@@ -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