Skip to content

Commit 064c597

Browse files
committed
camera: Implement new library for camera
1 parent 814339a commit 064c597

File tree

8 files changed

+512
-0
lines changed

8 files changed

+512
-0
lines changed

py/circuitpy_defns.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ endif
139139
ifeq ($(CIRCUITPY_BUSIO),1)
140140
SRC_PATTERNS += busio/% bitbangio/OneWire.%
141141
endif
142+
ifeq ($(CIRCUITPY_CAMERA),1)
143+
SRC_PATTERNS += camera/%
144+
endif
142145
ifeq ($(CIRCUITPY_COUNTIO),1)
143146
SRC_PATTERNS += countio/%
144147
endif
@@ -310,6 +313,9 @@ SRC_COMMON_HAL_ALL = \
310313
busio/SPI.c \
311314
busio/UART.c \
312315
busio/__init__.c \
316+
camera/__init__.c \
317+
camera/Camera.c \
318+
camera/ImageSize.c \
313319
countio/Counter.c \
314320
countio/__init__.c \
315321
digitalio/DigitalInOut.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ extern const struct _mp_obj_module_t busio_module;
329329
#define BUSIO_MODULE
330330
#endif
331331

332+
#if CIRCUITPY_CAMERA
333+
extern const struct _mp_obj_module_t camera_module;
334+
#define CAMERA_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_camera), (mp_obj_t)&camera_module },
335+
#else
336+
#define CAMERA_MODULE
337+
#endif
338+
332339
#if CIRCUITPY_COUNTIO
333340
extern const struct _mp_obj_module_t countio_module;
334341
#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;
758765
BLEIO_MODULE \
759766
BOARD_MODULE \
760767
BUSIO_MODULE \
768+
CAMERA_MODULE \
761769
COUNTIO_MODULE \
762770
DIGITALIO_MODULE \
763771
DISPLAYIO_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ CFLAGS += -DCIRCUITPY_BOARD=$(CIRCUITPY_BOARD)
9090
CIRCUITPY_BUSIO ?= 1
9191
CFLAGS += -DCIRCUITPY_BUSIO=$(CIRCUITPY_BUSIO)
9292

93+
CIRCUITPY_CAMERA ?= 0
94+
CFLAGS += -DCIRCUITPY_CAMERA=$(CIRCUITPY_CAMERA)
95+
9396
CIRCUITPY_DIGITALIO ?= 1
9497
CFLAGS += -DCIRCUITPY_DIGITALIO=$(CIRCUITPY_DIGITALIO)
9598

shared-bindings/camera/Camera.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
};

shared-bindings/camera/Camera.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
29+
30+
#include "common-hal/camera/Camera.h"
31+
#include "shared-bindings/camera/ImageSize.h"
32+
33+
extern const mp_obj_type_t camera_type;
34+
35+
void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size);
36+
void common_hal_camera_deinit(camera_obj_t *self);
37+
bool common_hal_camera_deinited(camera_obj_t *self);
38+
void common_hal_camera_take_picture(camera_obj_t *self);
39+
40+
uint8_t* common_hal_camera_get_picture_buffer(camera_obj_t *self);
41+
size_t common_hal_camera_get_picture_size(camera_obj_t *self);
42+
camera_imagesize_t common_hal_camera_get_size(camera_obj_t *self);
43+
void common_hal_camera_set_size(camera_obj_t *self, camera_imagesize_t size);
44+
45+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H

0 commit comments

Comments
 (0)