Skip to content

Commit 16ae25b

Browse files
camtest: improve stability by using one thread
1 parent c108dc1 commit 16ae25b

1 file changed

Lines changed: 31 additions & 40 deletions

File tree

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

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

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,27 @@ def remove_bom(buffer):
4848
return buffer[3:]
4949
return buffer
5050

51-
def qrdecode_live():
51+
def qrdecode_one():
5252
global status_label, status_label_text
53-
while keepgoing and keepliveqrdecoding:
54-
try:
55-
import qrdecode
56-
result = qrdecode.qrdecode_rgb565(current_cam_buffer, width, height)
57-
if not result:
58-
status_label_text = status_label_text_searching
59-
else:
60-
#raise ValueError('A very specific bad thing happened.')
61-
result = remove_bom(result)
62-
result = print_qr_buffer(result)
63-
print(f"QR decoding found: {result}")
64-
status_label_text = result
65-
stop_qr_decoding()
66-
except ValueError as e:
67-
print("QR ValueError: ", e)
68-
status_label_text = status_label_text_searching
69-
except TypeError as e:
70-
print("QR TypeError: ", e)
71-
status_label_text = status_label_text_found
72-
except Exception as e:
73-
print("QR got other error: ", e)
74-
time.sleep_ms(100)
53+
try:
54+
import qrdecode
55+
result = qrdecode.qrdecode_rgb565(current_cam_buffer, width, height)
56+
if not result:
57+
status_label.set_text(status_label_text_searching)
58+
else:
59+
result = remove_bom(result)
60+
result = print_qr_buffer(result)
61+
print(f"QR decoding found: {result}")
62+
status_label.set_text(result)
63+
stop_qr_decoding()
64+
except ValueError as e:
65+
print("QR ValueError: ", e)
66+
status_label.set_text(status_label_text_searching)
67+
except TypeError as e:
68+
print("QR TypeError: ", e)
69+
status_label.set_text(status_label_text_found)
70+
except Exception as e:
71+
print("QR got other error: ", e)
7572

7673

7774
def close_button_click(e):
@@ -102,24 +99,22 @@ def snap_button_click(e):
10299

103100

104101
def start_qr_decoding():
105-
global qr_label, keepliveqrdecoding
102+
global qr_label, keepliveqrdecoding, status_label_cont, status_label
106103
print("Activating live QR decoding...")
107104
keepliveqrdecoding = True
108105
qr_label.set_text(lv.SYMBOL.EYE_CLOSE)
109-
try:
110-
import _thread
111-
_thread.stack_size(12*1024) # 16KB is too much
112-
_thread.start_new_thread(qrdecode_live, ())
113-
except Exception as e:
114-
print("Could not start live QR decoding thread: ", e)
106+
status_label_cont.remove_flag(lv.obj.FLAG.HIDDEN)
107+
status_label.set_text(status_label_text_searching)
115108

116109
def stop_qr_decoding():
117-
global qr_label, keepliveqrdecoding, status_label_text
110+
global qr_label, keepliveqrdecoding
118111
print("Deactivating live QR decoding...")
119112
keepliveqrdecoding = False
120113
qr_label.set_text(lv.SYMBOL.EYE_OPEN)
114+
status_label_text = status_label.get_text()
121115
if status_label_text == status_label_text_searching or status_label_text == status_label_text_found: # if it found a QR code, then leave it
122-
status_label_text = ""
116+
status_label_cont.add_flag(lv.obj.FLAG.HIDDEN)
117+
123118

124119
def qr_button_click(e):
125120
global keepliveqrdecoding
@@ -144,6 +139,8 @@ def try_capture(event, data):
144139
image.set_src(image_dsc)
145140
if not use_webcam:
146141
cam.free_buffer() # Free the old buffer
142+
if keepliveqrdecoding:
143+
qrdecode_one()
147144
except Exception as e:
148145
print(f"Camera capture exception: {e}")
149146

@@ -202,7 +199,7 @@ def build_ui():
202199
status_label_cont.set_style_bg_opa(66, 0)
203200
status_label_cont.set_style_border_width(0, 0)
204201
status_label = lv.label(status_label_cont)
205-
status_label.set_text(status_label_text)
202+
status_label.set_text("No camera found.")
206203
status_label.set_long_mode(lv.label.LONG.WRAP)
207204
status_label.set_style_text_color(lv.color_white(), 0)
208205
status_label.set_width(lv.pct(100))
@@ -257,21 +254,15 @@ def init_cam():
257254
print(f"camtest.py: webcam exception: {e}")
258255

259256
if cam and keepgoing:
260-
status_label_text = ""
261257
qr_button.remove_flag(lv.obj.FLAG.HIDDEN)
262258
snap_button.remove_flag(lv.obj.FLAG.HIDDEN)
259+
status_label_cont.add_flag(lv.obj.FLAG.HIDDEN)
263260
import task_handler
264261
th.add_event_cb(try_capture, task_handler.TASK_HANDLER_STARTED)
265262

266263

267264
while appscreen == lv.screen_active() and keepgoing:
268265
time.sleep_ms(100)
269-
if status_label.get_text() != status_label_text:
270-
status_label.set_text(status_label_text)
271-
if status_label_text:
272-
status_label_cont.remove_flag(lv.obj.FLAG.HIDDEN)
273-
else:
274-
status_label_cont.add_flag(lv.obj.FLAG.HIDDEN)
275266

276267
print("camtest.py: stopping...")
277268
if cam:

0 commit comments

Comments
 (0)