|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 Limor Fried for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | +""" |
| 5 | +This demo is designed for the Raspberry Pi Pico and Camera PiCowbell |
| 6 | +When the shutter is pressed the camera is autofocussed before capturing |
| 7 | +an image andsaving it to the microSD card. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import time |
| 12 | +import busio |
| 13 | +import board |
| 14 | +import digitalio |
| 15 | +import keypad |
| 16 | +import sdcardio |
| 17 | +import storage |
| 18 | +import adafruit_ov5640 |
| 19 | + |
| 20 | +print("Initializing SD card") |
| 21 | +sd_spi = busio.SPI(clock=board.GP18, MOSI=board.GP19, MISO=board.GP16) |
| 22 | +sd_cs = board.GP17 |
| 23 | +sdcard = sdcardio.SDCard(sd_spi, sd_cs) |
| 24 | +vfs = storage.VfsFat(sdcard) |
| 25 | +storage.mount(vfs, "/sd") |
| 26 | + |
| 27 | +print("construct bus") |
| 28 | +i2c = busio.I2C(board.GP5, board.GP4) |
| 29 | +print("construct camera") |
| 30 | +reset = digitalio.DigitalInOut(board.GP14) |
| 31 | +cam = adafruit_ov5640.OV5640( |
| 32 | + i2c, |
| 33 | + data_pins=( |
| 34 | + board.GP6, |
| 35 | + board.GP7, |
| 36 | + board.GP8, |
| 37 | + board.GP9, |
| 38 | + board.GP10, |
| 39 | + board.GP11, |
| 40 | + board.GP12, |
| 41 | + board.GP13, |
| 42 | + ), |
| 43 | + clock=board.GP3, |
| 44 | + vsync=board.GP0, |
| 45 | + href=board.GP2, |
| 46 | + mclk=None, |
| 47 | + shutdown=None, |
| 48 | + reset=reset, |
| 49 | + size=adafruit_ov5640.OV5640_SIZE_VGA, |
| 50 | +) |
| 51 | +print("print chip id") |
| 52 | +print(cam.chip_id) |
| 53 | + |
| 54 | +keys = keypad.Keys((board.GP22,), value_when_pressed=False, pull=True) |
| 55 | + |
| 56 | + |
| 57 | +def exists(filename): |
| 58 | + try: |
| 59 | + os.stat(filename) |
| 60 | + return True |
| 61 | + except OSError as _: |
| 62 | + return False |
| 63 | + |
| 64 | + |
| 65 | +_image_counter = 0 |
| 66 | + |
| 67 | + |
| 68 | +def open_next_image(): |
| 69 | + global _image_counter # pylint: disable=global-statement |
| 70 | + while True: |
| 71 | + filename = f"/sd/img{_image_counter:04d}.jpg" |
| 72 | + _image_counter += 1 |
| 73 | + if exists(filename): |
| 74 | + continue |
| 75 | + print("# writing to", filename) |
| 76 | + return open(filename, "wb") |
| 77 | + |
| 78 | + |
| 79 | +cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG |
| 80 | +cam.quality = 3 |
| 81 | +b = bytearray(cam.capture_buffer_size) |
| 82 | + |
| 83 | +cam.autofocus() |
| 84 | +print("AF Status: ", cam.autofocus_status, cam.autofocus_vcm_step) |
| 85 | + |
| 86 | +jpeg = cam.capture(b) |
| 87 | + |
| 88 | +while True: |
| 89 | + shutter = keys.events.get() |
| 90 | + # event will be None if nothing has happened. |
| 91 | + if shutter: |
| 92 | + if shutter.pressed: |
| 93 | + cam.autofocus() |
| 94 | + print("AF Status: ", cam.autofocus_status, cam.autofocus_vcm_step) |
| 95 | + time.sleep(0.01) |
| 96 | + jpeg = cam.capture(b) |
| 97 | + print(f"Captured {len(jpeg)} bytes of jpeg data") |
| 98 | + print(f" (had allocated {cam.capture_buffer_size} bytes") |
| 99 | + print(f"Resolution {cam.width}x{cam.height}") |
| 100 | + try: |
| 101 | + with open_next_image() as f: |
| 102 | + f.write(jpeg) |
| 103 | + print("# Wrote image") |
| 104 | + except OSError as e: |
| 105 | + print(e) |
0 commit comments