Skip to content

Commit

Permalink
Merge pull request #268 from pimoroni/feature/qrcode
Browse files Browse the repository at this point in the history
QRCode Library
  • Loading branch information
Gadgetoid committed Mar 11, 2022
2 parents 9079a11 + 421d1ed commit 80e4558
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
[submodule "drivers/scd4x/src"]
path = drivers/scd4x/src
url = https://github.com/Sensirion/embedded-i2c-scd4x
[submodule "micropython/modules/qrcode"]
path = micropython/modules/qrcode
url = https://github.com/pimoroni/QR-Code-Generator
Binary file modified micropython/examples/badger2040/assets/launchericons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions micropython/examples/badger2040/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
inverted = False

icons = bytearray(launchericons.data())
icons_width = 576

examples = [
("_clock", 0),
Expand All @@ -24,6 +25,7 @@
("_image", 3),
("_list", 4),
("_badge", 5),
("_qrgen", 8),
("_info", 6),
("_help", 7)
]
Expand Down Expand Up @@ -139,9 +141,9 @@ def render():
for i in range(max_icons):
x = centers[i]
label, icon = examples[i + (page * 3)]
label = label[1:]
label = label[1:].replace("_", " ")
display.pen(0)
display.icon(icons, icon, 512, 64, x - 32, 24)
display.icon(icons, icon, icons_width, 64, x - 32, 24)
w = display.measure_text(label, font_sizes[font_size])
display.text(label, x - int(w / 2), 16 + 80, font_sizes[font_size])

Expand Down
1 change: 1 addition & 0 deletions micropython/examples/badger2040/micropython-builtins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ copy_module(usermod_badger2040 ${CMAKE_CURRENT_LIST_DIR}/list.py _list)
copy_module(usermod_badger2040 ${CMAKE_CURRENT_LIST_DIR}/badge.py _badge)
copy_module(usermod_badger2040 ${CMAKE_CURRENT_LIST_DIR}/help.py _help)
copy_module(usermod_badger2040 ${CMAKE_CURRENT_LIST_DIR}/info.py _info)
copy_module(usermod_badger2040 ${CMAKE_CURRENT_LIST_DIR}/qrgen.py _qrgen)
70 changes: 70 additions & 0 deletions micropython/examples/badger2040/qrgen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import badger2040
import qrcode
import time


# Open the qrcode file
try:
text = open("qrcode.txt", "r")
except OSError:
text = open("qrcode.txt", "w")
text.write("""https://pimoroni.com/badger2040
Badger 2040
* 296x128 1-bit e-ink
* six user buttons
* user LED
* 2MB QSPI flash
Scan this code to learn
more about Badger 2040.
""")
text.flush()
text.seek(0)


lines = text.read().strip().split("\n")
code_text = lines.pop(0)
title_text = lines.pop(0)
detail_text = lines

display = badger2040.Badger2040()
code = qrcode.QRCode()


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.pen(15)
display.rectangle(ox, oy, size, size)
display.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)


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.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

display.update()

while True:
time.sleep(1.0)
43 changes: 43 additions & 0 deletions micropython/examples/pico_display/basic_qrcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import picodisplay as display # Comment this line out to use PicoDisplay2
# import picodisplay2 as display # Uncomment this line to use PicoDisplay2
import qrcode


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(128, 128, 128)
display.rectangle(ox, oy, size, size)
display.set_pen(0, 0, 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)


width = display.get_width()
height = display.get_height()

code = qrcode.QRCode()
code.set_text("shop.pimoroni.com")

display.init(bytearray(width * height * 2))

display.set_pen(128, 128, 128)
display.clear()
display.set_pen(0, 0, 0)

size, module_size = measure_qr_code(height, code)
left = int((width // 2) - (size // 2))
top = int((height // 2) - (size // 2))
draw_qr_code(left, top, height, code)

display.update()

while True:
pass
1 change: 1 addition & 0 deletions micropython/modules/badger2040-micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ include(badger2040/micropython)
include(micropython/examples/badger2040/micropython-builtins)
include(plasma/micropython)
include(ulab/code/micropython)
include(qrcode/micropython/micropython)
3 changes: 2 additions & 1 deletion micropython/modules/micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ include(pico_wireless/micropython)
include(plasma/micropython)
include(hub75/micropython)
include(ulab/code/micropython)
include(qrcode/micropython/micropython)

include(modules_py/modules_py)
include(modules_py/modules_py)
1 change: 1 addition & 0 deletions micropython/modules/qrcode
Submodule qrcode added at b5afd3

0 comments on commit 80e4558

Please sign in to comment.