@@ -784,69 +784,39 @@ def fetch(self, refresh_url=None):
784
784
return values [0 ]
785
785
return values
786
786
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 ): # pylint: disable=invalid-name
788
788
"""Display a QR code on the TFT
789
789
790
790
:param qr_data: The data for the QR code.
791
- :param int qr_size: The size of the QR code in pixels.
792
- :param position: The (x, y) tuple position of the QR code on the display.
791
+ :param int qr_size: The scale of the QR code.
792
+ :param x: The x position of upper left corner of the QR code on the display.
793
+ :param y: The y position of upper left corner of the QR code on the display.
793
794
794
795
"""
795
796
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
-
797
+ # generate the QR code
812
798
qrcode = adafruit_miniqr .QRCode ()
813
799
qrcode .add_data (qr_data )
814
800
qrcode .make ()
815
801
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
802
+ # monochrome (2 color) palette
824
803
palette = displayio .Palette (2 )
825
804
palette [0 ] = 0xFFFFFF
826
805
palette [1 ] = 0x000000
827
806
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 )
807
+ # pylint: disable=invalid-name
808
+ # bitmap the size of the matrix, plus border, monochrome (2 colors)
809
+ qr_bitmap = displayio .Bitmap (qrcode .matrix .width + 2 , qrcode .matrix .height + 2 , 2 )
810
+ for i in range (qr_bitmap .width * qr_bitmap .height ):
811
+ qr_bitmap [i ] = 0
812
+
813
+ # transcribe QR code into bitmap
814
+ for xx in range (qrcode .matrix .width ):
815
+ for yy in range (qrcode .matrix .height ):
816
+ qr_bitmap [xx + 1 , yy + 1 ] = 1 if qrcode .matrix [xx , yy ] else 0
817
+
818
+ # display the QR code
819
+ qr_sprite = displayio .TileGrid (qr_bitmap , pixel_shader = palette )
850
820
if self ._qr_group :
851
821
try :
852
822
self ._qr_group .pop ()
@@ -855,9 +825,11 @@ def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=inval
855
825
else :
856
826
self ._qr_group = displayio .Group ()
857
827
self .splash .append (self ._qr_group )
828
+ self ._qr_group .scale = qr_size
829
+ self ._qr_group .x = x
830
+ self ._qr_group .y = y
858
831
self ._qr_group .append (qr_sprite )
859
- board .DISPLAY .refresh_soon ()
860
- board .DISPLAY .wait_for_frame ()
832
+ board .DISPLAY .show (self ._qr_group )
861
833
862
834
# return a list of lines with wordwrapping
863
835
@staticmethod
0 commit comments