Skip to content

Commit 34728e9

Browse files
webcam: no more errors!
1 parent 830454c commit 34728e9

File tree

1 file changed

+73
-81
lines changed

1 file changed

+73
-81
lines changed

c_mpos/src/webcam.c

Lines changed: 73 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -64,69 +64,6 @@ static void resize_640x480_to_240x240(uint8_t *src, uint8_t *dst) {
6464
}
6565
}
6666

67-
// Initialize the webcam
68-
static mp_obj_t webcam_init(void) {
69-
webcam_obj_t *self = m_new_obj(webcam_obj_t);
70-
self->base.type = &webcam_type;
71-
self->fd = -1;
72-
self->buffer = NULL;
73-
self->buffer_length = 0;
74-
self->streaming = false;
75-
76-
// Open the webcam device
77-
self->fd = open(VIDEO_DEVICE, O_RDWR);
78-
if (self->fd < 0) {
79-
WEBCAM_DEBUG_PRINT("webcam: Failed to open device %s\n", VIDEO_DEVICE);
80-
mp_raise_OSError(errno);
81-
}
82-
83-
// Set format to YUYV at 640x480
84-
struct v4l2_format fmt = {0};
85-
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
86-
fmt.fmt.pix.width = CAPTURE_WIDTH;
87-
fmt.fmt.pix.height = CAPTURE_HEIGHT;
88-
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
89-
fmt.fmt.pix.field = V4L2_FIELD_ANY;
90-
if (ioctl(self->fd, VIDIOC_S_FMT, &fmt) < 0) {
91-
WEBCAM_DEBUG_PRINT("webcam: Failed to set YUYV format at %dx%d\n", CAPTURE_WIDTH, CAPTURE_HEIGHT);
92-
close(self->fd);
93-
mp_raise_OSError(errno);
94-
}
95-
96-
// Request one buffer
97-
struct v4l2_requestbuffers req = {0};
98-
req.count = 1;
99-
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
100-
req.memory = V4L2_MEMORY_MMAP;
101-
if (ioctl(self->fd, VIDIOC_REQBUFS, &req) < 0) {
102-
WEBCAM_DEBUG_PRINT("webcam: Failed to request memory-mapped buffer\n");
103-
close(self->fd);
104-
mp_raise_OSError(errno);
105-
}
106-
107-
// Query and map the buffer
108-
struct v4l2_buffer buf = {0};
109-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
110-
buf.memory = V4L2_MEMORY_MMAP;
111-
buf.index = 0;
112-
if (ioctl(self->fd, VIDIOC_QUERYBUF, &buf) < 0) {
113-
WEBCAM_DEBUG_PRINT("webcam: Failed to query buffer properties\n");
114-
close(self->fd);
115-
mp_raise_OSError(errno);
116-
}
117-
118-
self->buffer_length = buf.length;
119-
self->buffer = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, self->fd, buf.m.offset);
120-
if (self->buffer == MAP_FAILED) {
121-
WEBCAM_DEBUG_PRINT("webcam: Failed to map buffer memory\n");
122-
close(self->fd);
123-
mp_raise_OSError(errno);
124-
}
125-
126-
return MP_OBJ_FROM_PTR(self);
127-
}
128-
MP_DEFINE_CONST_FUN_OBJ_0(webcam_init_obj, webcam_init);
129-
13067
// Capture a grayscale image
13168
static mp_obj_t webcam_capture_grayscale(mp_obj_t self_in) {
13269
webcam_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -224,34 +161,89 @@ static mp_obj_t webcam_deinit(mp_obj_t self_in) {
224161
}
225162
MP_DEFINE_CONST_FUN_OBJ_1(webcam_deinit_obj, webcam_deinit);
226163

227-
// Attribute lookup for webcam object
228-
static void webcam_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
229-
if (dest[0] == MP_OBJ_NULL) {
230-
// Lookup attribute
231-
if (attr == MP_QSTR_capture_grayscale) {
232-
dest[0] = MP_OBJ_FROM_PTR(&webcam_capture_grayscale_obj);
233-
dest[1] = self_in;
234-
} else if (attr == MP_QSTR_deinit) {
235-
dest[0] = MP_OBJ_FROM_PTR(&webcam_deinit_obj);
236-
dest[1] = self_in;
237-
}
164+
// Initialize the webcam and return a tuple with the object and methods
165+
static mp_obj_t webcam_init(void) {
166+
webcam_obj_t *self = m_new_obj(webcam_obj_t);
167+
self->base.type = &webcam_type;
168+
self->fd = -1;
169+
self->buffer = NULL;
170+
self->buffer_length = 0;
171+
self->streaming = false;
172+
173+
// Open the webcam device
174+
self->fd = open(VIDEO_DEVICE, O_RDWR);
175+
if (self->fd < 0) {
176+
WEBCAM_DEBUG_PRINT("webcam: Failed to open device %s\n", VIDEO_DEVICE);
177+
mp_raise_OSError(errno);
238178
}
239-
}
240179

241-
// Webcam type definition
242-
static const mp_obj_type_t webcam_type = {
243-
{ &mp_type_type },
244-
.name = MP_QSTR_webcam,
245-
.attr = webcam_attr,
246-
};
180+
// Set format to YUYV at 640x480
181+
struct v4l2_format fmt = {0};
182+
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
183+
fmt.fmt.pix.width = CAPTURE_WIDTH;
184+
fmt.fmt.pix.height = CAPTURE_HEIGHT;
185+
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
186+
fmt.fmt.pix.field = V4L2_FIELD_ANY;
187+
if (ioctl(self->fd, VIDIOC_S_FMT, &fmt) < 0) {
188+
WEBCAM_DEBUG_PRINT("webcam: Failed to set YUYV format at %dx%d\n", CAPTURE_WIDTH, CAPTURE_HEIGHT);
189+
close(self->fd);
190+
mp_raise_OSError(errno);
191+
}
192+
193+
// Request one buffer
194+
struct v4l2_requestbuffers req = {0};
195+
req.count = 1;
196+
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
197+
req.memory = V4L2_MEMORY_MMAP;
198+
if (ioctl(self->fd, VIDIOC_REQBUFS, &req) < 0) {
199+
WEBCAM_DEBUG_PRINT("webcam: Failed to request memory-mapped buffer\n");
200+
close(self->fd);
201+
mp_raise_OSError(errno);
202+
}
203+
204+
// Query and map the buffer
205+
struct v4l2_buffer buf = {0};
206+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
207+
buf.memory = V4L2_MEMORY_MMAP;
208+
buf.index = 0;
209+
if (ioctl(self->fd, VIDIOC_QUERYBUF, &buf) < 0) {
210+
WEBCAM_DEBUG_PRINT("webcam: Failed to query buffer properties\n");
211+
close(self->fd);
212+
mp_raise_OSError(errno);
213+
}
214+
215+
self->buffer_length = buf.length;
216+
self->buffer = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, self->fd, buf.m.offset);
217+
if (self->buffer == MAP_FAILED) {
218+
WEBCAM_DEBUG_PRINT("webcam: Failed to map buffer memory\n");
219+
close(self->fd);
220+
mp_raise_OSError(errno);
221+
}
222+
223+
// Create a tuple to hold the webcam object and method references
224+
mp_obj_t tuple[3];
225+
tuple[0] = MP_OBJ_FROM_PTR(self);
226+
tuple[1] = MP_OBJ_FROM_PTR(&webcam_capture_grayscale_obj);
227+
tuple[2] = MP_OBJ_FROM_PTR(&webcam_deinit_obj);
228+
return mp_obj_new_tuple(3, tuple);
229+
}
230+
MP_DEFINE_CONST_FUN_OBJ_0(webcam_init_obj, webcam_init);
247231

248232
// Module definition
249233
static const mp_rom_map_elem_t webcam_module_globals_table[] = {
250234
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_webcam) },
251235
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&webcam_init_obj) },
236+
{ MP_ROM_QSTR(MP_QSTR_capture_grayscale), MP_ROM_PTR(&webcam_capture_grayscale_obj) },
237+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) },
252238
};
253239
static MP_DEFINE_CONST_DICT(webcam_module_globals, webcam_module_globals_table);
254240

241+
// Webcam type definition
242+
static const mp_obj_type_t webcam_type = {
243+
{ &mp_type_type },
244+
.name = MP_QSTR_webcam,
245+
};
246+
255247
const mp_obj_module_t webcam_user_cmodule = {
256248
.base = { &mp_type_module },
257249
.globals = (mp_obj_dict_t *)&webcam_module_globals,

0 commit comments

Comments
 (0)