|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 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 | +This demo needs reserved psram properly configured in settings.toml: |
| 10 | +CIRCUITPY_RESERVED_PSRAM=1048576 |
| 11 | +
|
| 12 | +This example also requires an SD card breakout wired as follows: |
| 13 | + * IO18: SD Clock Input |
| 14 | + * IO17: SD Serial Output (MISO) |
| 15 | + * IO14: SD Serial Input (MOSI) |
| 16 | + * IO12: SD Chip Select |
| 17 | +
|
| 18 | +Insert a CircuitPython-compatible SD card before powering on the Kaluga. |
| 19 | +Press the "BOOT" button to take a photo in BMP format. |
| 20 | +""" |
| 21 | + |
| 22 | +import os |
| 23 | +import ssl |
| 24 | +import struct |
| 25 | + |
| 26 | +import board |
| 27 | +import busio |
| 28 | +import displayio |
| 29 | +import espcamera |
| 30 | +import espidf |
| 31 | +import keypad |
| 32 | +import sdcardio |
| 33 | +import socketpool |
| 34 | +import storage |
| 35 | + |
| 36 | +print("Initializing display") |
| 37 | +displayio.release_displays() |
| 38 | +spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) |
| 39 | +display_bus = displayio.FourWire( |
| 40 | + spi, |
| 41 | + command=board.LCD_D_C, |
| 42 | + chip_select=board.LCD_CS, |
| 43 | + reset=board.LCD_RST, |
| 44 | + baudrate=80_000_000, |
| 45 | +) |
| 46 | +_INIT_SEQUENCE = ( |
| 47 | + b"\x01\x80\x80" # Software reset then delay 0x80 (128ms) |
| 48 | + b"\xEF\x03\x03\x80\x02" |
| 49 | + b"\xCF\x03\x00\xC1\x30" |
| 50 | + b"\xED\x04\x64\x03\x12\x81" |
| 51 | + b"\xE8\x03\x85\x00\x78" |
| 52 | + b"\xCB\x05\x39\x2C\x00\x34\x02" |
| 53 | + b"\xF7\x01\x20" |
| 54 | + b"\xEA\x02\x00\x00" |
| 55 | + b"\xc0\x01\x23" # Power control VRH[5:0] |
| 56 | + b"\xc1\x01\x10" # Power control SAP[2:0];BT[3:0] |
| 57 | + b"\xc5\x02\x3e\x28" # VCM control |
| 58 | + b"\xc7\x01\x86" # VCM control2 |
| 59 | + b"\x36\x01\x40" # Memory Access Control |
| 60 | + b"\x37\x01\x00" # Vertical scroll zero |
| 61 | + b"\x3a\x01\x55" # COLMOD: Pixel Format Set |
| 62 | + b"\xb1\x02\x00\x18" # Frame Rate Control (In Normal Mode/Full Colors) |
| 63 | + b"\xb6\x03\x08\x82\x27" # Display Function Control |
| 64 | + b"\xF2\x01\x00" # 3Gamma Function Disable |
| 65 | + b"\x26\x01\x01" # Gamma curve selected |
| 66 | + b"\xe0\x0f\x0F\x31\x2B\x0C\x0E\x08\x4E\xF1\x37\x07\x10\x03\x0E\x09\x00" # Set Gamma |
| 67 | + b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F" # Set Gamma |
| 68 | + b"\x11\x80\x78" # Exit Sleep then delay 0x78 (120ms) |
| 69 | + b"\x29\x80\x78" # Display on then delay 0x78 (120ms) |
| 70 | +) |
| 71 | + |
| 72 | +display = displayio.Display(display_bus, _INIT_SEQUENCE, width=320, height=240) |
| 73 | + |
| 74 | +if espidf.get_reserved_psram() < 1047586: |
| 75 | + print("""Place the following line in CIRCUITPY/settings.toml, then hard-reset the board: |
| 76 | +``` |
| 77 | +CIRCUITPY_RESERVED_PSRAM=1048576 |
| 78 | +``` |
| 79 | +""") |
| 80 | + raise SystemExit |
| 81 | + |
| 82 | +print("Initializing SD card") |
| 83 | +sd_spi = busio.SPI(clock=board.IO18, MOSI=board.IO14, MISO=board.IO17) |
| 84 | +sd_cs = board.IO12 |
| 85 | +sdcard = sdcardio.SDCard(sd_spi, sd_cs) |
| 86 | +vfs = storage.VfsFat(sdcard) |
| 87 | +storage.mount(vfs, "/sd") |
| 88 | + |
| 89 | +print("Initializing camera") |
| 90 | +cam = espcamera.Camera( |
| 91 | + data_pins=board.CAMERA_DATA, |
| 92 | + external_clock_pin=board.CAMERA_XCLK, |
| 93 | + pixel_clock_pin=board.CAMERA_PCLK, |
| 94 | + vsync_pin=board.CAMERA_VSYNC, |
| 95 | + href_pin=board.CAMERA_HREF, |
| 96 | + pixel_format=espcamera.PixelFormat.RGB565, |
| 97 | + frame_size=espcamera.FrameSize.QVGA, |
| 98 | + i2c=board.I2C(), |
| 99 | + external_clock_frequency=20_000_000, |
| 100 | + framebuffer_count=1) |
| 101 | +print("initialized") |
| 102 | +display.auto_refresh = False |
| 103 | + |
| 104 | +def exists(filename): |
| 105 | + try: |
| 106 | + os.stat(filename) |
| 107 | + return True |
| 108 | + except OSError: |
| 109 | + return False |
| 110 | + |
| 111 | + |
| 112 | +_image_counter = 0 |
| 113 | + |
| 114 | + |
| 115 | +def open_next_image(extension="jpg"): |
| 116 | + global _image_counter # pylint: disable=global-statement |
| 117 | + while True: |
| 118 | + filename = f"/sd/img{_image_counter:04d}.{extension}" |
| 119 | + _image_counter += 1 |
| 120 | + if exists(filename): |
| 121 | + continue |
| 122 | + print("#", filename) |
| 123 | + return open(filename, "wb") # pylint: disable=consider-using-with |
| 124 | + |
| 125 | +ow = (display.width - cam.width) // 2 |
| 126 | +oh = (display.height - cam.height) // 2 |
| 127 | + |
| 128 | +k = keypad.Keys([board.IO0], value_when_pressed=False) |
| 129 | + |
| 130 | +while True: |
| 131 | + frame = cam.take(1) |
| 132 | + display_bus.send(42, struct.pack(">hh", ow, cam.width + ow - 1)) |
| 133 | + display_bus.send(43, struct.pack(">hh", oh, cam.height + ow - 1)) |
| 134 | + display_bus.send(44, frame) |
| 135 | + if (e := k.events.get()) is not None and e.pressed: |
| 136 | + cam.reconfigure( |
| 137 | + pixel_format=espcamera.PixelFormat.JPEG, |
| 138 | + frame_size=espcamera.FrameSize.SVGA, |
| 139 | + ) |
| 140 | + frame = cam.take(1) |
| 141 | + if isinstance(frame, memoryview): |
| 142 | + jpeg = frame |
| 143 | + print(f"Captured {len(jpeg)} bytes of jpeg data") |
| 144 | + |
| 145 | + with open_next_image() as f: |
| 146 | + f.write(jpeg) |
| 147 | + cam.reconfigure( |
| 148 | + pixel_format=espcamera.PixelFormat.RGB565, |
| 149 | + frame_size=espcamera.FrameSize.QVGA, |
| 150 | + ) |
| 151 | + |
0 commit comments