|
| 1 | +# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Unlicense |
| 5 | + |
| 6 | +""" |
| 7 | +Display an image on the LCD, then record an image when a button is pressed/held. |
| 8 | +
|
| 9 | +Uses the SD Card present on the 2inch st7789 breakout board |
| 10 | +
|
| 11 | +This example also requires an SD card breakout wired as follows: |
| 12 | +SPI BUS - same as the ST7789 -- with MISO connected |
| 13 | + * GP2: SD Clock Input |
| 14 | + * GP3: SD Serial Input (MOSI) |
| 15 | + * GP4: SD Serial Output (MISO) |
| 16 | + * GP5: SD Chip Select |
| 17 | +
|
| 18 | +A button is needed to trigger the capture |
| 19 | +Attach a button to GROUND and GP22 |
| 20 | +
|
| 21 | +Insert a CircuitPython-compatible SD card before powering on the Kaluga. |
| 22 | +Press the "Record" button on the audio daughterboard to take a photo. |
| 23 | +""" |
| 24 | + |
| 25 | +import time |
| 26 | +from displayio import ( |
| 27 | + Bitmap, |
| 28 | + Group, |
| 29 | + TileGrid, |
| 30 | + FourWire, |
| 31 | + release_displays, |
| 32 | + ColorConverter, |
| 33 | + Colorspace, |
| 34 | +) |
| 35 | + |
| 36 | +from adafruit_st7789 import ST7789 |
| 37 | +import board |
| 38 | +import busio |
| 39 | +import digitalio |
| 40 | +import adafruit_ov2640 |
| 41 | +import sdcardio |
| 42 | +import storage |
| 43 | +import os |
| 44 | + |
| 45 | +release_displays() |
| 46 | +# Set up the display (You must customize this block for your display!) |
| 47 | +spi = busio.SPI(clock=board.GP2, MOSI=board.GP3, MISO=board.GP4) |
| 48 | +# setup the SD Card |
| 49 | +sd_cs = board.GP5 |
| 50 | +sdcard = sdcardio.SDCard(spi, sd_cs) |
| 51 | +vfs = storage.VfsFat(sdcard) |
| 52 | +storage.mount(vfs, "/sd") |
| 53 | +# setup the button |
| 54 | +button = digitalio.DigitalInOut(board.GP22) |
| 55 | +button.pull = digitalio.Pull.UP |
| 56 | + |
| 57 | +display_bus = FourWire(spi, command=board.GP0, chip_select=board.GP1, reset=None) |
| 58 | +display = ST7789(display_bus, width=320, height=240, rotation=270) |
| 59 | +display.auto_refresh = False |
| 60 | + |
| 61 | +# Ensure the camera is shut down, so that it releases the SDA/SCL lines, |
| 62 | +# then create the configuration I2C bus |
| 63 | + |
| 64 | +with digitalio.DigitalInOut(board.GP10) as reset: |
| 65 | + reset.switch_to_output(False) |
| 66 | + time.sleep(0.001) |
| 67 | + bus = busio.I2C(board.GP9, board.GP8) |
| 68 | + |
| 69 | +# Set up the camera (you must customize this for your board!) |
| 70 | +cam = adafruit_ov2640.OV2640( |
| 71 | + bus, |
| 72 | + data_pins=[ |
| 73 | + board.GP12, |
| 74 | + board.GP13, |
| 75 | + board.GP14, |
| 76 | + board.GP15, |
| 77 | + board.GP16, |
| 78 | + board.GP17, |
| 79 | + board.GP18, |
| 80 | + board.GP19, |
| 81 | + ], # [16] [org] etc |
| 82 | + clock=board.GP11, # [15] [blk] |
| 83 | + vsync=board.GP7, # [10] [brn] |
| 84 | + href=board.GP21, # [27/o14] [red] |
| 85 | + mclk=board.GP20, # [16/o15] |
| 86 | + shutdown=None, |
| 87 | + reset=board.GP10, |
| 88 | +) # [14] |
| 89 | + |
| 90 | +width = display.width |
| 91 | +height = display.height |
| 92 | + |
| 93 | +cam.size = adafruit_ov2640.OV2640_SIZE_QQVGA |
| 94 | +# cam.test_pattern = True |
| 95 | +bitmap = Bitmap(cam.width, cam.height, 65536) |
| 96 | + |
| 97 | +print(width, height, cam.width, cam.height) |
| 98 | +if bitmap is None: |
| 99 | + raise SystemExit("Could not allocate a bitmap") |
| 100 | + |
| 101 | +g = Group(scale=1, x=(width - cam.width) // 2, y=(height - cam.height) // 2) |
| 102 | +tg = TileGrid( |
| 103 | + bitmap, pixel_shader=ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED) |
| 104 | +) |
| 105 | +g.append(tg) |
| 106 | +display.show(g) |
| 107 | + |
| 108 | +display.auto_refresh = False |
| 109 | + |
| 110 | + |
| 111 | +def exists(filename): |
| 112 | + try: |
| 113 | + os.stat(filename) |
| 114 | + return True |
| 115 | + except OSError as e: |
| 116 | + return False |
| 117 | + |
| 118 | + |
| 119 | +_image_counter = 0 |
| 120 | + |
| 121 | + |
| 122 | +def open_next_image(): |
| 123 | + global _image_counter |
| 124 | + while True: |
| 125 | + filename = f"/sd/img{_image_counter:04d}.jpg" |
| 126 | + _image_counter += 1 |
| 127 | + if exists(filename): |
| 128 | + continue |
| 129 | + print("#", filename) |
| 130 | + return open(filename, "wb") |
| 131 | + |
| 132 | + |
| 133 | +def capture_image(): |
| 134 | + old_size = cam.size |
| 135 | + old_colorspace = cam.colorspace |
| 136 | + |
| 137 | + try: |
| 138 | + cam.size = adafruit_ov2640.OV2640_SIZE_QVGA |
| 139 | + cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG |
| 140 | + b = bytearray(cam.capture_buffer_size) |
| 141 | + jpeg = cam.capture(b) |
| 142 | + |
| 143 | + print(f"Captured {len(jpeg)} bytes of jpeg data") |
| 144 | + with open_next_image() as f: |
| 145 | + f.write(jpeg) |
| 146 | + finally: |
| 147 | + cam.size = old_size |
| 148 | + cam.colorspace = old_colorspace |
| 149 | + |
| 150 | + |
| 151 | +def main(): |
| 152 | + display.auto_refresh = False |
| 153 | + while True: |
| 154 | + if not button.value: # button pressed |
| 155 | + capture_image() |
| 156 | + cam.capture(bitmap) |
| 157 | + bitmap.dirty() |
| 158 | + display.refresh(minimum_frames_per_second=0) |
| 159 | + |
| 160 | + |
| 161 | +main() |
0 commit comments