|
| 1 | +import board |
| 2 | +import pulseio |
| 3 | +import displayio |
| 4 | +import adafruit_miniqr |
| 5 | + |
| 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 // (qr.matrix.width+4) |
| 15 | + |
| 16 | + # Center the QR code in the middle of the screen |
| 17 | + X_OFFSET = (DISPLAY_W - BLOCK_SIZE * qr.matrix.width) // 2 |
| 18 | + Y_OFFSET = (DISPLAY_H - BLOCK_SIZE * qr.matrix.height) // 2 |
| 19 | + |
| 20 | + # monochome (2 color) palette |
| 21 | + palette = displayio.Palette(2) |
| 22 | + palette[0] = 0xFFFFFF |
| 23 | + palette[1] = 0x000000 |
| 24 | + |
| 25 | + # bitmap the size of the screen, monochrome (2 colors) |
| 26 | + bitmap = displayio.Bitmap(DISPLAY_H, DISPLAY_W, 2) |
| 27 | + |
| 28 | + # raster the QR code |
| 29 | + line = bytearray(DISPLAY_W // 8) # monochrome means 8 pixels per byte |
| 30 | + for y in range(qr.matrix.height): # each scanline in the height |
| 31 | + for i in range(len(line)): # initialize it to be empty |
| 32 | + line[i] = 0 |
| 33 | + for x in range(qr.matrix.width): |
| 34 | + if qr.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 | + bitmap._load_row(Y_OFFSET + y*BLOCK_SIZE+b, line) |
| 42 | + |
| 43 | + # display the bitmap using our palette |
| 44 | + splash = displayio.Group() |
| 45 | + face = displayio.Sprite(bitmap, pixel_shader=palette, position=(0,0)) |
| 46 | + splash.append(face) |
| 47 | + board.DISPLAY.show(splash) |
| 48 | + board.DISPLAY.wait_for_frame() |
| 49 | + |
| 50 | +qr = adafruit_miniqr.QRCode() |
| 51 | +qr.add_data(b'https://www.adafruit.com/circuitpython') |
| 52 | +qr.make() |
| 53 | +draw_QR(qr.matrix) |
| 54 | + |
| 55 | +# turn on backlight |
| 56 | +backlight.duty_cycle = 35000 |
| 57 | + |
| 58 | +while True: |
| 59 | + pass |
0 commit comments