48
48
//| vfs = storage.VfsFat(sd)
49
49
//| storage.mount(vfs, '/sd')
50
50
//|
51
- //| cam = camera.Camera(1920, 1080 )
51
+ //| cam = camera.Camera()
52
52
//|
53
53
//| buffer = bytearray(512 * 1024)
54
54
//| 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)
56
56
//| file.write(buffer, size)
57
57
//| file.close()"""
58
58
//|
59
59
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."""
65
62
//| ...
66
63
//|
67
64
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 ) {
68
65
camera_obj_t * self = m_new_obj (camera_obj_t );
69
66
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);
77
69
78
- common_hal_camera_construct (self , args [ ARG_width ]. u_int , args [ ARG_height ]. u_int );
70
+ common_hal_camera_construct (self );
79
71
return MP_OBJ_FROM_PTR (self );
80
72
}
81
73
@@ -97,17 +89,20 @@ STATIC void check_for_deinit(camera_obj_t *self) {
97
89
}
98
90
99
91
//| 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.
101
94
//|
102
95
//| :return: the number of bytes written into buf
103
96
//| :rtype: int"""
104
97
//| ...
105
98
//|
106
99
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 };
108
101
static const mp_arg_t allowed_args [] = {
109
102
{ 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 },
111
106
};
112
107
camera_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
113
108
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,
119
114
120
115
camera_imageformat_t format = camera_imageformat_obj_to_type (args [ARG_format ].u_obj );
121
116
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 ));
133
118
}
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 );
179
120
180
121
STATIC const mp_rom_map_elem_t camera_locals_dict_table [] = {
181
122
{ MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& camera_deinit_obj ) },
182
123
{ 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 ) },
186
124
};
187
125
STATIC MP_DEFINE_CONST_DICT (camera_locals_dict , camera_locals_dict_table );
188
126
0 commit comments