Skip to content

Commit 61687c8

Browse files
committed
camera: Pass width and height to take_picture()
1 parent ce7ee58 commit 61687c8

File tree

4 files changed

+22
-118
lines changed

4 files changed

+22
-118
lines changed

ports/cxd56/common-hal/camera/Camera.c

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ static void camera_start_preview() {
118118
camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE);
119119
}
120120

121-
extern uint32_t _ebss;
122-
extern uint32_t _stext;
123-
void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height) {
121+
void common_hal_camera_construct(camera_obj_t *self) {
124122
if (camera_dev.fd < 0) {
125123
if (video_initialize(camera_dev.devpath) < 0) {
126124
mp_raise_ValueError(translate("Could not initialize Camera"));
@@ -131,17 +129,8 @@ void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t he
131129
}
132130
}
133131

134-
if (!camera_check_width_and_height(width, height)) {
135-
mp_raise_ValueError(translate("Size not supported"));
136-
}
137-
138-
self->width = width;
139-
self->height = height;
140-
141132
camera_start_preview();
142133

143-
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height);
144-
145134
camera_start_streaming(V4L2_BUF_TYPE_STILL_CAPTURE);
146135

147136
sleep(1);
@@ -162,18 +151,18 @@ bool common_hal_camera_deinited(camera_obj_t *self) {
162151
return camera_dev.fd < 0;
163152
}
164153

165-
size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, camera_imageformat_t format) {
166-
if (!camera_check_width_and_height(self->width, self->height)) {
154+
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) {
155+
if (!camera_check_width_and_height(width, height)) {
167156
mp_raise_ValueError(translate("Size not supported"));
168157
}
169-
if (!camera_check_buffer_length(self->width, self->height, format, len)) {
158+
if (!camera_check_buffer_length(width, height, format, len)) {
170159
mp_raise_ValueError(translate("Buffer is too small"));
171160
}
172161
if (!camera_check_format(format)) {
173162
mp_raise_ValueError(translate("Format not supported"));
174163
}
175164

176-
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, self->width, self->height);
165+
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height);
177166

178167
v4l2_buffer_t buf;
179168

@@ -192,19 +181,3 @@ size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_
192181

193182
return (size_t)buf.bytesused;
194183
}
195-
196-
uint16_t common_hal_camera_get_width(camera_obj_t *self) {
197-
return self->width;
198-
}
199-
200-
void common_hal_camera_set_width(camera_obj_t *self, uint16_t width) {
201-
self->width = width;
202-
}
203-
204-
uint16_t common_hal_camera_get_height(camera_obj_t *self) {
205-
return self->height;
206-
}
207-
208-
void common_hal_camera_set_height(camera_obj_t *self, uint16_t height) {
209-
self->height = height;
210-
}

ports/cxd56/common-hal/camera/Camera.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
typedef struct {
3333
mp_obj_base_t base;
34-
uint16_t width;
35-
uint16_t height;
3634
} camera_obj_t;
3735

3836
#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_CAMERA_CAMERA_H

shared-bindings/camera/Camera.c

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,26 @@
4848
//| vfs = storage.VfsFat(sd)
4949
//| storage.mount(vfs, '/sd')
5050
//|
51-
//| cam = camera.Camera(1920, 1080)
51+
//| cam = camera.Camera()
5252
//|
5353
//| buffer = bytearray(512 * 1024)
5454
//| file = open("/sd/image.jpg","wb")
55-
//| size = cam.take_picture(buffer, camera.ImageFormat.JPG)
55+
//| size = cam.take_picture(buffer, width=1920, height=1080, format=camera.ImageFormat.JPG)
5656
//| file.write(buffer, size)
5757
//| file.close()"""
5858
//|
5959

60-
//| def __init__(self, width: int, height: int) -> None:
61-
//| """Initialize camera.
62-
//|
63-
//| :param int width: Width in pixels
64-
//| :param int height: Height in pixels"""
60+
//| def __init__(self) -> None:
61+
//| """Initialize camera."""
6562
//| ...
6663
//|
6764
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) {
6865
camera_obj_t *self = m_new_obj(camera_obj_t);
6966
self->base.type = &camera_type;
70-
enum { ARG_width, ARG_height };
71-
static const mp_arg_t allowed_args[] = {
72-
{ MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
73-
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
74-
};
75-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
76-
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
67+
// No arguments
68+
mp_arg_check_num(n_args, kw_args, 0, 0, false);
7769

78-
common_hal_camera_construct(self, args[ARG_width].u_int, args[ARG_height].u_int);
70+
common_hal_camera_construct(self);
7971
return MP_OBJ_FROM_PTR(self);
8072
}
8173

@@ -97,17 +89,20 @@ STATIC void check_for_deinit(camera_obj_t *self) {
9789
}
9890

9991
//| def take_picture(self, buf: WriteableBuffer, format: ImageFormat) -> int:
100-
//| """Take picture and save to ``buf`` in the given ``format``
92+
//| """Take picture and save to ``buf`` in the given ``format``. The size of the picture
93+
//| taken is ``width`` by ``height`` in pixels.
10194
//|
10295
//| :return: the number of bytes written into buf
10396
//| :rtype: int"""
10497
//| ...
10598
//|
10699
STATIC mp_obj_t camera_obj_take_picture(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
107-
enum { ARG_buffer, ARG_format };
100+
enum { ARG_buffer, ARG_width, ARG_height, ARG_format };
108101
static const mp_arg_t allowed_args[] = {
109102
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
110-
{ MP_QSTR_format, MP_ARG_REQUIRED | MP_ARG_OBJ },
103+
{ MP_QSTR_width, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
104+
{ MP_QSTR_height, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
105+
{ MP_QSTR_format, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
111106
};
112107
camera_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
113108
check_for_deinit(self);
@@ -119,70 +114,13 @@ STATIC mp_obj_t camera_obj_take_picture(size_t n_args, const mp_obj_t *pos_args,
119114

120115
camera_imageformat_t format = camera_imageformat_obj_to_type(args[ARG_format].u_obj);
121116

122-
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_take_picture(self, (uint8_t *)bufinfo.buf, bufinfo.len, format));
123-
}
124-
MP_DEFINE_CONST_FUN_OBJ_KW(camera_take_picture_obj, 3, camera_obj_take_picture);
125-
126-
//| width: int
127-
//| """Image width in pixels."""
128-
//|
129-
STATIC mp_obj_t camera_obj_get_width(mp_obj_t self_in) {
130-
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
131-
check_for_deinit(self);
132-
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_get_width(self));
117+
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));
133118
}
134-
MP_DEFINE_CONST_FUN_OBJ_1(camera_get_width_obj, camera_obj_get_width);
135-
136-
STATIC mp_obj_t camera_obj_set_width(mp_obj_t self_in, mp_obj_t value) {
137-
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
138-
check_for_deinit(self);
139-
140-
common_hal_camera_set_width(self, mp_obj_get_int(value));
141-
142-
return mp_const_none;
143-
}
144-
MP_DEFINE_CONST_FUN_OBJ_2(camera_set_width_obj, camera_obj_set_width);
145-
146-
const mp_obj_property_t camera_width_obj = {
147-
.base.type = &mp_type_property,
148-
.proxy = {(mp_obj_t)&camera_get_width_obj,
149-
(mp_obj_t)&camera_set_width_obj,
150-
(mp_obj_t)&mp_const_none_obj},
151-
};
152-
153-
//| height: int
154-
//| """Image height in pixels."""
155-
//|
156-
STATIC mp_obj_t camera_obj_get_height(mp_obj_t self_in) {
157-
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
158-
check_for_deinit(self);
159-
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_get_height(self));
160-
}
161-
MP_DEFINE_CONST_FUN_OBJ_1(camera_get_height_obj, camera_obj_get_height);
162-
163-
STATIC mp_obj_t camera_obj_set_height(mp_obj_t self_in, mp_obj_t value) {
164-
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
165-
check_for_deinit(self);
166-
167-
common_hal_camera_set_height(self, mp_obj_get_int(value));
168-
169-
return mp_const_none;
170-
}
171-
MP_DEFINE_CONST_FUN_OBJ_2(camera_set_height_obj, camera_obj_set_height);
172-
173-
const mp_obj_property_t camera_height_obj = {
174-
.base.type = &mp_type_property,
175-
.proxy = {(mp_obj_t)&camera_get_height_obj,
176-
(mp_obj_t)&camera_set_height_obj,
177-
(mp_obj_t)&mp_const_none_obj},
178-
};
119+
MP_DEFINE_CONST_FUN_OBJ_KW(camera_take_picture_obj, 2, camera_obj_take_picture);
179120

180121
STATIC const mp_rom_map_elem_t camera_locals_dict_table[] = {
181122
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&camera_deinit_obj) },
182123
{ MP_ROM_QSTR(MP_QSTR_take_picture), MP_ROM_PTR(&camera_take_picture_obj) },
183-
184-
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&camera_width_obj) },
185-
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&camera_height_obj) },
186124
};
187125
STATIC MP_DEFINE_CONST_DICT(camera_locals_dict, camera_locals_dict_table);
188126

shared-bindings/camera/Camera.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@
3232

3333
extern const mp_obj_type_t camera_type;
3434

35-
void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height);
35+
void common_hal_camera_construct(camera_obj_t *self);
3636
void common_hal_camera_deinit(camera_obj_t *self);
3737
bool common_hal_camera_deinited(camera_obj_t *self);
38-
size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, camera_imageformat_t format);
39-
40-
uint16_t common_hal_camera_get_width(camera_obj_t *self);
41-
void common_hal_camera_set_width(camera_obj_t *self, uint16_t width);
42-
uint16_t common_hal_camera_get_height(camera_obj_t *self);
43-
void common_hal_camera_set_height(camera_obj_t *self, uint16_t height);
38+
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);
4439

4540
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H

0 commit comments

Comments
 (0)