Skip to content

Commit 6631b98

Browse files
Add QR code scanning labels
1 parent f982f28 commit 6631b98

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

c_mpos/src/quirc_decode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
8181
free(data);
8282
free(code);
8383
quirc_destroy(qr);
84-
mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code"));
84+
mp_raise_TypeError(MP_ERROR_TEXT("failed to decode QR code"));
8585
}
8686

8787
mp_obj_t result = mp_obj_new_bytes((const uint8_t *)data->payload, data->payload_len);

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
image_dsc = None
1818
image = None
1919
qr_label = None
20-
status_label = None
20+
2121
use_webcam = False
2222
qr_button = None
2323
snap_button = None
2424

25-
26-
memview = None
25+
status_label = None
26+
status_label_text = "No camera found."
27+
status_label_text_searching = "Searching QR codes..."
28+
status_label_text_found = "Decoding QR..."
2729

2830
def print_qr_buffer(buffer):
2931
try:
@@ -46,17 +48,22 @@ def remove_bom(buffer):
4648
return buffer
4749

4850
def qrdecode_live():
49-
# Image dimensions
50-
buffer_size = width * height # 240 * 240 = 57600 bytes
51+
global status_label, status_label_text
5152
while keepgoing and keepliveqrdecoding:
5253
try:
5354
import qrdecode
5455
result = qrdecode.qrdecode_rgb565(current_cam_buffer, width, height)
5556
result = remove_bom(result)
5657
result = print_qr_buffer(result)
5758
print(f"QR decoding found: {result}")
58-
except Exception as e:
59-
print("QR decode error: ", e)
59+
status_label_text = f"QR decoded: {result}"
60+
stop_qr_decoding()
61+
except ValueError as e:
62+
print("QR ValueError: ", e)
63+
status_label_text = status_label_text_searching
64+
except TypeError as e:
65+
print("QR TypeError: ", e)
66+
status_label_text = status_label_text_found
6067
time.sleep_ms(500)
6168

6269

@@ -87,22 +94,32 @@ def snap_button_click(e):
8794
print(f"Error writing to file: {e}")
8895

8996

97+
def start_qr_decoding():
98+
global qr_label, keepliveqrdecoding
99+
print("Activating live QR decoding...")
100+
keepliveqrdecoding = True
101+
qr_label.set_text(lv.SYMBOL.EYE_CLOSE)
102+
try:
103+
import _thread
104+
_thread.stack_size(12*1024) # 16KB is too much
105+
_thread.start_new_thread(qrdecode_live, ())
106+
except Exception as e:
107+
print("Could not start live QR decoding thread: ", e)
108+
109+
def stop_qr_decoding():
110+
global qr_label, keepliveqrdecoding, status_label_text
111+
print("Deactivating live QR decoding...")
112+
keepliveqrdecoding = False
113+
qr_label.set_text(lv.SYMBOL.EYE_OPEN)
114+
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
115+
status_label_text = ""
116+
90117
def qr_button_click(e):
91-
global keepliveqrdecoding, qr_label
118+
global keepliveqrdecoding
92119
if not keepliveqrdecoding:
93-
print("Activating live QR decoding...")
94-
keepliveqrdecoding = True
95-
qr_label.set_text(lv.SYMBOL.EYE_CLOSE)
96-
try:
97-
import _thread
98-
_thread.stack_size(12*1024) # 16KB is too much
99-
_thread.start_new_thread(qrdecode_live, ())
100-
except Exception as e:
101-
print("Could not start live QR decoding thread: ", e)
120+
start_qr_decoding()
102121
else:
103-
print("Deactivating live QR decoding...")
104-
keepliveqrdecoding = False
105-
qr_label.set_text(lv.SYMBOL.EYE_OPEN)
122+
stop_qr_decoding()
106123

107124
def try_capture():
108125
global current_cam_buffer, image_dsc, image, use_webcam
@@ -169,7 +186,8 @@ def build_ui():
169186
})
170187
image.set_src(image_dsc)
171188
status_label = lv.label(appscreen)
172-
status_label.set_text("No camera found.")
189+
status_label.set_text(status_label_text)
190+
status_label.set_style_text_font(lv.font_montserrat_16, 0)
173191
status_label.center()
174192

175193

@@ -210,7 +228,7 @@ def init_cam():
210228

211229
cam = init_cam()
212230
if cam:
213-
image.set_rotation(900)
231+
image.set_rotation(900) # internal camera is rotated 90 degrees
214232
else:
215233
print("camtest.py: no internal camera found, trying webcam on /dev/video0")
216234
try:
@@ -221,19 +239,21 @@ def init_cam():
221239
print(f"camtest.py: webcam exception: {e}")
222240

223241
if cam:
224-
status_label.set_text("")
242+
status_label_text = ""
225243
qr_button.remove_flag(lv.obj.FLAG.HIDDEN)
226244
snap_button.remove_flag(lv.obj.FLAG.HIDDEN)
227245

228246
# Task handler needs to be updated from this app's thread, otherwise it causes concurrency issues
229247
th.disable()
230248

231249
while appscreen == lv.screen_active() and keepgoing is True:
232-
if cam:
233-
try_capture()
250+
if status_label.get_text() != status_label_text:
251+
status_label.set_text(status_label_text)
234252
lv.task_handler()
235253
time.sleep_ms(5)
236254
lv.tick_inc(5)
255+
if cam:
256+
try_capture()
237257

238258
print("camtest.py: stopping...")
239259

0 commit comments

Comments
 (0)