Skip to content

Commit 12b2145

Browse files
Memview sort of works but hangs when it's being redrawn...
1 parent 0e57b10 commit 12b2145

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

c_mpos/src/webcam.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ static void deinit_webcam(webcam_obj_t *self) {
149149
self->fd = -1;
150150
}
151151

152+
static mp_obj_t recapture_frame(webcam_obj_t *self) {
153+
struct v4l2_buffer buf = {0};
154+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
155+
buf.memory = V4L2_MEMORY_MMAP;
156+
if (ioctl(self->fd, VIDIOC_DQBUF, &buf) < 0) {
157+
mp_raise_OSError(MP_EIO);
158+
}
159+
160+
if (!self->gray_buffer) {
161+
mp_raise_OSError(MP_ENOMEM);
162+
}
163+
164+
yuyv_to_grayscale_240x240(self->buffers[buf.index], self->gray_buffer, WIDTH, HEIGHT);
165+
166+
//char filename[32];
167+
//snprintf(filename, sizeof(filename), "frame_%03d.raw", self->frame_count++);
168+
//save_raw(filename, self->gray_buffer, OUTPUT_WIDTH, OUTPUT_HEIGHT);
169+
170+
//mp_obj_t result = mp_obj_new_memoryview(0x01, OUTPUT_WIDTH * OUTPUT_HEIGHT, self->gray_buffer);
171+
mp_obj_t result = mp_const_none;
172+
173+
if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) {
174+
mp_raise_OSError(MP_EIO);
175+
}
176+
177+
return result;
178+
}
179+
152180
static mp_obj_t capture_frame(webcam_obj_t *self) {
153181
struct v4l2_buffer buf = {0};
154182
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
@@ -212,6 +240,16 @@ static mp_obj_t webcam_capture_frame(mp_obj_t self_in) {
212240
}
213241
MP_DEFINE_CONST_FUN_OBJ_1(webcam_capture_frame_obj, webcam_capture_frame);
214242

243+
static mp_obj_t webcam_recapture_frame(mp_obj_t self_in) {
244+
webcam_obj_t *self = MP_OBJ_TO_PTR(self_in);
245+
if (self->fd < 0) {
246+
mp_raise_OSError(MP_EIO);
247+
}
248+
return recapture_frame(self);
249+
}
250+
MP_DEFINE_CONST_FUN_OBJ_1(webcam_recapture_frame_obj, webcam_recapture_frame);
251+
252+
215253
static const mp_obj_type_t webcam_type = {
216254
{ &mp_type_type },
217255
.name = MP_QSTR_Webcam,
@@ -222,6 +260,7 @@ static const mp_rom_map_elem_t mp_module_webcam_globals_table[] = {
222260
{ MP_ROM_QSTR(MP_QSTR_Webcam), MP_ROM_PTR(&webcam_type) },
223261
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&webcam_init_obj) },
224262
{ MP_ROM_QSTR(MP_QSTR_capture_frame), MP_ROM_PTR(&webcam_capture_frame_obj) },
263+
{ MP_ROM_QSTR(MP_QSTR_recapture_frame), MP_ROM_PTR(&webcam_recapture_frame_obj) },
225264
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) },
226265
};
227266
static MP_DEFINE_CONST_DICT(mp_module_webcam_globals, mp_module_webcam_globals_table);

internal_filesystem/apps/com.example.camtest/assets/camtest.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def try_capture_old():
128128

129129

130130
def build_ui():
131-
global image, image_dsc,qr_label
131+
global image, image_dsc,qr_label, cam
132132
cont = lv.obj(appscreen)
133133
cont.set_style_pad_all(0, 0)
134134
cont.set_style_border_width(0, 0)
@@ -160,6 +160,7 @@ def build_ui():
160160
image.align(lv.ALIGN.LEFT_MID, 0, 0)
161161
image.set_rotation(900)
162162
# Create image descriptor once
163+
memview = webcam.capture_frame(cam) # Returns memoryview
163164
image_dsc = lv.image_dsc_t({
164165
"header": {
165166
"magic": lv.IMAGE_HEADER_MAGIC,
@@ -170,7 +171,7 @@ def build_ui():
170171
"cf": lv.COLOR_FORMAT.L8
171172
},
172173
'data_size': width * height,
173-
'data': None # Will be updated per frame
174+
'data': memview # Will be updated per frame
174175
})
175176
image.set_src(image_dsc)
176177

@@ -226,7 +227,10 @@ def init_cam():
226227
while appscreen == lv.screen_active() and keepgoing is True:
227228
print(f"capture nr {count}")
228229
count += 1
229-
try_capture()
230+
#try_capture()
231+
webcam.recapture_frame(cam)
232+
#image.invalidate()
233+
#image.set_src(image_dsc)
230234
time.sleep_ms(100) # Allow for the MicroPython REPL to still work. Reducing it doesn't seem to affect the on-display FPS.
231235
print("App backgrounded, deinitializing camera...")
232236
if use_webcam:

0 commit comments

Comments
 (0)