Skip to content

Commit 4b768b1

Browse files
webcam: add debug prints
1 parent 218a3eb commit 4b768b1

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

c_mpos/src/webcam.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "py/runtime.h"
1212
#include "py/mperrno.h"
1313

14+
// Debug print macro
15+
#define WEBCAM_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__)
16+
1417
// Module name
1518
#define MODULE_NAME "webcam"
1619

@@ -66,6 +69,7 @@ static mp_obj_t webcam_capture_grayscale(void) {
6669
// Open the webcam device
6770
cam.fd = open(VIDEO_DEVICE, O_RDWR);
6871
if (cam.fd < 0) {
72+
WEBCAM_DEBUG_PRINT("webcam: Failed to open device %s\n", VIDEO_DEVICE);
6973
mp_raise_OSError(errno);
7074
}
7175

@@ -76,6 +80,7 @@ static mp_obj_t webcam_capture_grayscale(void) {
7680
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
7781
fmt.fmt.pix.field = V4L2_FIELD_ANY;
7882
if (ioctl(cam.fd, VIDIOC_S_FMT, &fmt) < 0) {
83+
WEBCAM_DEBUG_PRINT("webcam: Failed to set YUYV format at %dx%d\n", CAPTURE_WIDTH, CAPTURE_HEIGHT);
7984
close(cam.fd);
8085
mp_raise_OSError(errno);
8186
}
@@ -85,6 +90,7 @@ static mp_obj_t webcam_capture_grayscale(void) {
8590
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
8691
req.memory = V4L2_MEMORY_MMAP;
8792
if (ioctl(cam.fd, VIDIOC_REQBUFS, &req) < 0) {
93+
WEBCAM_DEBUG_PRINT("webcam: Failed to request memory-mapped buffer\n");
8894
close(cam.fd);
8995
mp_raise_OSError(errno);
9096
}
@@ -94,19 +100,22 @@ static mp_obj_t webcam_capture_grayscale(void) {
94100
buf.memory = V4L2_MEMORY_MMAP;
95101
buf.index = 0;
96102
if (ioctl(cam.fd, VIDIOC_QUERYBUF, &buf) < 0) {
103+
WEBCAM_DEBUG_PRINT("webcam: Failed to query buffer properties\n");
97104
close(cam.fd);
98105
mp_raise_OSError(errno);
99106
}
100107

101108
cam.buffer_length = buf.length;
102109
cam.buffer = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, cam.fd, buf.m.offset);
103110
if (cam.buffer == MAP_FAILED) {
111+
WEBCAM_DEBUG_PRINT("webcam: Failed to map buffer memory\n");
104112
close(cam.fd);
105113
mp_raise_OSError(errno);
106114
}
107115

108116
// Queue the buffer
109117
if (ioctl(cam.fd, VIDIOC_QBUF, &buf) < 0) {
118+
WEBCAM_DEBUG_PRINT("webcam: Failed to queue buffer for capture\n");
110119
munmap(cam.buffer, cam.buffer_length);
111120
close(cam.fd);
112121
mp_raise_OSError(errno);
@@ -115,13 +124,15 @@ static mp_obj_t webcam_capture_grayscale(void) {
115124
// Start streaming
116125
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
117126
if (ioctl(cam.fd, VIDIOC_STREAMON, &type) < 0) {
127+
WEBCAM_DEBUG_PRINT("webcam: Failed to start video streaming\n");
118128
munmap(cam.buffer, cam.buffer_length);
119129
close(cam.fd);
120130
mp_raise_OSError(errno);
121131
}
122132

123133
// Dequeue the buffer (capture frame)
124134
if (ioctl(cam.fd, VIDIOC_DQBUF, &buf) < 0) {
135+
WEBCAM_DEBUG_PRINT("webcam: Failed to dequeue captured frame\n");
125136
ioctl(cam.fd, VIDIOC_STREAMOFF, &type);
126137
munmap(cam.buffer, cam.buffer_length);
127138
close(cam.fd);
@@ -132,6 +143,7 @@ static mp_obj_t webcam_capture_grayscale(void) {
132143
size_t capture_size = CAPTURE_WIDTH * CAPTURE_HEIGHT;
133144
uint8_t *grayscale_buf = (uint8_t *)malloc(capture_size);
134145
if (!grayscale_buf) {
146+
WEBCAM_DEBUG_PRINT("webcam: Failed to allocate memory for grayscale buffer (%zu bytes)\n", capture_size);
135147
ioctl(cam.fd, VIDIOC_STREAMOFF, &type);
136148
munmap(cam.buffer, cam.buffer_length);
137149
close(cam.fd);
@@ -143,6 +155,7 @@ static mp_obj_t webcam_capture_grayscale(void) {
143155
size_t output_size = OUTPUT_WIDTH * OUTPUT_HEIGHT;
144156
uint8_t *resized_buf = (uint8_t *)malloc(output_size);
145157
if (!resized_buf) {
158+
WEBCAM_DEBUG_PRINT("webcam: Failed to allocate memory for resized buffer (%zu bytes)\n", output_size);
146159
free(grayscale_buf);
147160
ioctl(cam.fd, VIDIOC_STREAMOFF, &type);
148161
munmap(cam.buffer, cam.buffer_length);

0 commit comments

Comments
 (0)