Skip to content

Commit 9b6a197

Browse files
webcam: fix debug prints
1 parent dd4975d commit 9b6a197

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

c_mpos/src/webcam.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
#define OUTPUT_WIDTH 240
1818
#define OUTPUT_HEIGHT 240
1919

20-
#define WEBCAM_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__);
20+
#define WEBCAM_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__)
2121

22-
// Forward declaration of the webcam type
2322
static const mp_obj_type_t webcam_type;
2423

2524
typedef struct _webcam_obj_t {
@@ -28,7 +27,7 @@ typedef struct _webcam_obj_t {
2827
void *buffers[NUM_BUFFERS];
2928
size_t buffer_length;
3029
int frame_count;
31-
unsigned char *gray_buffer; // Persistent buffer for memoryview
30+
unsigned char *gray_buffer;
3231
} webcam_obj_t;
3332

3433
static void yuyv_to_grayscale_240x240(unsigned char *yuyv, unsigned char *gray, int in_width, int in_height) {
@@ -48,7 +47,7 @@ static void yuyv_to_grayscale_240x240(unsigned char *yuyv, unsigned char *gray,
4847
static void save_raw(const char *filename, unsigned char *data, int width, int height) {
4948
FILE *fp = fopen(filename, "wb");
5049
if (!fp) {
51-
fprintf(stderr, "Cannot open file %s: %s\n", filename, strerror(errno));
50+
WEBCAM_DEBUG_PRINT("Cannot open file %s: %s\n", filename, strerror(errno));
5251
return;
5352
}
5453
fwrite(data, 1, width * height, fp);
@@ -58,7 +57,7 @@ static void save_raw(const char *filename, unsigned char *data, int width, int h
5857
static int init_webcam(webcam_obj_t *self, const char *device) {
5958
self->fd = open(device, O_RDWR);
6059
if (self->fd < 0) {
61-
fprintf(stderr, "Cannot open device: %s\n", strerror(errno));
60+
WEBCAM_DEBUG_PRINT("Cannot open device: %s\n", strerror(errno));
6261
return -1;
6362
}
6463

@@ -69,7 +68,7 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
6968
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
7069
fmt.fmt.pix.field = V4L2_FIELD_ANY;
7170
if (ioctl(self->fd, VIDIOC_S_FMT, &fmt) < 0) {
72-
fprintf(stderr, "Cannot set format: %s\n", strerror(errno));
71+
WEBCAM_DEBUG_PRINT("Cannot set format: %s\n", strerror(errno));
7372
close(self->fd);
7473
return -1;
7574
}
@@ -79,7 +78,7 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
7978
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
8079
req.memory = V4L2_MEMORY_MMAP;
8180
if (ioctl(self->fd, VIDIOC_REQBUFS, &req) < 0) {
82-
fprintf(stderr, "Cannot request buffers: %s\n", strerror(errno));
81+
WEBCAM_DEBUG_PRINT("Cannot request buffers: %s\n", strerror(errno));
8382
close(self->fd);
8483
return -1;
8584
}
@@ -90,14 +89,14 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
9089
buf.memory = V4L2_MEMORY_MMAP;
9190
buf.index = i;
9291
if (ioctl(self->fd, VIDIOC_QUERYBUF, &buf) < 0) {
93-
fprintf(stderr, "Cannot query buffer: %s\n", strerror(errno));
92+
WEBCAM_DEBUG_PRINT("Cannot query buffer: %s\n", strerror(errno));
9493
close(self->fd);
9594
return -1;
9695
}
9796
self->buffer_length = buf.length;
9897
self->buffers[i] = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, self->fd, buf.m.offset);
9998
if (self->buffers[i] == MAP_FAILED) {
100-
fprintf(stderr, "Cannot map buffer: %s\n", strerror(errno));
99+
WEBCAM_DEBUG_PRINT("Cannot map buffer: %s\n", strerror(errno));
101100
close(self->fd);
102101
return -1;
103102
}
@@ -109,21 +108,21 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
109108
buf.memory = V4L2_MEMORY_MMAP;
110109
buf.index = i;
111110
if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) {
112-
fprintf(stderr, "Cannot queue buffer: %s\n", strerror(errno));
111+
WEBCAM_DEBUG_PRINT("Cannot queue buffer: %s\n", strerror(errno));
113112
return -1;
114113
}
115114
}
116115

117116
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
118117
if (ioctl(self->fd, VIDIOC_STREAMON, &type) < 0) {
119-
fprintf(stderr, "Cannot start streaming: %s\n", strerror(errno));
118+
WEBCAM_DEBUG_PRINT("Cannot start streaming: %s\n", strerror(errno));
120119
return -1;
121120
}
122121

123122
self->frame_count = 0;
124123
self->gray_buffer = (unsigned char *)malloc(OUTPUT_WIDTH * OUTPUT_HEIGHT);
125124
if (!self->gray_buffer) {
126-
fprintf(stderr, "Cannot allocate gray buffer: %s\n", strerror(errno));
125+
WEBCAM_DEBUG_PRINT("Cannot allocate gray buffer: %s\n", strerror(errno));
127126
close(self->fd);
128127
return -1;
129128
}
@@ -165,10 +164,6 @@ static mp_obj_t capture_frame(webcam_obj_t *self) {
165164

166165
yuyv_to_grayscale_240x240(self->buffers[buf.index], self->gray_buffer, WIDTH, HEIGHT);
167166

168-
//char filename[32];
169-
//snprintf(filename, sizeof(filename), "frame_%03d.raw", self->frame_count++);
170-
//save_raw(filename, self->gray_buffer, OUTPUT_WIDTH, OUTPUT_HEIGHT);
171-
172167
mp_obj_t result = mp_obj_new_memoryview('b', OUTPUT_WIDTH * OUTPUT_HEIGHT, self->gray_buffer);
173168

174169
if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) {
@@ -180,7 +175,7 @@ static mp_obj_t capture_frame(webcam_obj_t *self) {
180175

181176
static mp_obj_t webcam_init(size_t n_args, const mp_obj_t *args) {
182177
mp_arg_check_num(n_args, 0, 0, 1, false);
183-
const char *device = "/dev/video0"; // Default device
178+
const char *device = "/dev/video0";
184179
if (n_args == 1) {
185180
device = mp_obj_str_get_str(args[0]);
186181
}

0 commit comments

Comments
 (0)