Skip to content

Commit f982f28

Browse files
camera: add status label, hide buttons until found
1 parent 08c0370 commit f982f28

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

c_mpos/src/webcam.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ static void save_raw_rgb565(const char *filename, uint16_t *data, int width, int
108108
}
109109

110110
static int init_webcam(webcam_obj_t *self, const char *device) {
111+
//WEBCAM_DEBUG_PRINT("webcam.c: init_webcam\n");
111112
self->fd = open(device, O_RDWR);
112113
if (self->fd < 0) {
113114
WEBCAM_DEBUG_PRINT("Cannot open device: %s\n", strerror(errno));
114-
return -1;
115+
return -errno;
115116
}
116117

117118
struct v4l2_format fmt = {0};
@@ -123,7 +124,7 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
123124
if (ioctl(self->fd, VIDIOC_S_FMT, &fmt) < 0) {
124125
WEBCAM_DEBUG_PRINT("Cannot set format: %s\n", strerror(errno));
125126
close(self->fd);
126-
return -1;
127+
return -errno;
127128
}
128129

129130
struct v4l2_requestbuffers req = {0};
@@ -133,7 +134,7 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
133134
if (ioctl(self->fd, VIDIOC_REQBUFS, &req) < 0) {
134135
WEBCAM_DEBUG_PRINT("Cannot request buffers: %s\n", strerror(errno));
135136
close(self->fd);
136-
return -1;
137+
return -errno;
137138
}
138139

139140
for (int i = 0; i < NUM_BUFFERS; i++) {
@@ -144,14 +145,14 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
144145
if (ioctl(self->fd, VIDIOC_QUERYBUF, &buf) < 0) {
145146
WEBCAM_DEBUG_PRINT("Cannot query buffer: %s\n", strerror(errno));
146147
close(self->fd);
147-
return -1;
148+
return -errno;
148149
}
149150
self->buffer_length = buf.length;
150151
self->buffers[i] = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, self->fd, buf.m.offset);
151152
if (self->buffers[i] == MAP_FAILED) {
152153
WEBCAM_DEBUG_PRINT("Cannot map buffer: %s\n", strerror(errno));
153154
close(self->fd);
154-
return -1;
155+
return -errno;
155156
}
156157
}
157158

@@ -162,14 +163,14 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
162163
buf.index = i;
163164
if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) {
164165
WEBCAM_DEBUG_PRINT("Cannot queue buffer: %s\n", strerror(errno));
165-
return -1;
166+
return -errno;
166167
}
167168
}
168169

169170
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
170171
if (ioctl(self->fd, VIDIOC_STREAMON, &type) < 0) {
171172
WEBCAM_DEBUG_PRINT("Cannot start streaming: %s\n", strerror(errno));
172-
return -1;
173+
return -errno;
173174
}
174175

175176
self->frame_count = 0;
@@ -180,7 +181,7 @@ static int init_webcam(webcam_obj_t *self, const char *device) {
180181
free(self->gray_buffer);
181182
free(self->rgb565_buffer);
182183
close(self->fd);
183-
return -1;
184+
return -errno;
184185
}
185186
return 0;
186187
}
@@ -215,12 +216,14 @@ static mp_obj_t free_buffer(webcam_obj_t *self) {
215216
}
216217

217218
static mp_obj_t capture_frame(mp_obj_t self_in, mp_obj_t format) {
219+
int res = 0;
218220
webcam_obj_t *self = MP_OBJ_TO_PTR(self_in);
219221
struct v4l2_buffer buf = {0};
220222
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
221223
buf.memory = V4L2_MEMORY_MMAP;
222-
if (ioctl(self->fd, VIDIOC_DQBUF, &buf) < 0) {
223-
mp_raise_OSError(MP_EIO);
224+
res = ioctl(self->fd, VIDIOC_DQBUF, &buf);
225+
if (res < 0) {
226+
mp_raise_OSError(-res);
224227
}
225228

226229
if (!self->gray_buffer) {
@@ -243,8 +246,9 @@ static mp_obj_t capture_frame(mp_obj_t self_in, mp_obj_t format) {
243246
// snprintf(filename, sizeof(filename), "frame_%03d.raw", self->frame_count++);
244247
// save_raw(filename, self->gray_buffer, OUTPUT_WIDTH, OUTPUT_HEIGHT);
245248
mp_obj_t result = mp_obj_new_memoryview('B', OUTPUT_WIDTH * OUTPUT_HEIGHT, self->gray_buffer);
246-
if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) {
247-
mp_raise_OSError(MP_EIO);
249+
res = ioctl(self->fd, VIDIOC_QBUF, &buf);
250+
if (res < 0) {
251+
mp_raise_OSError(-res);
248252
}
249253
return result;
250254
} else {
@@ -253,8 +257,9 @@ static mp_obj_t capture_frame(mp_obj_t self_in, mp_obj_t format) {
253257
// snprintf(filename, sizeof(filename), "frame_%03d.rgb565", self->frame_count++);
254258
// save_raw_rgb565(filename, self->rgb565_buffer, OUTPUT_WIDTH, OUTPUT_HEIGHT);
255259
mp_obj_t result = mp_obj_new_memoryview('H', OUTPUT_WIDTH * OUTPUT_HEIGHT, self->rgb565_buffer);
256-
if (ioctl(self->fd, VIDIOC_QBUF, &buf) < 0) {
257-
mp_raise_OSError(MP_EIO);
260+
res = ioctl(self->fd, VIDIOC_QBUF, &buf);
261+
if (res < 0) {
262+
mp_raise_OSError(-res);
258263
}
259264
return result;
260265
}
@@ -273,8 +278,9 @@ static mp_obj_t webcam_init(size_t n_args, const mp_obj_t *args) {
273278
self->gray_buffer = NULL;
274279
self->rgb565_buffer = NULL;
275280

276-
if (init_webcam(self, device) < 0) {
277-
mp_raise_OSError(MP_EIO);
281+
int res = init_webcam(self, device);
282+
if (res < 0) {
283+
mp_raise_OSError(-res);
278284
}
279285

280286
return MP_OBJ_FROM_PTR(self);

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

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
import time
99

10-
appscreen = lv.screen_active()
11-
th.disable()
12-
1310
keepgoing = True
1411
keepliveqrdecoding = False
1512
width = 240
@@ -20,7 +17,11 @@
2017
image_dsc = None
2118
image = None
2219
qr_label = None
20+
status_label = None
2321
use_webcam = False
22+
qr_button = None
23+
snap_button = None
24+
2425

2526
memview = None
2627

@@ -121,7 +122,7 @@ def try_capture():
121122

122123

123124
def build_ui():
124-
global image, image_dsc,qr_label, cam
125+
global image, image_dsc,qr_label, status_label, cam, use_webcam, qr_button, snap_button
125126
cont = lv.obj(appscreen)
126127
cont.set_style_pad_all(0, 0)
127128
cont.set_style_border_width(0, 0)
@@ -137,12 +138,14 @@ def build_ui():
137138
snap_button = lv.button(cont)
138139
snap_button.set_size(60, 60)
139140
snap_button.align(lv.ALIGN.RIGHT_MID, 0, 0)
141+
snap_button.add_flag(lv.obj.FLAG.HIDDEN)
140142
snap_label = lv.label(snap_button)
141143
snap_label.set_text(lv.SYMBOL.OK)
142-
snap_label.center()
143-
snap_button.add_event_cb(snap_button_click,lv.EVENT.CLICKED,None)
144+
snap_label.center()
145+
snap_button.add_event_cb(snap_button_click,lv.EVENT.CLICKED,None)
144146
qr_button = lv.button(cont)
145147
qr_button.set_size(60, 60)
148+
qr_button.add_flag(lv.obj.FLAG.HIDDEN)
146149
qr_button.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0)
147150
qr_label = lv.label(qr_button)
148151
qr_label.set_text(lv.SYMBOL.EYE_OPEN)
@@ -151,8 +154,6 @@ def build_ui():
151154
# Initialize LVGL image widget
152155
image = lv.image(cont)
153156
image.align(lv.ALIGN.LEFT_MID, 0, 0)
154-
if not use_webcam:
155-
image.set_rotation(900)
156157
# Create image descriptor once
157158
image_dsc = lv.image_dsc_t({
158159
"header": {
@@ -167,6 +168,9 @@ def build_ui():
167168
'data': None # Will be updated per frame
168169
})
169170
image.set_src(image_dsc)
171+
status_label = lv.label(appscreen)
172+
status_label.set_text("No camera found.")
173+
status_label.center()
170174

171175

172176
def init_cam():
@@ -201,11 +205,13 @@ def init_cam():
201205

202206

203207

204-
205-
208+
appscreen = lv.screen_active()
209+
build_ui()
206210

207211
cam = init_cam()
208-
if not cam:
212+
if cam:
213+
image.set_rotation(900)
214+
else:
209215
print("camtest.py: no internal camera found, trying webcam on /dev/video0")
210216
try:
211217
import webcam
@@ -215,23 +221,28 @@ def init_cam():
215221
print(f"camtest.py: webcam exception: {e}")
216222

217223
if cam:
218-
build_ui()
219-
count=0
220-
while appscreen == lv.screen_active() and keepgoing is True:
224+
status_label.set_text("")
225+
qr_button.remove_flag(lv.obj.FLAG.HIDDEN)
226+
snap_button.remove_flag(lv.obj.FLAG.HIDDEN)
227+
228+
# Task handler needs to be updated from this app's thread, otherwise it causes concurrency issues
229+
th.disable()
230+
231+
while appscreen == lv.screen_active() and keepgoing is True:
232+
if cam:
221233
try_capture()
222-
# Task handler needs to be updated from the same thread, otherwise it causes concurrency issues:
223-
lv.task_handler()
224-
time.sleep_ms(1)
225-
lv.tick_inc(1)
226-
print("camtest.py: stopping...")
227-
if use_webcam:
228-
webcam.deinit(cam) # Deinitializes webcam
229-
else:
230-
cam.deinit()
231-
else:
232-
print("No camera found, exiting...")
234+
lv.task_handler()
235+
time.sleep_ms(5)
236+
lv.tick_inc(5)
233237

234-
th.enable()
235-
show_launcher()
238+
print("camtest.py: stopping...")
239+
240+
if use_webcam:
241+
webcam.deinit(cam)
242+
elif cam:
243+
cam.deinit()
236244

237245

246+
print("camtest.py: showing launcher...")
247+
show_launcher()
248+
th.enable()

0 commit comments

Comments
 (0)