From 41105ee5e4703bec161ab81dd8d07e1672ef2edf Mon Sep 17 00:00:00 2001 From: Marcin Juszkiewicz Date: Thu, 9 May 2024 09:03:21 +0200 Subject: [PATCH 1/3] qrgen: loop entries on Up/Down presses Up on first == show last Down on last == show first Signed-off-by: Marcin Juszkiewicz --- badger_os/examples/qrgen.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/badger_os/examples/qrgen.py b/badger_os/examples/qrgen.py index 0914819..d47bab0 100644 --- a/badger_os/examples/qrgen.py +++ b/badger_os/examples/qrgen.py @@ -133,11 +133,16 @@ def draw_qr_file(n): if state["current_qr"] > 0: state["current_qr"] -= 1 changed = True + else: + state["current_qr"] = TOTAL_CODES - 1 + changed = True - if display.pressed(badger2040.BUTTON_DOWN): + elif display.pressed(badger2040.BUTTON_DOWN): if state["current_qr"] < TOTAL_CODES - 1: state["current_qr"] += 1 - changed = True + else: + state["current_qr"] = 0 + changed = True if display.pressed(badger2040.BUTTON_B) or display.pressed(badger2040.BUTTON_C): display.set_pen(15) From 7d54118eb152c6819078a13649ba06dd1030312c Mon Sep 17 00:00:00 2001 From: Marcin Juszkiewicz Date: Thu, 9 May 2024 09:05:20 +0200 Subject: [PATCH 2/3] qrgen: handle 'list out of range' situation Code tries to show entry shown last time. But if there are less qrcodes/ files then it goes out of range. Signed-off-by: Marcin Juszkiewicz --- badger_os/examples/qrgen.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/badger_os/examples/qrgen.py b/badger_os/examples/qrgen.py index d47bab0..9b5508a 100644 --- a/badger_os/examples/qrgen.py +++ b/badger_os/examples/qrgen.py @@ -121,6 +121,11 @@ def draw_qr_file(n): badger_os.state_load("qrcodes", state) + +# When we removed some files code may try to display not existing entry +if state["current_qr"] > TOTAL_CODES - 1: + state["current_qr"] = 0 + changed = True while True: From 22d1ff807423b55f08f86046f009634b200da538 Mon Sep 17 00:00:00 2001 From: Marcin Juszkiewicz Date: Fri, 10 May 2024 12:05:57 +0200 Subject: [PATCH 3/3] qrgen: sort by file names --- badger_os/examples/qrgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/badger_os/examples/qrgen.py b/badger_os/examples/qrgen.py index 9b5508a..aded95f 100644 --- a/badger_os/examples/qrgen.py +++ b/badger_os/examples/qrgen.py @@ -43,7 +43,7 @@ # Load all available QR Code Files try: - CODES = [f for f in os.listdir("/qrcodes") if f.endswith(".txt")] + CODES = sorted([f for f in os.listdir("/qrcodes") if f.endswith(".txt")]) TOTAL_CODES = len(CODES) except OSError: pass