|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright 2020 Sony Semiconductor Solutions Corporation |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/objproperty.h" |
| 28 | +#include "py/runtime.h" |
| 29 | + |
| 30 | +#include "shared-bindings/camera/Camera.h" |
| 31 | +#include "shared-bindings/util.h" |
| 32 | + |
| 33 | +//| class Camera: |
| 34 | +//| """The class to control camera. |
| 35 | +//| |
| 36 | +//| Usage:: |
| 37 | +//| |
| 38 | +//| import board |
| 39 | +//| import sdioio |
| 40 | +//| import storage |
| 41 | +//| import camera |
| 42 | +//| |
| 43 | +//| sd = sdioio.SDCard( |
| 44 | +//| clock=board.SDIO_CLOCK, |
| 45 | +//| command=board.SDIO_COMMAND, |
| 46 | +//| data=board.SDIO_DATA, |
| 47 | +//| frequency=25000000) |
| 48 | +//| vfs = storage.VfsFat(sd) |
| 49 | +//| storage.mount(vfs, '/sd') |
| 50 | +//| |
| 51 | +//| cam = camera.Camera(camera.ImageSize.IMAGE_SIZE_1920x1080) |
| 52 | +//| |
| 53 | +//| file = open("/sd/image.jpg","wb") |
| 54 | +//| cam.take_picture() |
| 55 | +//| file.write(cam.picture) |
| 56 | +//| file.close()""" |
| 57 | +//| |
| 58 | + |
| 59 | +//| def __init__(self, ): |
| 60 | +//| """Initialize camera. |
| 61 | +//| |
| 62 | +//| :param camera.ImageSize size: image size""" |
| 63 | +//| ... |
| 64 | +//| |
| 65 | +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) { |
| 66 | + camera_obj_t *self = m_new_obj(camera_obj_t); |
| 67 | + self->base.type = &camera_type; |
| 68 | + enum { ARG_size }; |
| 69 | + static const mp_arg_t allowed_args[] = { |
| 70 | + { MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 71 | + }; |
| 72 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 73 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 74 | + |
| 75 | + camera_imagesize_t size = camera_imagesize_obj_to_type(args[ARG_size].u_obj); |
| 76 | + |
| 77 | + common_hal_camera_construct(self, size); |
| 78 | + return MP_OBJ_FROM_PTR(self); |
| 79 | +} |
| 80 | + |
| 81 | +//| def deinit(self, ) -> Any: |
| 82 | +//| """De-initialize camera.""" |
| 83 | +//| ... |
| 84 | +//| |
| 85 | +STATIC mp_obj_t camera_obj_deinit(mp_obj_t self_in) { |
| 86 | + camera_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 87 | + common_hal_camera_deinit(self); |
| 88 | + return mp_const_none; |
| 89 | +} |
| 90 | +MP_DEFINE_CONST_FUN_OBJ_1(camera_deinit_obj, camera_obj_deinit); |
| 91 | + |
| 92 | +STATIC void check_for_deinit(camera_obj_t *self) { |
| 93 | + if (common_hal_camera_deinited(self)) { |
| 94 | + raise_deinited_error(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +//| def take_picture(self, ) -> Any: |
| 99 | +//| """Take picture.""" |
| 100 | +//| ... |
| 101 | +//| |
| 102 | +STATIC mp_obj_t camera_obj_take_picture(mp_obj_t self_in) { |
| 103 | + camera_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 104 | + check_for_deinit(self); |
| 105 | + |
| 106 | + common_hal_camera_take_picture(self); |
| 107 | + return mp_const_none; |
| 108 | +} |
| 109 | +MP_DEFINE_CONST_FUN_OBJ_1(camera_take_picture_obj, camera_obj_take_picture); |
| 110 | + |
| 111 | +//| picture: Any = ... |
| 112 | +//| """Image buffer.""" |
| 113 | +//| |
| 114 | +STATIC mp_obj_t camera_obj_get_picture(mp_obj_t self_in) { |
| 115 | + camera_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 116 | + check_for_deinit(self); |
| 117 | + |
| 118 | + uint8_t *buffer = common_hal_camera_get_picture_buffer(self); |
| 119 | + size_t size = common_hal_camera_get_picture_size(self); |
| 120 | + |
| 121 | + return mp_obj_new_bytearray_by_ref(size, buffer); |
| 122 | +} |
| 123 | +MP_DEFINE_CONST_FUN_OBJ_1(camera_get_picture_obj, camera_obj_get_picture); |
| 124 | + |
| 125 | +const mp_obj_property_t camera_picture_obj = { |
| 126 | + .base.type = &mp_type_property, |
| 127 | + .proxy = {(mp_obj_t)&camera_get_picture_obj, |
| 128 | + (mp_obj_t)&mp_const_none_obj, |
| 129 | + (mp_obj_t)&mp_const_none_obj}, |
| 130 | +}; |
| 131 | + |
| 132 | +//| size: Any = ... |
| 133 | +//| """Image size.""" |
| 134 | +//| |
| 135 | +STATIC mp_obj_t camera_obj_get_size(mp_obj_t self_in) { |
| 136 | + camera_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 137 | + check_for_deinit(self); |
| 138 | + return camera_imagesize_type_to_obj(common_hal_camera_get_size(self)); |
| 139 | +} |
| 140 | +MP_DEFINE_CONST_FUN_OBJ_1(camera_get_size_obj, camera_obj_get_size); |
| 141 | + |
| 142 | +STATIC mp_obj_t camera_obj_set_size(mp_obj_t self_in, mp_obj_t value) { |
| 143 | + camera_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 144 | + check_for_deinit(self); |
| 145 | + |
| 146 | + camera_imagesize_t size = camera_imagesize_obj_to_type(value); |
| 147 | + if (size == IMAGESIZE_NONE) { |
| 148 | + mp_raise_ValueError(translate("Invalid image size.")); |
| 149 | + } |
| 150 | + |
| 151 | + common_hal_camera_set_size(self, size); |
| 152 | + |
| 153 | + return mp_const_none; |
| 154 | +} |
| 155 | +MP_DEFINE_CONST_FUN_OBJ_2(camera_set_size_obj, camera_obj_set_size); |
| 156 | + |
| 157 | +const mp_obj_property_t camera_size_obj = { |
| 158 | + .base.type = &mp_type_property, |
| 159 | + .proxy = {(mp_obj_t)&camera_get_size_obj, |
| 160 | + (mp_obj_t)&camera_set_size_obj, |
| 161 | + (mp_obj_t)&mp_const_none_obj}, |
| 162 | +}; |
| 163 | + |
| 164 | +STATIC const mp_rom_map_elem_t camera_locals_dict_table[] = { |
| 165 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&camera_deinit_obj) }, |
| 166 | + { MP_ROM_QSTR(MP_QSTR_take_picture), MP_ROM_PTR(&camera_take_picture_obj) }, |
| 167 | + |
| 168 | + { MP_ROM_QSTR(MP_QSTR_picture), MP_ROM_PTR(&camera_picture_obj) }, |
| 169 | + { MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&camera_size_obj) }, |
| 170 | +}; |
| 171 | +STATIC MP_DEFINE_CONST_DICT(camera_locals_dict, camera_locals_dict_table); |
| 172 | + |
| 173 | +const mp_obj_type_t camera_type = { |
| 174 | + { &mp_type_type }, |
| 175 | + .name = MP_QSTR_GNSS, |
| 176 | + .make_new = camera_make_new, |
| 177 | + .locals_dict = (mp_obj_dict_t*)&camera_locals_dict, |
| 178 | +}; |
0 commit comments