-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_manual.py
More file actions
62 lines (50 loc) · 1.71 KB
/
example_manual.py
File metadata and controls
62 lines (50 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# M5Stack Unit-QRCode — manual scan mode
#
# Either press GP15 or the physical button on the QR module itself to scan.
# As soon as a QR code is decoded the result is printed.
#
# Wiring (Grove connector on Unit-QRCode):
# Yellow (SCL) -> GP5
# White (SDA) -> GP4
# Red (3V3) -> 3V3
# Black (GND) -> GND
#
# Button wiring (optional external button):
# One side -> GP15
# Other side -> GND (internal pull-up is used)
import board
import busio
import digitalio
import time
from m5_unit_qrcode import UnitQRCode, MANUAL_SCAN
i2c = busio.I2C(board.GP5, board.GP4)
scanner = UnitQRCode(i2c)
scanner.scan_mode = MANUAL_SCAN
button = digitalio.DigitalInOut(board.GP15)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP # button connects GP15 to GND when pressed
print("Ready — press GP15 or the button on the QR module to scan.")
scanning = False
button_was_pressed = False
while True:
# GP15 external button (active LOW via pull-up)
pressed = not button.value
# Module's own physical button (active LOW per protocol doc: 0 = pressed)
module_button = scanner.trigger_key == 0
if (pressed and not button_was_pressed) or module_button:
if not scanning:
print("Scanning…")
scanner.trigger(start=True)
scanning = True
if not pressed and button_was_pressed and not module_button and scanning:
scanner.trigger(start=False)
scanning = False
print("Cancelled.")
if scanning and scanner.ready:
result = scanner.read()
if result:
scanner.trigger(start=False)
scanning = False
print("Scanned:", result)
button_was_pressed = pressed
time.sleep(0.02)