|
1 | 1 | import board
|
2 |
| -import pulseio |
3 | 2 | import displayio
|
4 | 3 | import adafruit_miniqr
|
5 | 4 |
|
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): |
20 | 6 | # monochome (2 color) palette
|
21 |
| - palette = displayio.Palette(2) |
22 |
| - palette[0] = 0xFFFFFF |
23 |
| - palette[1] = 0x000000 |
| 7 | + BORDER_PIXELS = 2 |
24 | 8 |
|
25 | 9 | # 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) |
28 | 12 | # raster the QR code
|
29 |
| - line = bytearray(DISPLAY_W // 8) # monochrome means 8 pixels per byte |
30 | 13 | 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 |
33 | 14 | for x in range(matrix.width):
|
34 | 15 | 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 |
43 | 20 |
|
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) |
52 | 22 | qr.add_data(b'https://www.adafruit.com/circuitpython')
|
53 | 23 | qr.make()
|
54 |
| -draw_QR(qr.matrix) |
55 |
| - |
56 |
| -# turn on backlight |
57 |
| -backlight.duty_cycle = 35000 |
58 | 24 |
|
| 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 |
59 | 44 | while True:
|
60 | 45 | pass
|
0 commit comments