From ce7ee58e92eca840f75c2fcf885fe862f1ebec13 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Mon, 14 Sep 2020 12:55:53 +0200 Subject: [PATCH] camera: Update camera module --- ports/cxd56/common-hal/camera/Camera.c | 38 +++++++++++++++----------- ports/cxd56/supervisor/port.c | 8 ++++-- shared-bindings/camera/Camera.c | 4 +-- shared-bindings/camera/__init__.c | 2 +- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/ports/cxd56/common-hal/camera/Camera.c b/ports/cxd56/common-hal/camera/Camera.c index 9376822c8a..a4e197bf33 100644 --- a/ports/cxd56/common-hal/camera/Camera.c +++ b/ports/cxd56/common-hal/camera/Camera.c @@ -42,18 +42,28 @@ typedef struct { 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) { - 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; + 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) { @@ -61,18 +71,14 @@ static bool camera_check_buffer_length(uint16_t width, uint16_t height, camera_i // 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; + return length >= (size_t)(width * height * 2 / 9); } else { return false; } } static bool camera_check_format(camera_imageformat_t format) { - if (format == IMAGEFORMAT_JPG) { - return true; - } else { - return false; - } + return format == IMAGEFORMAT_JPG; } static void camera_set_format(enum v4l2_buf_type type, uint32_t pixformat, uint16_t width, uint16_t height) { diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c index 6a32dc0f9e..6164f74113 100644 --- a/ports/cxd56/supervisor/port.c +++ b/ports/cxd56/supervisor/port.c @@ -46,7 +46,7 @@ #include "common-hal/pwmio/PWMOut.h" #include "common-hal/busio/UART.h" -#define HEAP_SIZE (1000 * 1024) +#define SPRESENSE_MEM_ALIGN (32) uint32_t* heap; uint32_t heap_size; @@ -57,8 +57,10 @@ safe_mode_t port_init(void) { // Wait until RTC is available while (g_rtc_enabled == false); - heap = memalign(32, HEAP_SIZE); - heap_size = HEAP_SIZE / sizeof(uint32_t); + 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; diff --git a/shared-bindings/camera/Camera.c b/shared-bindings/camera/Camera.c index 3b839d802f..5fe54e429d 100644 --- a/shared-bindings/camera/Camera.c +++ b/shared-bindings/camera/Camera.c @@ -52,7 +52,7 @@ //| //| buffer = bytearray(512 * 1024) //| file = open("/sd/image.jpg","wb") -//| size = cam.take_picture() +//| size = cam.take_picture(buffer, camera.ImageFormat.JPG) //| file.write(buffer, size) //| file.close()""" //| @@ -99,7 +99,7 @@ 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`` //| -//| :return: the size of the picture taken +//| :return: the number of bytes written into buf //| :rtype: int""" //| ... //| diff --git a/shared-bindings/camera/__init__.c b/shared-bindings/camera/__init__.c index 28cf7df575..080516d51c 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_imageformat_type) }, + { MP_ROM_QSTR(MP_QSTR_ImageFormat), MP_ROM_PTR(&camera_imageformat_type) }, }; STATIC MP_DEFINE_CONST_DICT(camera_module_globals, camera_module_globals_table);