Skip to content

Commit d46c585

Browse files
committed
update show_QR
1 parent 3da6b3c commit d46c585

File tree

1 file changed

+18
-50
lines changed

1 file changed

+18
-50
lines changed

adafruit_pyportal.py

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -784,69 +784,35 @@ def fetch(self, refresh_url=None):
784784
return values[0]
785785
return values
786786

787-
def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=invalid-name
787+
def show_QR(self, qr_data, qr_size=1, x=0, y=0):
788788
"""Display a QR code on the TFT
789789
790790
:param qr_data: The data for the QR code.
791-
:param int qr_size: The size of the QR code in pixels.
791+
:param int qr_size: The scale of the QR code.
792792
:param position: The (x, y) tuple position of the QR code on the display.
793793
794794
"""
795795
import adafruit_miniqr
796-
797-
if not qr_data: # delete it
798-
if self._qr_group:
799-
try:
800-
self._qr_group.pop()
801-
except IndexError:
802-
pass
803-
board.DISPLAY.refresh_soon()
804-
board.DISPLAY.wait_for_frame()
805-
return
806-
807-
if not position:
808-
position = (0, 0)
809-
if qr_size % 32 != 0:
810-
raise RuntimeError("QR size must be divisible by 32")
811-
796+
# generate the QR code
812797
qrcode = adafruit_miniqr.QRCode()
813798
qrcode.add_data(qr_data)
814799
qrcode.make()
815800

816-
# pylint: disable=invalid-name
817-
# how big each pixel is, add 2 blocks on either side
818-
BLOCK_SIZE = qr_size // (qrcode.matrix.width+4)
819-
# Center the QR code in the middle
820-
X_OFFSET = (qr_size - BLOCK_SIZE * qrcode.matrix.width) // 2
821-
Y_OFFSET = (qr_size - BLOCK_SIZE * qrcode.matrix.height) // 2
822-
823-
# monochome (2 color) palette
801+
# monochrome (2 color) palette
824802
palette = displayio.Palette(2)
825803
palette[0] = 0xFFFFFF
826804
palette[1] = 0x000000
827805

828-
# bitmap the size of the matrix + borders, monochrome (2 colors)
829-
qr_bitmap = displayio.Bitmap(qr_size, qr_size, 2)
830-
831-
# raster the QR code
832-
line = bytearray(qr_size // 8) # monochrome means 8 pixels per byte
833-
for y in range(qrcode.matrix.height): # each scanline in the height
834-
for i, _ in enumerate(line): # initialize it to be empty
835-
line[i] = 0
836-
for x in range(qrcode.matrix.width):
837-
if qrcode.matrix[x, y]:
838-
for b in range(BLOCK_SIZE):
839-
_x = X_OFFSET + x * BLOCK_SIZE + b
840-
line[_x // 8] |= 1 << (7-(_x % 8))
841-
842-
for b in range(BLOCK_SIZE):
843-
# load this line of data in, as many time as block size
844-
for i, byte in enumerate(line):
845-
qr_bitmap[Y_OFFSET + y*BLOCK_SIZE+b + i] = byte
846-
# pylint: enable=invalid-name
847-
848-
# display the bitmap using our palette
849-
qr_sprite = displayio.Sprite(qr_bitmap, pixel_shader=palette, position=position)
806+
# bitmap the size of the matrix, monochrome (2 colors)
807+
qr_bitmap = displayio.Bitmap(qrcode.matrix.width, qrcode.matrix.height, 2)
808+
809+
# transcribe QR code into bitmap
810+
for xx in range(qrcode.matrix.width):
811+
for yy in range(qrcode.matrix.height):
812+
qr_bitmap[xx, yy] = 1 if qrcode.matrix[xx, yy] else 0
813+
814+
# display the QR code
815+
qr_sprite = displayio.TileGrid(qr_bitmap, pixel_shader=palette)
850816
if self._qr_group:
851817
try:
852818
self._qr_group.pop()
@@ -855,9 +821,11 @@ def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=inval
855821
else:
856822
self._qr_group = displayio.Group()
857823
self.splash.append(self._qr_group)
824+
self._qr_group.scale = qr_size
825+
self._qr_group.x = x
826+
self._qr_group.y = y
858827
self._qr_group.append(qr_sprite)
859-
board.DISPLAY.refresh_soon()
860-
board.DISPLAY.wait_for_frame()
828+
board.DISPLAY.show(self._qr_group)
861829

862830
# return a list of lines with wordwrapping
863831
@staticmethod

0 commit comments

Comments
 (0)