diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 351d30e651..8f6401d91a 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-09-13 14:21-0500\n" +"POT-Creation-Date: 2020-09-11 13:30+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -430,7 +430,7 @@ msgstr "" msgid "Buffer is not a bytearray." msgstr "" -#: shared-bindings/displayio/Display.c +#: ports/cxd56/common-hal/camera/Camera.c shared-bindings/displayio/Display.c #: shared-bindings/framebufferio/FramebufferDisplay.c msgid "Buffer is too small" msgstr "" @@ -615,6 +615,10 @@ msgstr "" msgid "Corrupt raw code" msgstr "" +#: ports/cxd56/common-hal/camera/Camera.c +msgid "Could not initialize Camera" +msgstr "" + #: ports/cxd56/common-hal/gnss/GNSS.c msgid "Could not initialize GNSS" msgstr "" @@ -841,6 +845,10 @@ msgstr "" msgid "File exists" msgstr "" +#: ports/cxd56/common-hal/camera/Camera.c +msgid "Format not supported" +msgstr "" + #: shared-module/framebufferio/FramebufferDisplay.c #, c-format msgid "Framebuffer requires %d bytes" @@ -1503,6 +1511,10 @@ msgstr "" msgid "Server side context cannot have hostname" msgstr "" +#: ports/cxd56/common-hal/camera/Camera.c +msgid "Size not supported" +msgstr "" + #: shared-bindings/nvm/ByteArray.c msgid "Slice and value different lengths." msgstr "" diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index bb3958af4f..7e145f5e2d 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -201,7 +201,7 @@ all: $(BUILD)/firmware.spk $(FIRMWARE): $(ECHO) "" $(ECHO) "Download the spresense binaries zip archive from:" - $(ECHO) "https://developer.sony.com/file/download/download-spresense-firmware-v1-4-000" + $(ECHO) "https://developer.sony.com/file/download/download-spresense-firmware-v2-0-000" $(ECHO) "Extract spresense binaries to $(FIRMWARE)" $(ECHO) "" $(ECHO) "run make flash-bootloader again to flash bootloader." diff --git a/ports/cxd56/common-hal/camera/Camera.c b/ports/cxd56/common-hal/camera/Camera.c new file mode 100644 index 0000000000..35e2ab8822 --- /dev/null +++ b/ports/cxd56/common-hal/camera/Camera.c @@ -0,0 +1,183 @@ +/* + * 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 +#include + +#include +#include +#include + +#include "py/runtime.h" + +#include "shared-bindings/camera/Camera.h" + +typedef struct { + const char* devpath; + int fd; +} camera_dev_t; + +STATIC camera_dev_t camera_dev = {"/dev/video", -1}; + +typedef struct { + uint16_t width; + uint16_t height; +} image_size_t; + +STATIC const image_size_t image_size_table[] = { + { VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA }, + { VIDEO_HSIZE_VGA, VIDEO_VSIZE_VGA }, + { VIDEO_HSIZE_HD, VIDEO_VSIZE_HD }, + { VIDEO_HSIZE_QUADVGA, VIDEO_VSIZE_QUADVGA }, + { VIDEO_HSIZE_FULLHD, VIDEO_VSIZE_FULLHD }, + { VIDEO_HSIZE_3M, VIDEO_VSIZE_3M }, + { VIDEO_HSIZE_5M, VIDEO_VSIZE_5M }, +}; + +static bool camera_check_width_and_height(uint16_t width, uint16_t height) { + for (int i = 0; i < MP_ARRAY_SIZE(image_size_table); i++) { + if (image_size_table[i].width == width && image_size_table[i].height == height) { + return true; + } + } + return false; +} + +static bool camera_check_buffer_length(uint16_t width, uint16_t height, camera_imageformat_t format, size_t length) { + if (format == IMAGEFORMAT_JPG) { + // In SPRESENSE SDK, JPEG compression quality=80 by default. + // In such setting, the maximum actual measured size of JPEG image + // is about width * height * 2 / 9. + return length >= (size_t)(width * height * 2 / 9); + } else { + return false; + } +} + +static bool camera_check_format(camera_imageformat_t format) { + return format == IMAGEFORMAT_JPG; +} + +static void camera_set_format(enum v4l2_buf_type type, uint32_t pixformat, uint16_t width, uint16_t height) { + v4l2_requestbuffers_t req = {0}; + + // Set Buffer Mode. + req.type = type; + req.memory = V4L2_MEMORY_USERPTR; + req.count = 1; + req.mode = V4L2_BUF_MODE_RING; + ioctl(camera_dev.fd, VIDIOC_REQBUFS, (unsigned long)&req); + v4l2_format_t fmt = {0}; + + // Set Format. + fmt.type = type; + fmt.fmt.pix.width = width; + fmt.fmt.pix.height = height; + fmt.fmt.pix.field = V4L2_FIELD_ANY; + fmt.fmt.pix.pixelformat = pixformat; + ioctl(camera_dev.fd, VIDIOC_S_FMT, (unsigned long)&fmt); +} + +static void camera_start_streaming(enum v4l2_buf_type type) { + ioctl(camera_dev.fd, VIDIOC_STREAMON, (unsigned long)&type); +} + +static void camera_start_preview() { + camera_set_format(V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_PIX_FMT_UYVY, VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA); + + v4l2_buffer_t buf; + + memset(&buf, 0, sizeof(v4l2_buffer_t)); + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_USERPTR; + ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf); + + camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE); +} + +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")); + } + camera_dev.fd = open(camera_dev.devpath, 0); + if (camera_dev.fd < 0) { + mp_raise_ValueError(translate("Could not initialize Camera")); + } + } + + camera_start_preview(); + + camera_start_streaming(V4L2_BUF_TYPE_STILL_CAPTURE); + + sleep(1); +} + +void common_hal_camera_deinit(camera_obj_t *self) { + if (common_hal_camera_deinited(self)) { + return; + } + + video_uninitialize(); + + close(camera_dev.fd); + camera_dev.fd = -1; +} + +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, 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(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, width, height); + + v4l2_buffer_t buf; + + memset(&buf, 0, sizeof(v4l2_buffer_t)); + buf.type = V4L2_BUF_TYPE_STILL_CAPTURE; + buf.memory = V4L2_MEMORY_USERPTR; + buf.m.userptr = (unsigned long)buffer; + buf.length = len; + ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf); + + ioctl(camera_dev.fd, VIDIOC_TAKEPICT_START, 0); + + ioctl(camera_dev.fd, VIDIOC_DQBUF, (unsigned long)&buf); + + ioctl(camera_dev.fd, VIDIOC_TAKEPICT_STOP, false); + + return (size_t)buf.bytesused; +} diff --git a/ports/cxd56/common-hal/camera/Camera.h b/ports/cxd56/common-hal/camera/Camera.h new file mode 100644 index 0000000000..7056237d16 --- /dev/null +++ b/ports/cxd56/common-hal/camera/Camera.h @@ -0,0 +1,36 @@ +/* + * 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_CXD56_COMMON_HAL_CAMERA_CAMERA_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_CAMERA_CAMERA_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} camera_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_CAMERA_CAMERA_H diff --git a/ports/cxd56/common-hal/camera/__init__.c b/ports/cxd56/common-hal/camera/__init__.c new file mode 100644 index 0000000000..bf38b5f2bd --- /dev/null +++ b/ports/cxd56/common-hal/camera/__init__.c @@ -0,0 +1 @@ +// No camera module functions. diff --git a/ports/cxd56/configs/circuitpython/defconfig b/ports/cxd56/configs/circuitpython/defconfig index 268f7a68e9..a97f821dfa 100644 --- a/ports/cxd56/configs/circuitpython/defconfig +++ b/ports/cxd56/configs/circuitpython/defconfig @@ -165,7 +165,6 @@ CONFIG_USBDEV=y CONFIG_USBDEV_DMA=y CONFIG_USBDEV_DUALSPEED=y CONFIG_USEC_PER_TICK=1000 -CONFIG_USERMAIN_STACKSIZE=1064960 CONFIG_USER_ENTRYPOINT="spresense_main" CONFIG_VIDEO_ISX012=y CONFIG_VIDEO_STREAM=y diff --git a/ports/cxd56/mpconfigport.h b/ports/cxd56/mpconfigport.h index df87946b95..27c82337bc 100644 --- a/ports/cxd56/mpconfigport.h +++ b/ports/cxd56/mpconfigport.h @@ -27,13 +27,15 @@ #ifndef __INCLUDED_MPCONFIGPORT_H #define __INCLUDED_MPCONFIGPORT_H -#define MICROPY_PY_SYS_PLATFORM "CXD56" +#define MICROPY_PY_SYS_PLATFORM "CXD56" // 64kiB stack -#define CIRCUITPY_DEFAULT_STACK_SIZE 0x10000 +#define CIRCUITPY_DEFAULT_STACK_SIZE (0x10000) #include "py/circuitpy_mpconfig.h" +#define MICROPY_BYTES_PER_GC_BLOCK (32) + #define MICROPY_PORT_ROOT_POINTERS \ CIRCUITPY_COMMON_ROOT_POINTERS \ diff --git a/ports/cxd56/mpconfigport.mk b/ports/cxd56/mpconfigport.mk index 914e0b37d5..e1ffc79d08 100644 --- a/ports/cxd56/mpconfigport.mk +++ b/ports/cxd56/mpconfigport.mk @@ -11,6 +11,7 @@ MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_AUDIOIO = 0 +CIRCUITPY_CAMERA = 1 CIRCUITPY_COUNTIO = 0 CIRCUITPY_DISPLAYIO = 0 CIRCUITPY_FREQUENCYIO = 0 diff --git a/ports/cxd56/spresense-exported-sdk b/ports/cxd56/spresense-exported-sdk index c991d439fa..752c4cd56d 160000 --- a/ports/cxd56/spresense-exported-sdk +++ b/ports/cxd56/spresense-exported-sdk @@ -1 +1 @@ -Subproject commit c991d439fac9c23cfcac0da16fe8055f818d40a4 +Subproject commit 752c4cd56dd0a270a559c28272ceb61ddcb7806c diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c index 7038ca4782..6164f74113 100644 --- a/ports/cxd56/supervisor/port.c +++ b/ports/cxd56/supervisor/port.c @@ -46,12 +46,22 @@ #include "common-hal/pwmio/PWMOut.h" #include "common-hal/busio/UART.h" +#define SPRESENSE_MEM_ALIGN (32) + +uint32_t* heap; +uint32_t heap_size; + safe_mode_t port_init(void) { boardctl(BOARDIOC_INIT, 0); // Wait until RTC is available while (g_rtc_enabled == false); + heap = memalign(SPRESENSE_MEM_ALIGN, 128 * 1024); + uint32_t size = CONFIG_RAM_START + CONFIG_RAM_SIZE - (uint32_t)heap - 2 * SPRESENSE_MEM_ALIGN; + heap = realloc(heap, size); + heap_size = size / sizeof(uint32_t); + if (board_requests_safe_mode()) { return USER_SAFE_MODE; } @@ -100,11 +110,11 @@ uint32_t *port_stack_get_top(void) { } uint32_t *port_heap_get_bottom(void) { - return port_stack_get_limit(); + return heap; } uint32_t *port_heap_get_top(void) { - return port_stack_get_top(); + return heap + heap_size; } extern uint32_t _ebss; diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 0e87287c13..6e98af8686 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -139,6 +139,9 @@ endif ifeq ($(CIRCUITPY_BUSIO),1) SRC_PATTERNS += busio/% bitbangio/OneWire.% endif +ifeq ($(CIRCUITPY_CAMERA),1) +SRC_PATTERNS += camera/% +endif ifeq ($(CIRCUITPY_COUNTIO),1) SRC_PATTERNS += countio/% endif @@ -310,6 +313,8 @@ SRC_COMMON_HAL_ALL = \ busio/SPI.c \ busio/UART.c \ busio/__init__.c \ + camera/__init__.c \ + camera/Camera.c \ countio/Counter.c \ countio/__init__.c \ digitalio/DigitalInOut.c \ @@ -380,6 +385,7 @@ $(filter $(SRC_PATTERNS), \ _bleio/Attribute.c \ _bleio/ScanEntry.c \ _eve/__init__.c \ + camera/ImageFormat.c \ digitalio/Direction.c \ digitalio/DriveMode.c \ digitalio/Pull.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 0fafbe876d..4272de2ec7 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -329,6 +329,13 @@ extern const struct _mp_obj_module_t busio_module; #define BUSIO_MODULE #endif +#if CIRCUITPY_CAMERA +extern const struct _mp_obj_module_t camera_module; +#define CAMERA_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_camera), (mp_obj_t)&camera_module }, +#else +#define CAMERA_MODULE +#endif + #if CIRCUITPY_COUNTIO extern const struct _mp_obj_module_t countio_module; #define COUNTIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_countio), (mp_obj_t)&countio_module }, @@ -758,6 +765,7 @@ extern const struct _mp_obj_module_t wifi_module; BLEIO_MODULE \ BOARD_MODULE \ BUSIO_MODULE \ + CAMERA_MODULE \ COUNTIO_MODULE \ DIGITALIO_MODULE \ DISPLAYIO_MODULE \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index a2da9c42f5..54bdefc6dd 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -90,6 +90,9 @@ CFLAGS += -DCIRCUITPY_BOARD=$(CIRCUITPY_BOARD) CIRCUITPY_BUSIO ?= 1 CFLAGS += -DCIRCUITPY_BUSIO=$(CIRCUITPY_BUSIO) +CIRCUITPY_CAMERA ?= 0 +CFLAGS += -DCIRCUITPY_CAMERA=$(CIRCUITPY_CAMERA) + CIRCUITPY_DIGITALIO ?= 1 CFLAGS += -DCIRCUITPY_DIGITALIO=$(CIRCUITPY_DIGITALIO) diff --git a/shared-bindings/camera/Camera.c b/shared-bindings/camera/Camera.c new file mode 100644 index 0000000000..fd88ccab00 --- /dev/null +++ b/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, 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; + // No arguments + mp_arg_check_num(n_args, kw_args, 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, 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) }, +}; +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/shared-bindings/camera/Camera.h b/shared-bindings/camera/Camera.h new file mode 100644 index 0000000000..8102672e49 --- /dev/null +++ b/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/shared-bindings/camera/ImageFormat.c b/shared-bindings/camera/ImageFormat.c new file mode 100644 index 0000000000..d4bdddc562 --- /dev/null +++ b/shared-bindings/camera/ImageFormat.c @@ -0,0 +1,93 @@ +/* + * 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 mp_obj_type_t camera_imageformat_type; + +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_obj_t)MP_ROM_PTR(&mp_const_none_obj); + } +} + +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/shared-bindings/camera/ImageFormat.h b/shared-bindings/camera/ImageFormat.h new file mode 100644 index 0000000000..8abc88438d --- /dev/null +++ b/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; + +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/shared-bindings/camera/__init__.c b/shared-bindings/camera/__init__.c new file mode 100644 index 0000000000..080516d51c --- /dev/null +++ b/shared-bindings/camera/__init__.c @@ -0,0 +1,50 @@ +/* + * 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, +};