|
1 | 1 | appscreen = lv.screen_active() |
2 | 2 |
|
3 | 3 | keepgoing = True |
| 4 | +keepliveqrdecoding = False |
4 | 5 | width = 240 |
5 | 6 | height = 240 |
6 | 7 |
|
@@ -48,21 +49,50 @@ def snap_button_click(e): |
48 | 49 | qr_label.set_text(lv.SYMBOL.EYE_OPEN) |
49 | 50 | qr_label.center() |
50 | 51 |
|
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(): |
53 | 66 | # Image dimensions |
54 | | - width = 240 |
55 | | - height = 240 |
56 | 67 | 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) |
65 | 79 |
|
| 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) |
66 | 96 |
|
67 | 97 | qr_button.add_event_cb(qr_button_click,lv.EVENT.CLICKED,None) |
68 | 98 |
|
|
0 commit comments