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//|
6764STATIC 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//|
10699STATIC 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
180121STATIC 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};
187125STATIC MP_DEFINE_CONST_DICT (camera_locals_dict , camera_locals_dict_table );
188126
0 commit comments