|
| 1 | +# VC0706 image capture to internal storage demo. |
| 2 | +# You must wire up the VC0706 to the board's serial port, and enable writes |
| 3 | +# to the internal filesystem by following this page to edit boot.py: |
| 4 | +# https://learn.adafruit.com/cpu-temperature-logging-with-circuit-python/writing-to-the-filesystem |
| 5 | +import board |
| 6 | +import busio |
| 7 | +import digitalio |
| 8 | +import time |
| 9 | + |
| 10 | +import adafruit_vc0706 |
| 11 | + |
| 12 | + |
| 13 | +# Configuration: |
| 14 | +RX_PIN = board.RX # RX pin of board, connected to VC0706 TX |
| 15 | +TX_PIN = board.TX # TX pin of board, connected to VC0706 RX |
| 16 | +IMAGE_FILE = '/image.jpg' # Full path to file name to save captured image. |
| 17 | + # Will overwrite! |
| 18 | + |
| 19 | +# Setup SPI bus (hardware SPI). |
| 20 | +spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 21 | + |
| 22 | +# Setup VC0706. |
| 23 | +vc0706 = adafruit_vc0706.VC0706(RX_PIN, TX_PIN) |
| 24 | + |
| 25 | +# Print the version string from the camera. |
| 26 | +print('VC0706 version:') |
| 27 | +print(vc0706.version) |
| 28 | + |
| 29 | +# Set the image size. |
| 30 | +vc0706.image_size = adafruit_vc0706.VC0706_640x480 # Or set VC0706_320x240 or |
| 31 | + # VC0706_160x120 |
| 32 | +# Note you can also read the property and compare against those values to |
| 33 | +# see the current size: |
| 34 | +size = vc0706.image_size |
| 35 | +if size == adafruit_vc0706.VC0706_640x480: |
| 36 | + print('Using 640x480 size image.') |
| 37 | +elif size == adafruit_vc0706.VC0706_320x240: |
| 38 | + print('Using 320x240 size image.') |
| 39 | +elif size == adafruit_vc0706.VC0706_160x120: |
| 40 | + print('Using 160x120 size image.') |
| 41 | + |
| 42 | +# Take a picture. |
| 43 | +print('Taking a picture in 3 seconds...') |
| 44 | +time.sleep(3) |
| 45 | +print('SNAP!') |
| 46 | +if not vc0706.take_picture(): |
| 47 | + raise RuntimeError('Failed to take picture!') |
| 48 | + |
| 49 | +# Print size of picture in bytes. |
| 50 | +frame_length = vc0706.frame_length |
| 51 | +print('Picture size (bytes): {}'.format(frame_length)) |
| 52 | + |
| 53 | +# Open a file for writing (overwriting it if necessary). |
| 54 | +# This will write 50 bytes at a time using a small buffer. |
| 55 | +# You MUST keep the buffer size under 100! |
| 56 | +print('Writing image: {}'.format(IMAGE_FILE), end='') |
| 57 | +with open(IMAGE_FILE, 'wb') as outfile: |
| 58 | + wcount = 0 |
| 59 | + while frame_length > 0: |
| 60 | + # Compute how much data is left to read as the lesser of remaining bytes |
| 61 | + # or the copy buffer size (32 bytes at a time). Buffer size MUST be |
| 62 | + # a multiple of 4 and under 100. Stick with 32! |
| 63 | + to_read = min(frame_length, 32) |
| 64 | + copy_buffer = bytearray(to_read) |
| 65 | + # Read picture data into the copy buffer. |
| 66 | + if vc0706.read_picture_into(copy_buffer) == 0: |
| 67 | + raise RuntimeError('Failed to read picture frame data!') |
| 68 | + # Write the data to SD card file and decrement remaining bytes. |
| 69 | + outfile.write(copy_buffer) |
| 70 | + frame_length -= 32 |
| 71 | + # Print a dot every 2k bytes to show progress. |
| 72 | + wcount += 1 |
| 73 | + if wcount >= 64: |
| 74 | + print('.', end='') |
| 75 | + wcount = 0 |
| 76 | +print() |
| 77 | +print('Finished!') |
0 commit comments