aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-bindings/camera
diff options
context:
space:
mode:
Diffstat (limited to 'circuitpython/shared-bindings/camera')
-rw-r--r--circuitpython/shared-bindings/camera/Camera.c132
-rw-r--r--circuitpython/shared-bindings/camera/Camera.h40
-rw-r--r--circuitpython/shared-bindings/camera/ImageFormat.c92
-rw-r--r--circuitpython/shared-bindings/camera/ImageFormat.h49
-rw-r--r--circuitpython/shared-bindings/camera/__init__.c52
5 files changed, 365 insertions, 0 deletions
diff --git a/circuitpython/shared-bindings/camera/Camera.c b/circuitpython/shared-bindings/camera/Camera.c
new file mode 100644
index 0000000..f7bc9ee
--- /dev/null
+++ b/circuitpython/shared-bindings/camera/Camera.c
@@ -0,0 +1,132 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/camera/Camera.h"
+#include "shared-bindings/util.h"
+
+//| class Camera:
+//| """The class to control camera.
+//|
+//| Usage::
+//|
+//| import board
+//| import sdioio
+//| import storage
+//| import camera
+//|
+//| sd = sdioio.SDCard(
+//| clock=board.SDIO_CLOCK,
+//| command=board.SDIO_COMMAND,
+//| data=board.SDIO_DATA,
+//| frequency=25000000)
+//| vfs = storage.VfsFat(sd)
+//| storage.mount(vfs, '/sd')
+//|
+//| cam = camera.Camera()
+//|
+//| buffer = bytearray(512 * 1024)
+//| file = open("/sd/image.jpg","wb")
+//| size = cam.take_picture(buffer, width=1920, height=1080, format=camera.ImageFormat.JPG)
+//| file.write(buffer, size)
+//| file.close()"""
+//|
+
+//| def __init__(self) -> None:
+//| """Initialize camera."""
+//| ...
+//|
+STATIC mp_obj_t camera_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
+ camera_obj_t *self = m_new_obj(camera_obj_t);
+ self->base.type = &camera_type;
+ // No arguments
+ mp_arg_check_num(n_args, n_kw, 0, 0, false);
+
+ common_hal_camera_construct(self);
+ return MP_OBJ_FROM_PTR(self);
+}
+
+//| def deinit(self) -> None:
+//| """De-initialize camera."""
+//| ...
+//|
+STATIC mp_obj_t camera_obj_deinit(mp_obj_t self_in) {
+ camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_camera_deinit(self);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(camera_deinit_obj, camera_obj_deinit);
+
+STATIC void check_for_deinit(camera_obj_t *self) {
+ if (common_hal_camera_deinited(self)) {
+ raise_deinited_error();
+ }
+}
+
+//| def take_picture(self, buf: WriteableBuffer, format: ImageFormat) -> int:
+//| """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_width, ARG_height, ARG_format };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_buffer, 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);
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
+
+ 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, args[ARG_width].u_int, args[ARG_height].u_int, format));
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(camera_take_picture_obj, 1, 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) },
+};
+STATIC MP_DEFINE_CONST_DICT(camera_locals_dict, camera_locals_dict_table);
+
+const mp_obj_type_t camera_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Camera,
+ .make_new = camera_make_new,
+ .locals_dict = (mp_obj_dict_t *)&camera_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/camera/Camera.h b/circuitpython/shared-bindings/camera/Camera.h
new file mode 100644
index 0000000..8102672
--- /dev/null
+++ b/circuitpython/shared-bindings/camera/Camera.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
+
+#include "common-hal/camera/Camera.h"
+#include "shared-bindings/camera/ImageFormat.h"
+
+extern const mp_obj_type_t camera_type;
+
+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, uint16_t width, uint16_t height, camera_imageformat_t format);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
diff --git a/circuitpython/shared-bindings/camera/ImageFormat.c b/circuitpython/shared-bindings/camera/ImageFormat.c
new file mode 100644
index 0000000..3ff687b
--- /dev/null
+++ b/circuitpython/shared-bindings/camera/ImageFormat.c
@@ -0,0 +1,92 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/camera/ImageFormat.h"
+
+//| class ImageFormat:
+//| """Image format"""
+//|
+//| def __init__(self) -> None:
+//| """Enum-like class to define the image format."""
+//|
+//| JPG: ImageFormat
+//| """JPG format."""
+//|
+//| RGB565: ImageFormat
+//| """RGB565 format."""
+//|
+
+const camera_imageformat_obj_t camera_imageformat_jpg_obj = {
+ { &camera_imageformat_type },
+};
+
+const camera_imageformat_obj_t camera_imageformat_rgb565_obj = {
+ { &camera_imageformat_type },
+};
+
+camera_imageformat_t camera_imageformat_obj_to_type(mp_obj_t obj) {
+ if (obj == MP_ROM_PTR(&camera_imageformat_jpg_obj)) {
+ return IMAGEFORMAT_JPG;
+ } else if (obj == MP_ROM_PTR(&camera_imageformat_rgb565_obj)) {
+ return IMAGEFORMAT_RGB565;
+ }
+ return IMAGEFORMAT_NONE;
+}
+
+mp_obj_t camera_imageformat_type_to_obj(camera_imageformat_t format) {
+ switch (format) {
+ case IMAGEFORMAT_JPG:
+ return (mp_obj_t)MP_ROM_PTR(&camera_imageformat_jpg_obj);
+ case IMAGEFORMAT_RGB565:
+ return (mp_obj_t)MP_ROM_PTR(&camera_imageformat_rgb565_obj);
+ case IMAGEFORMAT_NONE:
+ default:
+ return MP_ROM_NONE;
+ }
+}
+
+STATIC const mp_rom_map_elem_t camera_imageformat_locals_dict_table[] = {
+ {MP_ROM_QSTR(MP_QSTR_JPG), MP_ROM_PTR(&camera_imageformat_jpg_obj)},
+ {MP_ROM_QSTR(MP_QSTR_RGB565), MP_ROM_PTR(&camera_imageformat_rgb565_obj)},
+};
+STATIC MP_DEFINE_CONST_DICT(camera_imageformat_locals_dict, camera_imageformat_locals_dict_table);
+
+STATIC void camera_imageformat_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ qstr format = MP_QSTR_None;
+ if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imageformat_jpg_obj)) {
+ format = MP_QSTR_JPG;
+ } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imageformat_rgb565_obj)) {
+ format = MP_QSTR_RGB565;
+ }
+ mp_printf(print, "%q.%q.%q", MP_QSTR_camera, MP_QSTR_ImageSize, format);
+}
+
+const mp_obj_type_t camera_imageformat_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_ImageFormat,
+ .print = camera_imageformat_print,
+ .locals_dict = (mp_obj_t)&camera_imageformat_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/camera/ImageFormat.h b/circuitpython/shared-bindings/camera/ImageFormat.h
new file mode 100644
index 0000000..32a3635
--- /dev/null
+++ b/circuitpython/shared-bindings/camera/ImageFormat.h
@@ -0,0 +1,49 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
+
+#include "py/obj.h"
+
+typedef enum {
+ IMAGEFORMAT_NONE,
+ IMAGEFORMAT_JPG,
+ IMAGEFORMAT_RGB565,
+} camera_imageformat_t;
+
+extern const mp_obj_type_t camera_imageformat_type;
+
+camera_imageformat_t camera_imageformat_obj_to_type(mp_obj_t obj);
+mp_obj_t camera_imageformat_type_to_obj(camera_imageformat_t mode);
+
+typedef struct {
+ mp_obj_base_t base;
+} camera_imageformat_obj_t;
+extern const camera_imageformat_obj_t camera_imageformat_jpg_obj;
+extern const camera_imageformat_obj_t camera_imageformat_rgb565_obj;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
diff --git a/circuitpython/shared-bindings/camera/__init__.c b/circuitpython/shared-bindings/camera/__init__.c
new file mode 100644
index 0000000..12cebae
--- /dev/null
+++ b/circuitpython/shared-bindings/camera/__init__.c
@@ -0,0 +1,52 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "py/mphal.h"
+#include "shared-bindings/camera/Camera.h"
+#include "shared-bindings/util.h"
+
+//| """Support for camera input
+//|
+//| The `camera` module contains classes to control the camera and take pictures."""
+//|
+STATIC const mp_rom_map_elem_t camera_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_camera) },
+ { MP_ROM_QSTR(MP_QSTR_Camera), MP_ROM_PTR(&camera_type) },
+
+ // Enum-like Classes.
+ { MP_ROM_QSTR(MP_QSTR_ImageFormat), MP_ROM_PTR(&camera_imageformat_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(camera_module_globals, camera_module_globals_table);
+
+const mp_obj_module_t camera_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&camera_module_globals,
+};
+
+MP_REGISTER_MODULE(MP_QSTR_camera, camera_module, CIRCUITPY_CAMERA);