Skip to content

Commit 9658184

Browse files
captures work now! at least for 10 seconds...
1 parent 8b3f596 commit 9658184

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

c_mpos/src/webcam.c

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

20+
// Forward declaration of the webcam type
21+
static const mp_obj_type_t webcam_type;
22+
2023
typedef struct _webcam_obj_t {
2124
mp_obj_base_t base;
2225
int fd;
@@ -164,15 +167,15 @@ static mp_obj_t capture_frame(webcam_obj_t *self) {
164167
return result;
165168
}
166169

167-
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) {
168-
mp_arg_check_num(n_args, n_kw, 0, 1, false);
170+
static mp_obj_t webcam_init(size_t n_args, const mp_obj_t *args) {
171+
mp_arg_check_num(n_args, 0, 0, 1, false);
169172
const char *device = "/dev/video0"; // Default device
170173
if (n_args == 1) {
171174
device = mp_obj_str_get_str(args[0]);
172175
}
173176

174177
webcam_obj_t *self = m_new_obj(webcam_obj_t);
175-
self->base.type = type;
178+
self->base.type = &webcam_type;
176179
self->fd = -1;
177180

178181
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
181184

182185
return MP_OBJ_FROM_PTR(self);
183186
}
187+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(webcam_init_obj, 0, 1, webcam_init);
184188

185189
static mp_obj_t webcam_deinit(mp_obj_t self_in) {
186190
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) {
198202
}
199203
MP_DEFINE_CONST_FUN_OBJ_1(webcam_capture_frame_obj, webcam_capture_frame);
200204

201-
static const mp_rom_map_elem_t webcam_locals_dict_table[] = {
202-
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) },
203-
{ MP_ROM_QSTR(MP_QSTR_capture_frame), MP_ROM_PTR(&webcam_capture_frame_obj) },
204-
};
205-
static MP_DEFINE_CONST_DICT(webcam_locals_dict, webcam_locals_dict_table);
206-
207205
static const mp_obj_type_t webcam_type = {
208206
{ &mp_type_type },
209207
.name = MP_QSTR_Webcam,
210-
.make_new = webcam_make_new,
211-
.locals_dict = (mp_obj_dict_t *)&webcam_locals_dict,
212208
};
213209

214210
static const mp_rom_map_elem_t mp_module_webcam_globals_table[] = {
215211
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_webcam) },
216212
{ MP_ROM_QSTR(MP_QSTR_Webcam), MP_ROM_PTR(&webcam_type) },
213+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&webcam_init_obj) },
214+
{ MP_ROM_QSTR(MP_QSTR_capture_frame), MP_ROM_PTR(&webcam_capture_frame_obj) },
215+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&webcam_deinit_obj) },
217216
};
218217
static MP_DEFINE_CONST_DICT(mp_module_webcam_globals, mp_module_webcam_globals_table);
219218

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import webcam
23

34
appscreen = lv.screen_active()
45

@@ -95,7 +96,7 @@ def qr_button_click(e):
9596
def try_capture():
9697
global current_cam_buffer, image_dsc, image, use_webcam
9798
if use_webcam:
98-
new_cam_buffer = cam.capture_grayscale()
99+
new_cam_buffer = webcam.capture_frame(cam)
99100
elif cam.frame_available():
100101
new_cam_buffer = cam.capture() # Returns memoryview
101102
if new_cam_buffer and len(new_cam_buffer):
@@ -193,27 +194,15 @@ def init_cam():
193194
return None
194195

195196

196-
import webcam
197197

198-
class Webcam:
199-
def __init__(self):
200-
# webcam.init() returns (obj, capture_grayscale, deinit)
201-
self.obj, self._capture_grayscale, self._deinit = webcam.init()
202-
def capture_grayscale(self):
203-
try:
204-
return self._capture_grayscale(self.obj)
205-
except Exception as e:
206-
print(f"capture got exception {e}")
207-
self.deinit()
208-
def deinit(self):
209-
return self._deinit(self.obj)
198+
210199

211200

212201
cam = init_cam()
213202
if not cam:
214203
print("init cam failed, retrying with webcam...")
215204
try:
216-
cam = Webcam()
205+
cam = webcam.init("/dev/video0") # Initialize webcam with device path
217206
use_webcam = True
218207
except Exception as e:
219208
print(f"camtest.py: webcam exception: {e}")
@@ -224,8 +213,13 @@ def deinit(self):
224213
try_capture()
225214
time.sleep_ms(100) # Allow for the MicroPython REPL to still work. Reducing it doesn't seem to affect the on-display FPS.
226215
print("App backgrounded, deinitializing camera...")
227-
cam.deinit()
216+
if use_webcam:
217+
webcam.deinit(cam) # Deinitializes webcam
218+
else:
219+
cam.deinit()
228220
else:
229221
print("No camera found, exiting...")
230222

231223
show_launcher()
224+
225+

0 commit comments

Comments
 (0)