Skip to content

Commit f31fcc2

Browse files
committed
update example to latest displayio
1 parent e17b62b commit f31fcc2

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

examples/miniqr_displaytest.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,44 @@
33
import displayio
44
import adafruit_miniqr
55

6-
DISPLAY_W = 128
7-
DISPLAY_H = 128
8-
9-
backlight = pulseio.PWMOut(board.TFT_BACKLIGHT)
10-
backlight.duty_cycle = 0
11-
12-
def draw_QR(matrix):
13-
# how big each pixel is, add 2 blocks on either side
14-
BLOCK_SIZE = DISPLAY_W // (matrix.width+4)
15-
16-
# Center the QR code in the middle of the screen
17-
X_OFFSET = (DISPLAY_W - BLOCK_SIZE * matrix.width) // 2
18-
Y_OFFSET = (DISPLAY_H - BLOCK_SIZE * matrix.height) // 2
19-
6+
def bitmap_QR(matrix):
207
# monochome (2 color) palette
21-
palette = displayio.Palette(2)
22-
palette[0] = 0xFFFFFF
23-
palette[1] = 0x000000
8+
BORDER_PIXELS = 2
249

2510
# bitmap the size of the screen, monochrome (2 colors)
26-
bitmap = displayio.Bitmap(DISPLAY_H, DISPLAY_W, 2)
27-
11+
bitmap = displayio.Bitmap(matrix.width+2*BORDER_PIXELS,
12+
matrix.height+2*BORDER_PIXELS, 2)
2813
# raster the QR code
29-
line = bytearray(DISPLAY_W // 8) # monochrome means 8 pixels per byte
3014
for y in range(matrix.height): # each scanline in the height
31-
for i, _ in enumerate(line): # initialize it to be empty
32-
line[i] = 0
3315
for x in range(matrix.width):
3416
if matrix[x, y]:
35-
for b in range(BLOCK_SIZE):
36-
_x = X_OFFSET + x * BLOCK_SIZE + b
37-
line[_x // 8] |= 1 << (7-(_x % 8))
38-
39-
for b in range(BLOCK_SIZE):
40-
# load this line of data in, as many time as block size
41-
#pylint: disable=protected-access
42-
bitmap._load_row(Y_OFFSET + y*BLOCK_SIZE+b, line)
17+
bitmap[x+BORDER_PIXELS, y+BORDER_PIXELS] = 1
18+
else:
19+
bitmap[x+BORDER_PIXELS, y+BORDER_PIXELS] = 0
20+
return bitmap
4321

44-
# display the bitmap using our palette
45-
splash = displayio.Group()
46-
face = displayio.Sprite(bitmap, pixel_shader=palette, position=(0, 0))
47-
splash.append(face)
48-
board.DISPLAY.show(splash)
49-
board.DISPLAY.wait_for_frame()
50-
51-
qr = adafruit_miniqr.QRCode()
22+
qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
5223
qr.add_data(b'https://www.adafruit.com/circuitpython')
5324
qr.make()
54-
draw_QR(qr.matrix)
55-
56-
# turn on backlight
57-
backlight.duty_cycle = 35000
5825

26+
# generate the 1-pixel-per-bit bitmap
27+
qr_bitmap = bitmap_QR(qr.matrix)
28+
# We'll draw with a classic black/white palette
29+
palette = displayio.Palette(2)
30+
palette[0] = 0xFFFFFF
31+
palette[1] = 0x000000
32+
# we'll scale the QR code as big as the display can handle
33+
scale = min(board.DISPLAY.width//qr_bitmap.width, board.DISPLAY.height//qr_bitmap.height)
34+
# then center it!
35+
x = int (((board.DISPLAY.width/scale) - qr_bitmap.width) / 2)
36+
y = int (((board.DISPLAY.height/scale) - qr_bitmap.height) / 2)
37+
qr_img = displayio.TileGrid(qr_bitmap, pixel_shader=palette, x=x, y=y)
38+
39+
splash = displayio.Group(scale=scale)
40+
splash.append(qr_img)
41+
board.DISPLAY.show(splash)
42+
board.DISPLAY.wait_for_frame()
43+
44+
# Hang out forever
5945
while True:
6046
pass

0 commit comments

Comments
 (0)