List index out of range fix, remove default QR message if text files are available and file housekeeping #658
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello!
I'm planning on taking my Badger2040 to an event soon and in doing so, needed to update the QR codes that were available for display in qrgen.py. In setting up the new files, I ended up with one fewer file than I had previously uploaded and so when trying to display the QR codes from the launcher, instead of seeing the first QR code, I received a warning saying "List index out of range".
After some debugging, I discovered that this was because the state/qrcodes.json file had remembered the last QR code viewed and that the index of this was now higher than the count of text files in the qrcodes folder.
Commit eb7eaae loads the state, checks to see if the last viewed index is in range and if the index isn't, set it in range. Finally, saving the state back.
It is a little annoying for the demo QR code to show, regardless of whether QR code files have been uploaded, so commit 77ee234 checks to see if there are any QR code text files in the qrcodes directory and only create the demo screen file if there aren't any.
I also noticed that files are being opened on screen refresh but not closed. I don't know if micropython automatically closes these file handles after a set period (like garbage collection) but commit 9aedf16 closes them explicitly, in case these aren't tidied up automatically.