Skip to content

Commit 5ac6c7b

Browse files
webcam: still errors
1 parent b488887 commit 5ac6c7b

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

c_mpos/src/webcam.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ typedef struct _webcam_obj_t {
3333
bool streaming; // Streaming state
3434
} webcam_obj_t;
3535

36-
const mp_obj_type_t webcam_type;
36+
// Forward declaration of the webcam type
37+
static const mp_obj_type_t webcam_type;
3738

3839
// Convert YUYV to grayscale (extract Y component)
3940
static void yuyv_to_grayscale(uint8_t *src, uint8_t *dst, size_t width, size_t height) {
@@ -182,6 +183,12 @@ static mp_obj_t webcam_capture_grayscale(mp_obj_t self_in) {
182183
// Clean up
183184
free(resized_buf);
184185

186+
// Re-queue the buffer for the next capture
187+
if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) {
188+
WEBCAM_DEBUG_PRINT("webcam: Failed to re-queue buffer after capture\n");
189+
mp_raise_OSError(errno);
190+
}
191+
185192
return result;
186193
}
187194
MP_DEFINE_CONST_FUN_OBJ_1(webcam_capture_grayscale_obj, webcam_capture_grayscale);
@@ -217,17 +224,24 @@ static mp_obj_t webcam_deinit(mp_obj_t self_in) {
217224
}
218225
MP_DEFINE_CONST_FUN_OBJ_1(webcam_deinit_obj, webcam_deinit);
219226

227+
// Make new webcam object (optional, for consistency)
228+
static mp_obj_t webcam_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
229+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
230+
return webcam_init();
231+
}
232+
220233
// Webcam type definition
221234
static const mp_rom_map_elem_t webcam_locals_dict_table[] = {
222235
{ MP_ROM_QSTR(MP_QSTR_capture_grayscale), MP_ROM_PTR(&webcam_capture_grayscale_obj) },
223236
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) },
224237
};
225238
static MP_DEFINE_CONST_DICT(webcam_locals_dict, webcam_locals_dict_table);
226239

227-
const mp_obj_type_t webcam_type = {
240+
static const mp_obj_type_t webcam_type = {
228241
{ &mp_type_type },
229242
.name = MP_QSTR_webcam,
230-
.locals_dict = (mp_obj_dict_t *)&webcam_locals_dict,
243+
.make_new = webcam_make_new,
244+
// Note: locals_dict is handled separately below
231245
};
232246

233247
// Module definition
@@ -244,3 +258,10 @@ const mp_obj_module_t webcam_user_cmodule = {
244258

245259
// Register the module
246260
MP_REGISTER_MODULE(MP_QSTR_webcam, webcam_user_cmodule);
261+
262+
// Register the locals dict for the webcam type (post-definition)
263+
static MP_DEFINE_CONST_OBJ(webcam_locals_dict_obj, &webcam_locals_dict);
264+
void webcam_type_init(void) {
265+
// Dynamically set the locals_dict after type definition
266+
*(mp_obj_t *)&webcam_type.locals_dict = MP_OBJ_FROM_PTR(&webcam_locals_dict_obj);
267+
}

0 commit comments

Comments
 (0)