Skip to content

Commit 5fe2b07

Browse files
authored
Merge pull request #9 from ladyada/master
update example to latest displayio
2 parents e17b62b + ca46fce commit 5fe2b07

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed

examples/miniqr_displaytest.py

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,45 @@
11
import board
2-
import pulseio
32
import displayio
43
import adafruit_miniqr
54

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-
5+
def bitmap_QR(matrix):
206
# monochome (2 color) palette
21-
palette = displayio.Palette(2)
22-
palette[0] = 0xFFFFFF
23-
palette[1] = 0x000000
7+
BORDER_PIXELS = 2
248

259
# bitmap the size of the screen, monochrome (2 colors)
26-
bitmap = displayio.Bitmap(DISPLAY_H, DISPLAY_W, 2)
27-
10+
bitmap = displayio.Bitmap(matrix.width+2*BORDER_PIXELS,
11+
matrix.height+2*BORDER_PIXELS, 2)
2812
# raster the QR code
29-
line = bytearray(DISPLAY_W // 8) # monochrome means 8 pixels per byte
3013
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
3314
for x in range(matrix.width):
3415
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)
16+
bitmap[x+BORDER_PIXELS, y+BORDER_PIXELS] = 1
17+
else:
18+
bitmap[x+BORDER_PIXELS, y+BORDER_PIXELS] = 0
19+
return bitmap
4320

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()
21+
qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
5222
qr.add_data(b'https://www.adafruit.com/circuitpython')
5323
qr.make()
54-
draw_QR(qr.matrix)
55-
56-
# turn on backlight
57-
backlight.duty_cycle = 35000
5824

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

0 commit comments

Comments
 (0)