-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqrgen.py
155 lines (124 loc) · 3.9 KB
/
qrgen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import badger2040
import qrcode
import time
import os
import badger_os
# Check that the qrcodes directory exists, if not, make it
try:
os.mkdir("/qrcodes")
except OSError:
pass
# Check that there is a qrcode.txt, if not preload
try:
text = open("/qrcodes/qrcode.txt", "r")
except OSError:
text = open("/qrcodes/qrcode.txt", "w")
if badger2040.is_wireless():
text.write("""https://pimoroni.com/badger2040w
Badger 2040 W
* 296x128 1-bit e-ink
* 2.4GHz wireless & RTC
* five user buttons
* user LED
* 2MB QSPI flash
Scan this code to learn
more about Badger 2040 W.
""")
else:
text.write("""https://pimoroni.com/badger2040
Badger 2040
* 296x128 1-bit e-ink
* five user buttons
* user LED
* 2MB QSPI flash
Scan this code to learn
more about Badger 2040.
""")
text.flush()
text.seek(0)
# Load all available QR Code Files
try:
CODES = [f for f in os.listdir("/qrcodes") if f.endswith(".txt")]
TOTAL_CODES = len(CODES)
except OSError:
pass
print(f'There are {TOTAL_CODES} QR Codes available:')
for codename in CODES:
print(f'File: {codename}')
display = badger2040.Badger2040()
code = qrcode.QRCode()
state = {
"current_qr": 0
}
def measure_qr_code(size, code):
w, h = code.get_size()
module_size = int(size / w)
return module_size * w, module_size
def draw_qr_code(ox, oy, size, code):
size, module_size = measure_qr_code(size, code)
display.set_pen(15)
display.rectangle(ox, oy, size, size)
display.set_pen(0)
for x in range(size):
for y in range(size):
if code.get_module(x, y):
display.rectangle(ox + x * module_size, oy + y * module_size, module_size, module_size)
def draw_qr_file(n):
display.led(128)
file = CODES[n]
codetext = open("/qrcodes/{}".format(file), "r")
lines = codetext.read().strip().split("\n")
code_text = lines.pop(0)
title_text = lines.pop(0)
detail_text = lines
# Clear the Display
display.set_pen(15) # Change this to 0 if a white background is used
display.clear()
display.set_pen(0)
code.set_text(code_text)
size, _ = measure_qr_code(128, code)
left = top = int((badger2040.HEIGHT / 2) - (size / 2))
draw_qr_code(left, top, 128, code)
left = 128 + 5
display.text(title_text, left, 20, badger2040.WIDTH, 2)
top = 40
for line in detail_text:
display.text(line, left, top, badger2040.WIDTH, 1)
top += 10
if TOTAL_CODES > 1:
for i in range(TOTAL_CODES):
x = 286
y = int((128 / 2) - (TOTAL_CODES * 10 / 2) + (i * 10))
display.set_pen(0)
display.rectangle(x, y, 8, 8)
if state["current_qr"] != i:
display.set_pen(15)
display.rectangle(x + 1, y + 1, 6, 6)
display.update()
badger_os.state_load("qrcodes", state)
changed = True
while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()
if TOTAL_CODES > 1:
if display.pressed(badger2040.BUTTON_UP):
if state["current_qr"] > 0:
state["current_qr"] -= 1
changed = True
if display.pressed(badger2040.BUTTON_DOWN):
if state["current_qr"] < TOTAL_CODES - 1:
state["current_qr"] += 1
changed = True
if display.pressed(badger2040.BUTTON_B) or display.pressed(badger2040.BUTTON_C):
display.set_pen(15)
display.clear()
badger_os.warning(display, "To add QR codes, connect Badger 2040 W to a PC, load up Thonny, and add files to /qrcodes directory.")
time.sleep(4)
changed = True
if changed:
draw_qr_file(state["current_qr"])
badger_os.state_save("qrcodes", state)
changed = False
# Halt the Badger to save power, it will wake up if any of the front buttons are pressed
display.halt()