|
| 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. It requires CircuitPython 8. |
| 8 | +
|
| 9 | +To fix the MemoryError when creating a Camera object, Place the line |
| 10 | +```toml |
| 11 | +CIRCUITPY_RESERVED_PSRAM=1048576 |
| 12 | +``` |
| 13 | +in the file **CIRCUITPY/settings.toml** and restart. |
| 14 | +""" |
| 15 | + |
| 16 | +import struct |
| 17 | + |
| 18 | +import board |
| 19 | +import busio |
| 20 | +import keypad |
| 21 | +import displayio |
| 22 | +import espcamera |
| 23 | +import espidf |
| 24 | + |
| 25 | +print("Initializing display") |
| 26 | +displayio.release_displays() |
| 27 | +spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) |
| 28 | +display_bus = displayio.FourWire( |
| 29 | + spi, |
| 30 | + command=board.LCD_D_C, |
| 31 | + chip_select=board.LCD_CS, |
| 32 | + reset=board.LCD_RST, |
| 33 | + baudrate=80_000_000, |
| 34 | +) |
| 35 | +_INIT_SEQUENCE = ( |
| 36 | + b"\x01\x80\x80" # Software reset then delay 0x80 (128ms) |
| 37 | + b"\xEF\x03\x03\x80\x02" |
| 38 | + b"\xCF\x03\x00\xC1\x30" |
| 39 | + b"\xED\x04\x64\x03\x12\x81" |
| 40 | + b"\xE8\x03\x85\x00\x78" |
| 41 | + b"\xCB\x05\x39\x2C\x00\x34\x02" |
| 42 | + b"\xF7\x01\x20" |
| 43 | + b"\xEA\x02\x00\x00" |
| 44 | + b"\xc0\x01\x23" # Power control VRH[5:0] |
| 45 | + b"\xc1\x01\x10" # Power control SAP[2:0];BT[3:0] |
| 46 | + b"\xc5\x02\x3e\x28" # VCM control |
| 47 | + b"\xc7\x01\x86" # VCM control2 |
| 48 | + b"\x36\x01\x40" # Memory Access Control |
| 49 | + b"\x37\x01\x00" # Vertical scroll zero |
| 50 | + b"\x3a\x01\x55" # COLMOD: Pixel Format Set |
| 51 | + b"\xb1\x02\x00\x18" # Frame Rate Control (In Normal Mode/Full Colors) |
| 52 | + b"\xb6\x03\x08\x82\x27" # Display Function Control |
| 53 | + b"\xF2\x01\x00" # 3Gamma Function Disable |
| 54 | + b"\x26\x01\x01" # Gamma curve selected |
| 55 | + b"\xe0\x0f\x0F\x31\x2B\x0C\x0E\x08\x4E\xF1\x37\x07\x10\x03\x0E\x09\x00" # Set Gamma |
| 56 | + b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F" # Set Gamma |
| 57 | + b"\x11\x80\x78" # Exit Sleep then delay 0x78 (120ms) |
| 58 | + b"\x29\x80\x78" # Display on then delay 0x78 (120ms) |
| 59 | +) |
| 60 | + |
| 61 | +display = displayio.Display(display_bus, _INIT_SEQUENCE, width=320, height=240) |
| 62 | + |
| 63 | +if espidf.get_reserved_psram() < 1047586: |
| 64 | + print("""Place the following line in CIRCUITPY/settings.toml, then hard-reset the board: |
| 65 | +``` |
| 66 | +CIRCUITPY_RESERVED_PSRAM |
| 67 | +``` |
| 68 | +""") |
| 69 | + raise SystemExit |
| 70 | + |
| 71 | + |
| 72 | +print("Initializing camera") |
| 73 | +cam = espcamera.Camera( |
| 74 | + data_pins=board.CAMERA_DATA, |
| 75 | + external_clock_pin=board.CAMERA_XCLK, |
| 76 | + pixel_clock_pin=board.CAMERA_PCLK, |
| 77 | + vsync_pin=board.CAMERA_VSYNC, |
| 78 | + href_pin=board.CAMERA_HREF, |
| 79 | + pixel_format=espcamera.PixelFormat.RGB565, |
| 80 | + frame_size=espcamera.FrameSize.QVGA, |
| 81 | + i2c=board.I2C(), |
| 82 | + external_clock_frequency=20_000_000, |
| 83 | + framebuffer_count=2) |
| 84 | +print(cam.width, cam.height) |
| 85 | +display.auto_refresh = False |
| 86 | + |
| 87 | +k = keypad.Keys([board.IO0], value_when_pressed=False) |
| 88 | + |
| 89 | +ow = (display.width - cam.width) // 2 |
| 90 | +oh = (display.height - cam.height) // 2 |
| 91 | +display_bus.send(42, struct.pack(">hh", ow, cam.width + ow - 1)) |
| 92 | +display_bus.send(43, struct.pack(">hh", oh, cam.height + ow - 1)) |
| 93 | + |
| 94 | +while True: |
| 95 | + if (e := k.events.get()) is not None and e.pressed: |
| 96 | + cam.colorbar = not cam.colorbar |
| 97 | + |
| 98 | + frame = cam.take(1) |
| 99 | + display_bus.send(44, frame) |
0 commit comments