captures work now! at least for 10 seconds...

This commit is contained in:
Thomas Farstrike
2025-05-14 10:56:04 +02:00
parent 8b3f596150
commit 965818408c
2 changed files with 20 additions and 27 deletions
+10 -11
View File
@@ -17,6 +17,9 @@
#define OUTPUT_WIDTH 240
#define OUTPUT_HEIGHT 240
// Forward declaration of the webcam type
static const mp_obj_type_t webcam_type;
typedef struct _webcam_obj_t {
mp_obj_base_t base;
int fd;
@@ -164,15 +167,15 @@ static mp_obj_t capture_frame(webcam_obj_t *self) {
return result;
}
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) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
static mp_obj_t webcam_init(size_t n_args, const mp_obj_t *args) {
mp_arg_check_num(n_args, 0, 0, 1, false);
const char *device = "/dev/video0"; // Default device
if (n_args == 1) {
device = mp_obj_str_get_str(args[0]);
}
webcam_obj_t *self = m_new_obj(webcam_obj_t);
self->base.type = type;
self->base.type = &webcam_type;
self->fd = -1;
if (init_webcam(self, device) < 0) {
@@ -181,6 +184,7 @@ static mp_obj_t webcam_make_new(const mp_obj_type_t *type, size_t n_args, size_t
return MP_OBJ_FROM_PTR(self);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(webcam_init_obj, 0, 1, webcam_init);
static mp_obj_t webcam_deinit(mp_obj_t self_in) {
webcam_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -198,22 +202,17 @@ static mp_obj_t webcam_capture_frame(mp_obj_t self_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(webcam_capture_frame_obj, webcam_capture_frame);
static const mp_rom_map_elem_t webcam_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_capture_frame), MP_ROM_PTR(&webcam_capture_frame_obj) },
};
static MP_DEFINE_CONST_DICT(webcam_locals_dict, webcam_locals_dict_table);
static const mp_obj_type_t webcam_type = {
{ &mp_type_type },
.name = MP_QSTR_Webcam,
.make_new = webcam_make_new,
.locals_dict = (mp_obj_dict_t *)&webcam_locals_dict,
};
static const mp_rom_map_elem_t mp_module_webcam_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_webcam) },
{ MP_ROM_QSTR(MP_QSTR_Webcam), MP_ROM_PTR(&webcam_type) },
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&webcam_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_capture_frame), MP_ROM_PTR(&webcam_capture_frame_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) },
};
static MP_DEFINE_CONST_DICT(mp_module_webcam_globals, mp_module_webcam_globals_table);
@@ -1,4 +1,5 @@
import time
import webcam
appscreen = lv.screen_active()
@@ -95,7 +96,7 @@ def qr_button_click(e):
def try_capture():
global current_cam_buffer, image_dsc, image, use_webcam
if use_webcam:
new_cam_buffer = cam.capture_grayscale()
new_cam_buffer = webcam.capture_frame(cam)
elif cam.frame_available():
new_cam_buffer = cam.capture() # Returns memoryview
if new_cam_buffer and len(new_cam_buffer):
@@ -193,27 +194,15 @@ def init_cam():
return None
import webcam
class Webcam:
def __init__(self):
# webcam.init() returns (obj, capture_grayscale, deinit)
self.obj, self._capture_grayscale, self._deinit = webcam.init()
def capture_grayscale(self):
try:
return self._capture_grayscale(self.obj)
except Exception as e:
print(f"capture got exception {e}")
self.deinit()
def deinit(self):
return self._deinit(self.obj)
cam = init_cam()
if not cam:
print("init cam failed, retrying with webcam...")
try:
cam = Webcam()
cam = webcam.init("/dev/video0") # Initialize webcam with device path
use_webcam = True
except Exception as e:
print(f"camtest.py: webcam exception: {e}")
@@ -224,8 +213,13 @@ if cam or use_webcam:
try_capture()
time.sleep_ms(100) # Allow for the MicroPython REPL to still work. Reducing it doesn't seem to affect the on-display FPS.
print("App backgrounded, deinitializing camera...")
cam.deinit()
if use_webcam:
webcam.deinit(cam) # Deinitializes webcam
else:
cam.deinit()
else:
print("No camera found, exiting...")
show_launcher()