Skip to content

Commit f17c04b

Browse files
authored
Merge pull request #658 from SimonShirley/main
List index out of range fix, remove default QR message if text files are available and file housekeeping
2 parents b08cf1f + cd95edc commit f17c04b

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

micropython/examples/badger2040/qrgen.py

Lines changed: 32 additions & 18 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+
new_qr_code_filename = "qrcode.txt"
23+
with open(f"/qrcodes/{new_qr_code_filename}", "w") as text:
24+
text.write("""https://pimoroni.com/badger2040
1925
Badger 2040
2026
* 296x128 1-bit e-ink
2127
* six user buttons
@@ -25,20 +31,15 @@
2531
Scan this code to learn
2632
more about Badger 2040.
2733
""")
28-
text.flush()
29-
text.seek(0)
34+
text.flush()
3035

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
36+
# Set the CODES list to contain the new_qr_code_filename (created above)
37+
CODES = [new_qr_code_filename]
3738

39+
except OSError:
40+
CODES = []
3841

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

4344
display = badger2040.Badger2040()
4445

@@ -50,6 +51,15 @@
5051
}
5152

5253

54+
def set_state_current_index_in_range():
55+
badger_os.state_load("qrcodes", state)
56+
if state["current_qr"] >= len(CODES):
57+
state["current_qr"] = len(CODES) - 1 # set to last index (zero-based). Note: will set to -1 if currently 0
58+
if state["current_qr"] < 0: # check that the index is not negative, thus still out of range
59+
state["current_qr"] = 0
60+
badger_os.state_save("qrcodes", state)
61+
62+
5363
def measure_qr_code(size, code):
5464
w, h = code.get_size()
5565
module_size = int(size / w)
@@ -70,9 +80,13 @@ def draw_qr_code(ox, oy, size, code):
7080
def draw_qr_file(n):
7181
display.led(128)
7282
file = CODES[n]
73-
codetext = open("qrcodes/{}".format(file), "r")
7483

75-
lines = codetext.read().strip().split("\n")
84+
try:
85+
with open(f"/qrcodes/{file}", "r") as codetext:
86+
lines = codetext.read().strip().split("\n")
87+
except OSError:
88+
lines = ["", "", "", "", "", "", "", "", "", ""]
89+
7690
code_text = lines.pop(0)
7791
title_text = lines.pop(0)
7892
detail_text = lines
@@ -110,7 +124,7 @@ def draw_qr_file(n):
110124
display.update()
111125

112126

113-
badger_os.state_load("qrcodes", state)
127+
set_state_current_index_in_range()
114128
changed = not badger2040.woken_by_button()
115129

116130
while True:

0 commit comments

Comments
 (0)