Skip to content

Commit 77ee234

Browse files
committed
Only create and show demo QR code if no other QR codes are available
1 parent eb7eaae commit 77ee234

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

micropython/examples/badger2040/qrgen.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
except OSError:
1111
pass
1212

13-
# Check that there is a qrcode.txt, if not preload
13+
# Load all available QR Code Files
1414
try:
15-
text = open("qrcodes/qrcode.txt", "r")
15+
CODES = [f for f in os.listdir("/qrcodes") if f.endswith(".txt")]
1616
except OSError:
17-
text = open("qrcodes/qrcode.txt", "w")
18-
text.write("""https://pimoroni.com/badger2040
17+
CODES = []
18+
19+
# create demo QR code file if no QR code files exist
20+
if len(CODES) == 0:
21+
try:
22+
newQRcodeFilename = "qrcode.txt"
23+
text = open("qrcodes/{}".format(newQRcodeFilename), "w")
24+
text.write("""https://pimoroni.com/badger2040
1925
Badger 2040
2026
* 296x128 1-bit e-ink
2127
* six user buttons
@@ -25,20 +31,16 @@
2531
Scan this code to learn
2632
more about Badger 2040.
2733
""")
28-
text.flush()
29-
text.seek(0)
34+
text.flush()
35+
text.seek(0)
3036

31-
# Load all available QR Code Files
32-
try:
33-
CODES = [f for f in os.listdir("/qrcodes") if f.endswith(".txt")]
34-
TOTAL_CODES = len(CODES)
35-
except OSError:
36-
pass
37+
# Set the CODES list to contain the newQRcodeFilename (created above)
38+
CODES = [newQRcodeFilename]
3739

40+
except:
41+
CODES = []
3842

39-
print(f'There are {TOTAL_CODES} QR Codes available:')
40-
for codename in CODES:
41-
print(f'File: {codename}')
43+
TOTAL_CODES = len(CODES)
4244

4345
display = badger2040.Badger2040()
4446

@@ -60,7 +62,7 @@ def set_state_current_index_in_range():
6062
state["current_qr"] = 0
6163

6264
badger_os.state_save("qrcodes", state)
63-
65+
6466

6567
def measure_qr_code(size, code):
6668
w, h = code.get_size()

0 commit comments

Comments
 (0)