Skip to content

Commit eb7eaae

Browse files
committed
Fixes 'list index out of range' if current_qr state is outside CODES range
1 parent 884722d commit eb7eaae

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

micropython/examples/badger2040/qrgen.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@
4949
"current_qr": 0
5050
}
5151

52+
def set_state_current_index_in_range():
53+
badger_os.state_load("qrcodes", state)
54+
55+
if state["current_qr"] >= len(CODES):
56+
state["current_qr"] = len(CODES) - 1 # set to last index (zero-based). Note: will set to -1 if currently 0
57+
58+
# check that the index is not negative, thus still out of range
59+
if state["current_qr"] < 0:
60+
state["current_qr"] = 0
61+
62+
badger_os.state_save("qrcodes", state)
63+
5264

5365
def measure_qr_code(size, code):
5466
w, h = code.get_size()
@@ -110,7 +122,7 @@ def draw_qr_file(n):
110122
display.update()
111123

112124

113-
badger_os.state_load("qrcodes", state)
125+
set_state_current_index_in_range()
114126
changed = not badger2040.woken_by_button()
115127

116128
while True:

0 commit comments

Comments
 (0)