diff --git a/ports/cxd56/common-hal/camera/Camera.c b/ports/cxd56/common-hal/camera/Camera.c index 0c22525302..9376822c8a 100644 --- a/ports/cxd56/common-hal/camera/Camera.c +++ b/ports/cxd56/common-hal/camera/Camera.c @@ -35,9 +35,6 @@ #include "shared-bindings/camera/Camera.h" -#define JPG_COMPRESS_RATIO (9) -#define SPRESENSE_CAMIMAGE_MEM_ALIGN (32) - typedef struct { const char* devpath; int fd; @@ -45,39 +42,36 @@ typedef struct { STATIC camera_dev_t camera_dev = {"/dev/video", -1}; -static void camera_size_to_width_and_height(camera_imagesize_t size, uint16_t *width, uint16_t *height) { - switch (size) { - case IMAGESIZE_320x240: - *height = VIDEO_VSIZE_QVGA; - *width = VIDEO_HSIZE_QVGA; - break; - case IMAGESIZE_640x320: - *height = VIDEO_VSIZE_VGA; - *width = VIDEO_HSIZE_VGA; - break; - case IMAGESIZE_1280x720: - *height = VIDEO_VSIZE_HD; - *width = VIDEO_HSIZE_HD; - break; - case IMAGESIZE_1280x960: - *height = VIDEO_VSIZE_QUADVGA; - *width = VIDEO_HSIZE_QUADVGA; - break; - case IMAGESIZE_1920x1080: - *height = VIDEO_VSIZE_FULLHD; - *width = VIDEO_HSIZE_FULLHD; - break; - case IMAGESIZE_2048x1536: - *height = VIDEO_VSIZE_3M; - *width = VIDEO_HSIZE_3M; - break; - case IMAGESIZE_2560x1920: - *height = VIDEO_VSIZE_5M; - *width = VIDEO_HSIZE_5M; - break; - default: - mp_raise_ValueError(translate("Size not supported")); - break; +static bool camera_check_width_and_height(uint16_t width, uint16_t height) { + if ((width == VIDEO_HSIZE_QVGA && height == VIDEO_VSIZE_QVGA) || + (width == VIDEO_HSIZE_VGA && height == VIDEO_VSIZE_VGA) || + (width == VIDEO_HSIZE_HD && height == VIDEO_VSIZE_HD) || + (width == VIDEO_HSIZE_QUADVGA && height == VIDEO_VSIZE_QUADVGA) || + (width == VIDEO_HSIZE_FULLHD && height == VIDEO_VSIZE_FULLHD) || + (width == VIDEO_HSIZE_3M && height == VIDEO_VSIZE_3M) || + (width == VIDEO_HSIZE_5M && height == VIDEO_VSIZE_5M)) { + return true; + } else { + 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) ? true : false; + } else { + return false; + } +} + +static bool camera_check_format(camera_imageformat_t format) { + if (format == IMAGEFORMAT_JPG) { + return true; + } else { + return false; } } @@ -118,7 +112,9 @@ static void camera_start_preview() { camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE); } -void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size) { +extern uint32_t _ebss; +extern uint32_t _stext; +void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height) { if (camera_dev.fd < 0) { if (video_initialize(camera_dev.devpath) < 0) { mp_raise_ValueError(translate("Could not initialize Camera")); @@ -129,25 +125,13 @@ void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size) { } } - uint16_t width, height; - - camera_size_to_width_and_height(size, &width, &height); - - self->size = size; - - // 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. - self->buffer_size = (size_t)(width * height * 2 / JPG_COMPRESS_RATIO);; - self->buffer = m_malloc(self->buffer_size, true); - if (self->buffer == NULL) { - mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate picture buffer")); - } - self->picture_buffer = self->buffer; - while ((uint32_t)self->picture_buffer % SPRESENSE_CAMIMAGE_MEM_ALIGN != 0) { - self->picture_buffer++; + 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); @@ -164,8 +148,6 @@ void common_hal_camera_deinit(camera_obj_t *self) { video_uninitialize(); - m_free(self->buffer); - close(camera_dev.fd); camera_dev.fd = -1; } @@ -174,14 +156,26 @@ bool common_hal_camera_deinited(camera_obj_t *self) { return camera_dev.fd < 0; } -void common_hal_camera_take_picture(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) { + if (!camera_check_width_and_height(self->width, self->height)) { + mp_raise_ValueError(translate("Size not supported")); + } + if (!camera_check_buffer_length(self->width, self->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); + 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)self->picture_buffer; - buf.length = self->buffer_size - SPRESENSE_CAMIMAGE_MEM_ALIGN; + 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); @@ -190,35 +184,21 @@ void common_hal_camera_take_picture(camera_obj_t *self) { ioctl(camera_dev.fd, VIDIOC_TAKEPICT_STOP, false); - self->picture_size = (size_t)buf.bytesused; + return (size_t)buf.bytesused; } -uint8_t *common_hal_camera_get_picture_buffer(camera_obj_t *self) { - return self->picture_buffer; +uint16_t common_hal_camera_get_width(camera_obj_t *self) { + return self->width; } -size_t common_hal_camera_get_picture_size(camera_obj_t *self) { - return self->picture_size; +void common_hal_camera_set_width(camera_obj_t *self, uint16_t width) { + self->width = width; } -camera_imagesize_t common_hal_camera_get_size(camera_obj_t *self) { - return self->size; +uint16_t common_hal_camera_get_height(camera_obj_t *self) { + return self->height; } -void common_hal_camera_set_size(camera_obj_t *self, camera_imagesize_t size) { - uint16_t width, height; - - camera_size_to_width_and_height(size, &width, &height); - - self->buffer_size = (size_t)(width * height * 2 / JPG_COMPRESS_RATIO);; - self->buffer = m_realloc(self->buffer, self->buffer_size); - if (self->buffer == NULL) { - mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate picture buffer")); - } - self->picture_buffer = self->buffer; - while ((uint32_t)self->picture_buffer % SPRESENSE_CAMIMAGE_MEM_ALIGN != 0) { - self->picture_buffer++; - } - - camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height); +void common_hal_camera_set_height(camera_obj_t *self, uint16_t height) { + self->height = height; } diff --git a/ports/cxd56/common-hal/camera/Camera.h b/ports/cxd56/common-hal/camera/Camera.h index 1eb63ace11..11fc102085 100644 --- a/ports/cxd56/common-hal/camera/Camera.h +++ b/ports/cxd56/common-hal/camera/Camera.h @@ -29,15 +29,10 @@ #include "py/obj.h" -#include "shared-bindings/camera/ImageSize.h" - typedef struct { mp_obj_base_t base; - uint8_t *buffer; - size_t buffer_size; - uint8_t *picture_buffer; - size_t picture_size; - camera_imagesize_t size; + uint16_t width; + uint16_t height; } camera_obj_t; #endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_CAMERA_CAMERA_H diff --git a/ports/cxd56/common-hal/camera/ImageSize.c b/ports/cxd56/common-hal/camera/ImageSize.c deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index f72c821e53..6e98af8686 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -315,7 +315,6 @@ SRC_COMMON_HAL_ALL = \ busio/__init__.c \ camera/__init__.c \ camera/Camera.c \ - camera/ImageSize.c \ countio/Counter.c \ countio/__init__.c \ digitalio/DigitalInOut.c \ @@ -386,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/shared-bindings/camera/Camera.c b/shared-bindings/camera/Camera.c index 3855fed3c7..3b839d802f 100644 --- a/shared-bindings/camera/Camera.c +++ b/shared-bindings/camera/Camera.c @@ -48,37 +48,38 @@ //| vfs = storage.VfsFat(sd) //| storage.mount(vfs, '/sd') //| -//| cam = camera.Camera(camera.ImageSize.IMAGE_SIZE_1920x1080) +//| cam = camera.Camera(1920, 1080) //| +//| buffer = bytearray(512 * 1024) //| file = open("/sd/image.jpg","wb") -//| cam.take_picture() -//| file.write(cam.picture) +//| size = cam.take_picture() +//| file.write(buffer, size) //| file.close()""" //| -//| def __init__(self, ): +//| def __init__(self, width: int, height: int) -> None: //| """Initialize camera. //| -//| :param camera.ImageSize size: image size""" +//| :param int width: Width in pixels +//| :param int height: Height in pixels""" //| ... //| 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_size }; + enum { ARG_width, ARG_height }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { 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); - camera_imagesize_t size = camera_imagesize_obj_to_type(args[ARG_size].u_obj); - - common_hal_camera_construct(self, size); + common_hal_camera_construct(self, args[ARG_width].u_int, args[ARG_height].u_int); return MP_OBJ_FROM_PTR(self); } -//| def deinit(self, ) -> Any: +//| def deinit(self) -> None: //| """De-initialize camera.""" //| ... //| @@ -95,69 +96,84 @@ STATIC void check_for_deinit(camera_obj_t *self) { } } -//| def take_picture(self, ) -> Any: -//| """Take picture.""" +//| def take_picture(self, buf: WriteableBuffer, format: ImageFormat) -> int: +//| """Take picture and save to ``buf`` in the given ``format`` +//| +//| :return: the size of the picture taken +//| :rtype: int""" //| ... //| -STATIC mp_obj_t camera_obj_take_picture(mp_obj_t self_in) { +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 }; + 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 }, + }; + 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, 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_take_picture(self); + common_hal_camera_set_width(self, mp_obj_get_int(value)); + return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(camera_take_picture_obj, camera_obj_take_picture); +MP_DEFINE_CONST_FUN_OBJ_2(camera_set_width_obj, camera_obj_set_width); -//| picture: Any = ... -//| """Image buffer.""" -//| -STATIC mp_obj_t camera_obj_get_picture(mp_obj_t self_in) { - camera_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - - uint8_t *buffer = common_hal_camera_get_picture_buffer(self); - size_t size = common_hal_camera_get_picture_size(self); - - return mp_obj_new_bytearray_by_ref(size, buffer); -} -MP_DEFINE_CONST_FUN_OBJ_1(camera_get_picture_obj, camera_obj_get_picture); - -const mp_obj_property_t camera_picture_obj = { +const mp_obj_property_t camera_width_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&camera_get_picture_obj, - (mp_obj_t)&mp_const_none_obj, + .proxy = {(mp_obj_t)&camera_get_width_obj, + (mp_obj_t)&camera_set_width_obj, (mp_obj_t)&mp_const_none_obj}, }; -//| size: Any = ... -//| """Image size.""" +//| height: int +//| """Image height in pixels.""" //| -STATIC mp_obj_t camera_obj_get_size(mp_obj_t self_in) { +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 camera_imagesize_type_to_obj(common_hal_camera_get_size(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_camera_get_height(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(camera_get_size_obj, camera_obj_get_size); +MP_DEFINE_CONST_FUN_OBJ_1(camera_get_height_obj, camera_obj_get_height); -STATIC mp_obj_t camera_obj_set_size(mp_obj_t self_in, mp_obj_t value) { +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); - camera_imagesize_t size = camera_imagesize_obj_to_type(value); - if (size == IMAGESIZE_NONE) { - mp_raise_ValueError(translate("Invalid image size.")); - } - - common_hal_camera_set_size(self, size); + common_hal_camera_set_height(self, mp_obj_get_int(value)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(camera_set_size_obj, camera_obj_set_size); +MP_DEFINE_CONST_FUN_OBJ_2(camera_set_height_obj, camera_obj_set_height); -const mp_obj_property_t camera_size_obj = { +const mp_obj_property_t camera_height_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&camera_get_size_obj, - (mp_obj_t)&camera_set_size_obj, + .proxy = {(mp_obj_t)&camera_get_height_obj, + (mp_obj_t)&camera_set_height_obj, (mp_obj_t)&mp_const_none_obj}, }; @@ -165,14 +181,14 @@ 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_picture), MP_ROM_PTR(&camera_picture_obj) }, - { MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&camera_size_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); const mp_obj_type_t camera_type = { { &mp_type_type }, - .name = MP_QSTR_GNSS, + .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 index e70d2493ec..7ef13cd07e 100644 --- a/shared-bindings/camera/Camera.h +++ b/shared-bindings/camera/Camera.h @@ -28,18 +28,18 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H #include "common-hal/camera/Camera.h" -#include "shared-bindings/camera/ImageSize.h" +#include "shared-bindings/camera/ImageFormat.h" extern const mp_obj_type_t camera_type; -void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size); +void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height); void common_hal_camera_deinit(camera_obj_t *self); bool common_hal_camera_deinited(camera_obj_t *self); -void common_hal_camera_take_picture(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); -uint8_t* common_hal_camera_get_picture_buffer(camera_obj_t *self); -size_t common_hal_camera_get_picture_size(camera_obj_t *self); -camera_imagesize_t common_hal_camera_get_size(camera_obj_t *self); -void common_hal_camera_set_size(camera_obj_t *self, camera_imagesize_t size); +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); #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/ImageSize.h b/shared-bindings/camera/ImageFormat.h similarity index 55% rename from shared-bindings/camera/ImageSize.h rename to shared-bindings/camera/ImageFormat.h index 3fe624e066..8abc88438d 100644 --- a/shared-bindings/camera/ImageSize.h +++ b/shared-bindings/camera/ImageFormat.h @@ -24,36 +24,26 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGESIZE_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGESIZE_H +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H #include "py/obj.h" typedef enum { - IMAGESIZE_NONE, - IMAGESIZE_320x240, - IMAGESIZE_640x320, - IMAGESIZE_1280x720, - IMAGESIZE_1280x960, - IMAGESIZE_1920x1080, - IMAGESIZE_2048x1536, - IMAGESIZE_2560x1920, -} camera_imagesize_t; + IMAGEFORMAT_NONE, + IMAGEFORMAT_JPG, + IMAGEFORMAT_RGB565, +} camera_imageformat_t; -const mp_obj_type_t camera_imagesize_type; +const mp_obj_type_t camera_imageformat_type; -camera_imagesize_t camera_imagesize_obj_to_type(mp_obj_t obj); -mp_obj_t camera_imagesize_type_to_obj(camera_imagesize_t mode); +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_imagesize_obj_t; -extern const camera_imagesize_obj_t camera_imagesize_320x240_obj; -extern const camera_imagesize_obj_t camera_imagesize_640x320_obj; -extern const camera_imagesize_obj_t camera_imagesize_1280x720_obj; -extern const camera_imagesize_obj_t camera_imagesize_1280x960_obj; -extern const camera_imagesize_obj_t camera_imagesize_1920x1080_obj; -extern const camera_imagesize_obj_t camera_imagesize_2048x1536_obj; -extern const camera_imagesize_obj_t camera_imagesize_2560x1920_obj; +} 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_IMAGESIZE_H +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H diff --git a/shared-bindings/camera/ImageSize.c b/shared-bindings/camera/ImageSize.c deleted file mode 100644 index bfa01814d9..0000000000 --- a/shared-bindings/camera/ImageSize.c +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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/ImageSize.h" - -//| class ImageSize: -//| """Image size""" -//| -//| def __init__(self) -> None: -//| """Enum-like class to define the image size.""" -//| -//| IMAGE_SIZE_320x240: ImageSize -//| """Image size 320x240.""" -//| -//| IMAGE_SIZE_640x480: ImageSize -//| """Image size 640x480.""" -//| -//| IMAGE_SIZE_1280x720: ImageSize -//| """Image size 1280x720.""" -//| -//| IMAGE_SIZE_1280x960: ImageSize -//| """Image size 1280x960.""" -//| -//| IMAGE_SIZE_1920x1080: ImageSize -//| """Image size 1920x1080.""" -//| -//| IMAGE_SIZE_2048x1536: ImageSize -//| """Image size 2048x1536.""" -//| -//| IMAGE_SIZE_2560x1920: ImageSize -//| """Image size 2560x1920.""" -//| -const mp_obj_type_t camera_imagesize_type; - -const camera_imagesize_obj_t camera_imagesize_320x240_obj = { - { &camera_imagesize_type }, -}; - -const camera_imagesize_obj_t camera_imagesize_640x320_obj = { - { &camera_imagesize_type }, -}; - -const camera_imagesize_obj_t camera_imagesize_1280x720_obj = { - { &camera_imagesize_type }, -}; - -const camera_imagesize_obj_t camera_imagesize_1280x960_obj = { - { &camera_imagesize_type }, -}; - -const camera_imagesize_obj_t camera_imagesize_1920x1080_obj = { - { &camera_imagesize_type }, -}; - -const camera_imagesize_obj_t camera_imagesize_2048x1536_obj = { - { &camera_imagesize_type }, -}; - -const camera_imagesize_obj_t camera_imagesize_2560x1920_obj = { - { &camera_imagesize_type }, -}; - -camera_imagesize_t camera_imagesize_obj_to_type(mp_obj_t obj) { - if (obj == MP_ROM_PTR(&camera_imagesize_320x240_obj)) { - return IMAGESIZE_320x240; - } else if (obj == MP_ROM_PTR(&camera_imagesize_640x320_obj)) { - return IMAGESIZE_640x320; - } else if (obj == MP_ROM_PTR(&camera_imagesize_1280x720_obj)) { - return IMAGESIZE_1280x720; - } else if (obj == MP_ROM_PTR(&camera_imagesize_1280x960_obj)) { - return IMAGESIZE_1280x960; - } else if (obj == MP_ROM_PTR(&camera_imagesize_1920x1080_obj)) { - return IMAGESIZE_1920x1080; - } else if (obj == MP_ROM_PTR(&camera_imagesize_2048x1536_obj)) { - return IMAGESIZE_2048x1536; - } else if (obj == MP_ROM_PTR(&camera_imagesize_2560x1920_obj)) { - return IMAGESIZE_2560x1920; - } - return IMAGESIZE_NONE; -} - -mp_obj_t camera_imagesize_type_to_obj(camera_imagesize_t size) { - switch (size) { - case IMAGESIZE_320x240: - return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_320x240_obj); - case IMAGESIZE_640x320: - return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_640x320_obj); - case IMAGESIZE_1280x720: - return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_1280x720_obj); - case IMAGESIZE_1280x960: - return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_1280x960_obj); - case IMAGESIZE_1920x1080: - return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_1920x1080_obj); - case IMAGESIZE_2048x1536: - return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_2048x1536_obj); - case IMAGESIZE_2560x1920: - return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_2560x1920_obj); - case IMAGESIZE_NONE: - default: - return (mp_obj_t)MP_ROM_PTR(&mp_const_none_obj); - } -} - -STATIC const mp_rom_map_elem_t camera_imagesize_locals_dict_table[] = { - {MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_320x240), MP_ROM_PTR(&camera_imagesize_320x240_obj)}, - {MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_640x320), MP_ROM_PTR(&camera_imagesize_640x320_obj)}, - {MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_1280x720), MP_ROM_PTR(&camera_imagesize_1280x720_obj)}, - {MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_1280x960), MP_ROM_PTR(&camera_imagesize_1280x960_obj)}, - {MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_1920x1080), MP_ROM_PTR(&camera_imagesize_1920x1080_obj)}, - {MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_2048x1536), MP_ROM_PTR(&camera_imagesize_2048x1536_obj)}, - {MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_2560x1920), MP_ROM_PTR(&camera_imagesize_2560x1920_obj)}, -}; -STATIC MP_DEFINE_CONST_DICT(camera_imagesize_locals_dict, camera_imagesize_locals_dict_table); - -STATIC void camera_imagesize_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr size = MP_QSTR_None; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_320x240_obj)) { - size = MP_QSTR_IMAGE_SIZE_320x240; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_640x320_obj)) { - size = MP_QSTR_IMAGE_SIZE_640x320; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_1280x720_obj)) { - size = MP_QSTR_IMAGE_SIZE_1280x720; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_1280x960_obj)) { - size = MP_QSTR_IMAGE_SIZE_1280x960; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_1920x1080_obj)) { - size = MP_QSTR_IMAGE_SIZE_1920x1080; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_2048x1536_obj)) { - size = MP_QSTR_IMAGE_SIZE_2048x1536; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_2560x1920_obj)) { - size = MP_QSTR_IMAGE_SIZE_2560x1920; - } - mp_printf(print, "%q.%q.%q", MP_QSTR_camera, MP_QSTR_ImageSize, size); -} - -const mp_obj_type_t camera_imagesize_type = { - { &mp_type_type }, - .name = MP_QSTR_ImageSize, - .print = camera_imagesize_print, - .locals_dict = (mp_obj_t)&camera_imagesize_locals_dict, -}; diff --git a/shared-bindings/camera/__init__.c b/shared-bindings/camera/__init__.c index 3398d1ade6..28cf7df575 100644 --- a/shared-bindings/camera/__init__.c +++ b/shared-bindings/camera/__init__.c @@ -39,7 +39,7 @@ STATIC const mp_rom_map_elem_t camera_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Camera), MP_ROM_PTR(&camera_type) }, // Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_ImageSize), MP_ROM_PTR(&camera_imagesize_type) }, + { MP_ROM_QSTR(MP_QSTR_ImageSize), MP_ROM_PTR(&camera_imageformat_type) }, }; STATIC MP_DEFINE_CONST_DICT(camera_module_globals, camera_module_globals_table);