|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | + |
| 5 | +""" |
| 6 | +This demo is designed for the Kaluga development kit version 1.3 with the |
| 7 | +ILI9341 display. |
| 8 | +""" |
| 9 | + |
| 10 | +import ssl |
| 11 | +from ulab import numpy as np |
| 12 | +from terminalio import FONT |
| 13 | +import board |
| 14 | +import busio |
| 15 | +import displayio |
| 16 | +import qrio |
| 17 | +import adafruit_ov2640 |
| 18 | +from adafruit_display_text.bitmap_label import Label |
| 19 | +from adafruit_ili9341 import ILI9341 |
| 20 | + |
| 21 | +print("Initializing display") |
| 22 | +displayio.release_displays() |
| 23 | +spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) |
| 24 | +display_bus = displayio.FourWire( |
| 25 | + spi, command=board.LCD_D_C, chip_select=board.LCD_CS, reset=board.LCD_RST |
| 26 | +) |
| 27 | +display = ILI9341(display_bus, width=320, height=240, rotation=90) |
| 28 | + |
| 29 | +print("Initializing camera") |
| 30 | +bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) |
| 31 | +cam = adafruit_ov2640.OV2640( |
| 32 | + bus, |
| 33 | + data_pins=board.CAMERA_DATA, |
| 34 | + clock=board.CAMERA_PCLK, |
| 35 | + vsync=board.CAMERA_VSYNC, |
| 36 | + href=board.CAMERA_HREF, |
| 37 | + mclk=board.CAMERA_XCLK, |
| 38 | + mclk_frequency=20_000_000, |
| 39 | + size=adafruit_ov2640.OV2640_SIZE_QQVGA, |
| 40 | +) |
| 41 | +cam.flip_x = False |
| 42 | +cam.flip_y = False |
| 43 | +cam.colorspace = adafruit_ov2640.OV2640_COLOR_YUV |
| 44 | + |
| 45 | +qrdecoder = qrio.QRDecoder(cam.width, cam.height) |
| 46 | +bitmap = displayio.Bitmap(cam.width, cam.height, 65536) |
| 47 | + |
| 48 | +# Create a greyscale palette |
| 49 | +pal = displayio.Palette(256) |
| 50 | +for i in range(256): |
| 51 | + pal[i] = 0x10101 * i |
| 52 | + |
| 53 | +label = Label( |
| 54 | + font=FONT, |
| 55 | + text="Scan QR Code...", |
| 56 | + color=0xFFFFFF, |
| 57 | + background_color=0x0, |
| 58 | + padding_top=2, |
| 59 | + padding_left=2, |
| 60 | + padding_right=2, |
| 61 | + padding_bottom=2, |
| 62 | + anchor_point=(0.5, 1.0), |
| 63 | + anchored_position=(160, 230), |
| 64 | +) |
| 65 | +# Show the camera image at 2x size |
| 66 | +g1 = displayio.Group(scale=2) |
| 67 | +view = np.frombuffer(bitmap, dtype=np.uint8) |
| 68 | +tg = displayio.TileGrid( |
| 69 | + bitmap, |
| 70 | + pixel_shader=pal, |
| 71 | +) |
| 72 | +tg.flip_y = True |
| 73 | +g1.append(tg) |
| 74 | +g = displayio.Group() |
| 75 | +g.append(g1) |
| 76 | +g.append(label) |
| 77 | +display.show(g) |
| 78 | +display.auto_refresh = False |
| 79 | + |
| 80 | +old_payload = None |
| 81 | +while True: |
| 82 | + cam.capture(bitmap) |
| 83 | + |
| 84 | + for row in qrdecoder.decode(bitmap, qrio.PixelPolicy.EVEN_BYTES): |
| 85 | + payload = row.payload |
| 86 | + try: |
| 87 | + payload = payload.decode("utf-8") |
| 88 | + except UnicodeError: |
| 89 | + payload = str(payload) |
| 90 | + print(payload) |
| 91 | + |
| 92 | + # Clear out the odd bytes, so that the bitmap displays as greyscale |
| 93 | + view[1::2] = 0 |
| 94 | + bitmap.dirty() |
| 95 | + display.refresh(minimum_frames_per_second=0) |
0 commit comments