|
| 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. Your secrets.py must be populated with your wifi credentials |
| 8 | +and your Adafruit IO credentials. |
| 9 | +""" |
| 10 | + |
| 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 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 21 | +import usb_hid |
| 22 | +from adafruit_hid.keyboard import Keyboard |
| 23 | +from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS |
| 24 | +from adafruit_hid.keycode import Keycode |
| 25 | + |
| 26 | +print("Initializing display") |
| 27 | +displayio.release_displays() |
| 28 | +spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) |
| 29 | +display_bus = displayio.FourWire( |
| 30 | + spi, command=board.LCD_D_C, chip_select=board.LCD_CS, reset=board.LCD_RST |
| 31 | +) |
| 32 | +display = ILI9341(display_bus, width=320, height=240, rotation=90) |
| 33 | + |
| 34 | +print("Initializing camera") |
| 35 | +bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) |
| 36 | +cam = adafruit_ov2640.OV2640( |
| 37 | + bus, |
| 38 | + data_pins=board.CAMERA_DATA, |
| 39 | + clock=board.CAMERA_PCLK, |
| 40 | + vsync=board.CAMERA_VSYNC, |
| 41 | + href=board.CAMERA_HREF, |
| 42 | + mclk=board.CAMERA_XCLK, |
| 43 | + mclk_frequency=20_000_000, |
| 44 | + size=adafruit_ov2640.OV2640_SIZE_QQVGA, |
| 45 | +) |
| 46 | +cam.flip_x = False |
| 47 | +cam.flip_y = False |
| 48 | +cam.colorspace = adafruit_ov2640.OV2640_COLOR_YUV |
| 49 | + |
| 50 | +print("Initializing USB") |
| 51 | +keyboard = Keyboard(usb_hid.devices) |
| 52 | +keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) |
| 53 | + |
| 54 | +qrdecoder = qrio.QRDecoder(cam.width, cam.height) |
| 55 | +bitmap = displayio.Bitmap(cam.width, cam.height, 65536) |
| 56 | + |
| 57 | +# Create a greyscale palette |
| 58 | +pal = displayio.Palette(256) |
| 59 | +for i in range(256): |
| 60 | + pal[i] = 0x10101 * i |
| 61 | + |
| 62 | +label = Label( |
| 63 | + font=FONT, |
| 64 | + text="Scan QR Code...", |
| 65 | + color=0xFFFFFF, |
| 66 | + background_color=0x0, |
| 67 | + padding_top=2, |
| 68 | + padding_left=2, |
| 69 | + padding_right=2, |
| 70 | + padding_bottom=2, |
| 71 | + anchor_point=(0.5, 1.0), |
| 72 | + anchored_position=(160, 230), |
| 73 | +) |
| 74 | +# Show the camera image at 2x size |
| 75 | +g1 = displayio.Group(scale=2) |
| 76 | +view = np.frombuffer(bitmap, dtype=np.uint8) |
| 77 | +tg = displayio.TileGrid( |
| 78 | + bitmap, |
| 79 | + pixel_shader=pal, |
| 80 | +) |
| 81 | +tg.flip_y = True |
| 82 | +g1.append(tg) |
| 83 | +g = displayio.Group() |
| 84 | +g.append(g1) |
| 85 | +g.append(label) |
| 86 | +display.show(g) |
| 87 | +display.auto_refresh = False |
| 88 | + |
| 89 | +i = 0 |
| 90 | +spin = ".oOo" |
| 91 | +old_payload = None |
| 92 | +while True: |
| 93 | + cam.capture(bitmap) |
| 94 | + |
| 95 | + for row in qrdecoder.decode(bitmap, qrio.PixelPolicy.EVEN_BYTES): |
| 96 | + payload = row.payload |
| 97 | + try: |
| 98 | + payload = payload.decode("utf-8") |
| 99 | + except UnicodeError: |
| 100 | + payload = str(payload) |
| 101 | + if payload != old_payload: |
| 102 | + label.text = payload |
| 103 | + keyboard_layout.write(payload) |
| 104 | + old_payload = payload |
| 105 | + |
| 106 | + # Clear out the odd bytes, so that the bitmap displays as greyscale |
| 107 | + view[1::2] = 0 |
| 108 | + bitmap.dirty() |
| 109 | + display.refresh(minimum_frames_per_second=0) |
0 commit comments