|
| 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. |
| 7 | +
|
| 8 | +To fix the MemoryError when creating a Camera object, Place the line |
| 9 | +```toml |
| 10 | +CIRCUITPY_RESERVED_PSRAM=1048576 |
| 11 | +``` |
| 12 | +in the file **CIRCUITPY/settings.toml** and restart. |
| 13 | +""" |
| 14 | + |
| 15 | +import sys |
| 16 | + |
| 17 | +import board |
| 18 | +import keypad |
| 19 | +import displayio |
| 20 | +import espcamera |
| 21 | +import espidf |
| 22 | + |
| 23 | +# The demo runs very slowly if the LCD display is enabled! |
| 24 | +# It's intended to be viewed on the REPL on a host computer |
| 25 | +displayio.release_displays() |
| 26 | + |
| 27 | +if espidf.get_reserved_psram() < 1047586: |
| 28 | + print("""Place the following line in CIRCUITPY/settings.toml, then hard-reset the board: |
| 29 | +``` |
| 30 | +CIRCUITPY_RESERVED_PSRAM |
| 31 | +``` |
| 32 | +""") |
| 33 | + raise SystemExit |
| 34 | + |
| 35 | +print("Initializing camera") |
| 36 | +cam = espcamera.Camera( |
| 37 | + data_pins=board.CAMERA_DATA, |
| 38 | + external_clock_pin=board.CAMERA_XCLK, |
| 39 | + pixel_clock_pin=board.CAMERA_PCLK, |
| 40 | + vsync_pin=board.CAMERA_VSYNC, |
| 41 | + href_pin=board.CAMERA_HREF, |
| 42 | + pixel_format=espcamera.PixelFormat.GRAYSCALE, |
| 43 | + frame_size=espcamera.FrameSize.QQVGA, |
| 44 | + i2c=board.I2C(), |
| 45 | + external_clock_frequency=20_000_000, |
| 46 | + framebuffer_count=2) |
| 47 | +print("initialized") |
| 48 | + |
| 49 | +k = keypad.Keys([board.IO0], value_when_pressed=False) |
| 50 | + |
| 51 | +chars = b" .:-=+*#%@" |
| 52 | +remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)] |
| 53 | +width = cam.width |
| 54 | +row = bytearray(width//2) |
| 55 | + |
| 56 | +sys.stdout.write("\033[2J") |
| 57 | + |
| 58 | +while True: |
| 59 | + if (e := k.events.get()) is not None and e.pressed: |
| 60 | + cam.colorbar = not cam.colorbar |
| 61 | + |
| 62 | + frame = cam.take(1) |
| 63 | + |
| 64 | + for j in range(0, cam.height, 5): |
| 65 | + sys.stdout.write(f"\033[{j//5}H") |
| 66 | + for i in range(cam.width // 2): |
| 67 | + row[i] = remap[frame[width * j + 2 * i]] |
| 68 | + sys.stdout.write(row) |
| 69 | + sys.stdout.write("\033[K") |
| 70 | + sys.stdout.write("\033[J") |
0 commit comments