Merge pull request #333 from pimoroni/patch-jpw-multi-qrcodes
Badger 2040: Support for multiple QR codes.
This commit is contained in:
commit
832d0f08dc
|
@ -1,13 +1,21 @@
|
||||||
import badger2040
|
import badger2040
|
||||||
import qrcode
|
import qrcode
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import badger_os
|
||||||
|
|
||||||
|
# Check that the qrcodes directory exists, if not, make it
|
||||||
# Open the qrcode file
|
|
||||||
try:
|
try:
|
||||||
text = open("qrcode.txt", "r")
|
os.mkdir("qrcodes")
|
||||||
except OSError:
|
except OSError:
|
||||||
with open("qrcode.txt", "w") as text:
|
pass
|
||||||
text.write("""https://pimoroni.com/badger2040
|
|
||||||
|
# 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")
|
||||||
|
text.write("""https://pimoroni.com/badger2040
|
||||||
Badger 2040
|
Badger 2040
|
||||||
* 296x128 1-bit e-ink
|
* 296x128 1-bit e-ink
|
||||||
* six user buttons
|
* six user buttons
|
||||||
|
@ -17,20 +25,31 @@ Badger 2040
|
||||||
Scan this code to learn
|
Scan this code to learn
|
||||||
more about Badger 2040.
|
more about Badger 2040.
|
||||||
""")
|
""")
|
||||||
text.flush()
|
text.flush()
|
||||||
text = open("qrcode.txt", "r")
|
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
|
||||||
|
|
||||||
|
|
||||||
lines = text.read().strip().split("\n")
|
print(f'There are {TOTAL_CODES} QR Codes available:')
|
||||||
code_text = lines.pop(0)
|
for codename in CODES:
|
||||||
title_text = lines.pop(0)
|
print(f'File: {codename}')
|
||||||
detail_text = lines
|
|
||||||
|
|
||||||
display = badger2040.Badger2040()
|
display = badger2040.Badger2040()
|
||||||
display.led(128)
|
|
||||||
code = qrcode.QRCode()
|
code = qrcode.QRCode()
|
||||||
|
|
||||||
|
|
||||||
|
state = {
|
||||||
|
"current_qr": 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def measure_qr_code(size, code):
|
def measure_qr_code(size, code):
|
||||||
w, h = code.get_size()
|
w, h = code.get_size()
|
||||||
module_size = int(size / w)
|
module_size = int(size / w)
|
||||||
|
@ -48,25 +67,75 @@ def draw_qr_code(ox, oy, size, code):
|
||||||
display.rectangle(ox + x * module_size, oy + y * module_size, module_size, module_size)
|
display.rectangle(ox + x * module_size, oy + y * module_size, module_size, module_size)
|
||||||
|
|
||||||
|
|
||||||
code.set_text(code_text)
|
def draw_qr_file(n):
|
||||||
size, _ = measure_qr_code(128, code)
|
display.led(128)
|
||||||
left = top = int((badger2040.HEIGHT / 2) - (size / 2))
|
file = CODES[n]
|
||||||
draw_qr_code(left, top, 128, code)
|
codetext = open("qrcodes/{}".format(file), "r")
|
||||||
|
|
||||||
left = 128 + 5
|
lines = codetext.read().strip().split("\n")
|
||||||
|
code_text = lines.pop(0)
|
||||||
|
title_text = lines.pop(0)
|
||||||
|
detail_text = lines
|
||||||
|
|
||||||
display.thickness(2)
|
# Clear the Display
|
||||||
display.text(title_text, left, 20, 0.5)
|
display.pen(15) # Change this to 0 if a white background is used
|
||||||
display.thickness(1)
|
display.clear()
|
||||||
|
display.pen(0)
|
||||||
|
|
||||||
top = 40
|
code.set_text(code_text)
|
||||||
for line in detail_text:
|
size, _ = measure_qr_code(128, code)
|
||||||
display.text(line, left, top, 0.4)
|
left = top = int((badger2040.HEIGHT / 2) - (size / 2))
|
||||||
top += 10
|
draw_qr_code(left, top, 128, code)
|
||||||
|
|
||||||
display.update()
|
left = 128 + 5
|
||||||
|
|
||||||
|
display.thickness(2)
|
||||||
|
display.text(title_text, left, 20, 0.5)
|
||||||
|
display.thickness(1)
|
||||||
|
|
||||||
|
top = 40
|
||||||
|
for line in detail_text:
|
||||||
|
display.text(line, left, top, 0.4)
|
||||||
|
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.pen(0)
|
||||||
|
display.rectangle(x, y, 8, 8)
|
||||||
|
if state["current_qr"] != i:
|
||||||
|
display.pen(15)
|
||||||
|
display.rectangle(x + 1, y + 1, 6, 6)
|
||||||
|
display.update()
|
||||||
|
|
||||||
|
|
||||||
|
badger_os.state_load("qrcodes", state)
|
||||||
|
changed = not badger2040.woken_by_button()
|
||||||
|
|
||||||
# Call halt in a loop, on battery this switches off power.
|
|
||||||
# On USB, the app will exit when A+C is pressed because the launcher picks that up.
|
|
||||||
while True:
|
while True:
|
||||||
|
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.pen(15)
|
||||||
|
display.clear()
|
||||||
|
badger_os.warning(display, "To add QR codes, connect Badger2040 to a PC, load up Thonny, and see qrgen.py.")
|
||||||
|
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()
|
display.halt()
|
||||||
|
|
Loading…
Reference in New Issue