19
19
# create demo QR code file if no QR code files exist
20
20
if len (CODES ) == 0 :
21
21
try :
22
- newQRcodeFilename = "qrcode.txt"
23
- text = open (" qrcodes/{}" . format ( newQRcodeFilename ) , "w" )
24
- text .write ("""https://pimoroni.com/badger2040
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
25
25
Badger 2040
26
26
* 296x128 1-bit e-ink
27
27
* six user buttons
31
31
Scan this code to learn
32
32
more about Badger 2040.
33
33
""" )
34
- text .flush ()
35
- text .close ()
34
+ text .flush ()
36
35
37
- # Set the CODES list to contain the newQRcodeFilename (created above)
38
- CODES = [newQRcodeFilename ]
36
+ # Set the CODES list to contain the new_qr_code_filename (created above)
37
+ CODES = [new_qr_code_filename ]
39
38
40
- except :
39
+ except OSError :
41
40
CODES = []
42
41
43
42
TOTAL_CODES = len (CODES )
51
50
"current_qr" : 0
52
51
}
53
52
53
+
54
54
def set_state_current_index_in_range ():
55
55
badger_os .state_load ("qrcodes" , state )
56
-
57
56
if state ["current_qr" ] >= len (CODES ):
58
57
state ["current_qr" ] = len (CODES ) - 1 # set to last index (zero-based). Note: will set to -1 if currently 0
59
-
60
- # check that the index is not negative, thus still out of range
61
- if state ["current_qr" ] < 0 :
58
+ if state ["current_qr" ] < 0 : # check that the index is not negative, thus still out of range
62
59
state ["current_qr" ] = 0
63
-
64
60
badger_os .state_save ("qrcodes" , state )
65
-
61
+
66
62
67
63
def measure_qr_code (size , code ):
68
64
w , h = code .get_size ()
@@ -86,12 +82,9 @@ def draw_qr_file(n):
86
82
file = CODES [n ]
87
83
88
84
try :
89
- codetext = open ("qrcodes/{}" .format (file ), "r" )
90
-
91
- lines = codetext .read ().strip ().split ("\n " )
92
-
93
- codetext .close ()
94
- except :
85
+ with open (f"/qrcodes/{ file } " , "r" ) as codetext :
86
+ lines = codetext .read ().strip ().split ("\n " )
87
+ except OSError :
95
88
lines = ["" , "" , "" , "" , "" , "" , "" , "" , "" , "" ]
96
89
97
90
code_text = lines .pop (0 )
0 commit comments