Skip to content

Commit 658a4c6

Browse files
camtest: add live QR decoding
1 parent 34d574a commit 658a4c6

File tree

1 file changed

+42
-12
lines changed
  • internal_filesystem/apps/com.example.camtest/assets

1 file changed

+42
-12
lines changed

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
appscreen = lv.screen_active()
22

33
keepgoing = True
4+
keepliveqrdecoding = False
45
width = 240
56
height = 240
67

@@ -48,21 +49,50 @@ def snap_button_click(e):
4849
qr_label.set_text(lv.SYMBOL.EYE_OPEN)
4950
qr_label.center()
5051

51-
def qr_button_click(e):
52-
print("Decoding QR")
52+
def process_qr_buffer(buffer):
53+
try:
54+
# Try to decode buffer as a UTF-8 string
55+
result = buffer.decode('utf-8')
56+
# Check if the string is printable (ASCII printable characters)
57+
if all(32 <= ord(c) <= 126 for c in result):
58+
return result
59+
except UnicodeDecodeError:
60+
pass
61+
# If not a valid string or not printable, convert to hex
62+
hex_str = ' '.join([f'{b:02x}' for b in buffer])
63+
return hex_str.lower()
64+
65+
def qrdecode_live():
5366
# Image dimensions
54-
width = 240
55-
height = 240
5667
buffer_size = width * height # 240 * 240 = 57600 bytes
57-
try:
58-
import qrdecode
59-
result = qrdecode.qrdecode(current_cam_buffer, width, height)
60-
if result.startswith('\ufeff'): # Remove BOM (\ufeff) from the start of the decoded string, if present
61-
result = result[1:]
62-
print(f"QR decoding found: {result}")
63-
except Exception as e:
64-
print("QR decode error: ", e)
68+
while keepgoing and keepliveqrdecoding:
69+
try:
70+
import qrdecode
71+
result = qrdecode.qrdecode(current_cam_buffer, width, height)
72+
if result.startswith('\ufeff'): # Remove BOM (\ufeff) from the start of the decoded string, if present
73+
result = result[1:]
74+
result = process_qr_buffer(result)
75+
print(f"QR decoding found: {result}")
76+
except Exception as e:
77+
print("QR decode error: ", e)
78+
time.sleep_ms(500)
6579

80+
def qr_button_click(e):
81+
global keepliveqrdecoding, qr_label
82+
if not keepliveqrdecoding:
83+
print("Activating live QR decoding...")
84+
keepliveqrdecoding = True
85+
qr_label.set_text(lv.SYMBOL.EYE_CLOSE)
86+
try:
87+
import _thread
88+
_thread.stack_size(12*1024) # 16KB is too much
89+
_thread.start_new_thread(qrdecode_live, ())
90+
except Exception as e:
91+
print("Could not start live QR decoding thread: ", e)
92+
else:
93+
print("Deactivating live QR decoding...")
94+
keepliveqrdecoding = False
95+
qr_label.set_text(lv.SYMBOL.EYE_OPEN)
6696

6797
qr_button.add_event_cb(qr_button_click,lv.EVENT.CLICKED,None)
6898

0 commit comments

Comments
 (0)