Simple test

Ensure your device works with this simple test.

examples/vc0706_snapshot_filesystem.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""VC0706 image capture to local storage.
 5You must wire up the VC0706 to a USB or hardware serial port.
 6Primarily for use with Linux/Raspberry Pi but also can work with Mac/Windows"""
 7
 8import time
 9
10import board
11import busio
12
13import adafruit_vc0706
14
15# Set this to the full path to the file name to save the captured image. WILL OVERWRITE!
16# CircuitPython internal filesystem configuration:
17IMAGE_FILE = "/image.jpg"
18# USB to serial adapter configuration:
19# IMAGE_FILE = 'image.jpg'  # Full path to file name to save captured image. Will overwrite!
20# Raspberry Pi configuration:
21# IMAGE_FILE = '/home/pi/image.jpg'  # Full path to file name to save image. Will overwrite!
22
23
24# Create a serial connection for the VC0706 connection.
25uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=0.25)
26# Update the serial port name to match the serial connection for the camera!
27# For use with USB to serial adapter:
28# import serial
29# uart = serial.Serial("/dev/ttyUSB0", baudrate=115200, timeout=0.25)
30# For use with Raspberry Pi:
31# import serial
32# uart = serial.Serial("/dev/ttyS0", baudrate=115200, timeout=0.25)
33
34# Setup VC0706 camera
35vc0706 = adafruit_vc0706.VC0706(uart)
36
37# Print the version string from the camera.
38print("VC0706 version:")
39print(vc0706.version)
40
41# Set the image size.
42vc0706.image_size = adafruit_vc0706.IMAGE_SIZE_640x480
43# Or set IMAGE_SIZE_320x240 or IMAGE_SIZE_160x120
44
45# Note you can also read the property and compare against those values to
46# see the current size:
47size = vc0706.image_size
48if size == adafruit_vc0706.IMAGE_SIZE_640x480:
49    print("Using 640x480 size image.")
50elif size == adafruit_vc0706.IMAGE_SIZE_320x240:
51    print("Using 320x240 size image.")
52elif size == adafruit_vc0706.IMAGE_SIZE_160x120:
53    print("Using 160x120 size image.")
54
55# Take a picture.
56print("Taking a picture in 3 seconds...")
57time.sleep(3)
58print("SNAP!")
59if not vc0706.take_picture():
60    raise RuntimeError("Failed to take picture!")
61
62# Print size of picture in bytes.
63frame_length = vc0706.frame_length
64print(f"Picture size (bytes): {frame_length}")
65
66# Open a file for writing (overwriting it if necessary).
67# This will write 50 bytes at a time using a small buffer.
68# You MUST keep the buffer size under 100!
69print(f"Writing image: {IMAGE_FILE}", end="", flush=True)
70stamp = time.monotonic()
71with open(IMAGE_FILE, "wb") as outfile:
72    wcount = 0
73    while frame_length > 0:
74        t = time.monotonic()
75        # Compute how much data is left to read as the lesser of remaining bytes
76        # or the copy buffer size (32 bytes at a time).  Buffer size MUST be
77        # a multiple of 4 and under 100.  Stick with 32!
78        to_read = min(frame_length, 32)
79        copy_buffer = bytearray(to_read)
80        # Read picture data into the copy buffer.
81        if vc0706.read_picture_into(copy_buffer) == 0:
82            raise RuntimeError("Failed to read picture frame data!")
83        # Write the data to SD card file and decrement remaining bytes.
84        outfile.write(copy_buffer)
85        frame_length -= 32
86        # Print a dot every 2k bytes to show progress.
87        wcount += 1
88        if wcount >= 64:
89            print(".", end="", flush=True)
90            wcount = 0
91print()
92print("Finished in %0.1f seconds!" % (time.monotonic() - stamp))
93# Turn the camera back into video mode.
94vc0706.resume_video()
examples/vc0706_snapshot_simpletest.py
  1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  2# SPDX-License-Identifier: MIT
  3
  4"""VC0706 image capture to SD card demo.
  5You must wire up the VC0706 to the board's serial port, and a SD card holder
  6to the board's SPI bus.  Use the Feather M0 Adalogger as it includes a SD
  7card holder pre-wired to the board--this sketch is setup to use the Adalogger!
  8In addition you MUST also install the following dependent SD card library:
  9https://github.com/adafruit/Adafruit_CircuitPython_SD
 10See the guide here for more details on using SD cards with CircuitPython:
 11https://learn.adafruit.com/micropython-hardware-sd-cards"""
 12
 13import time
 14
 15import board
 16import busio
 17
 18# import adafruit_sdcard # Uncomment if your board doesn't support sdcardio
 19import sdcardio  # Comment out if your board doesn't support sdcardio
 20
 21# import digitalio # Uncomment if your board doesn't support sdcardio
 22import storage
 23
 24import adafruit_vc0706
 25
 26# Configuration:
 27SD_CS_PIN = board.D10  # CS for SD card (SD_CS is for Feather Adalogger)
 28IMAGE_FILE = "/sd/image.jpg"  # Full path to file name to save captured image.
 29# Will overwrite!
 30
 31# Setup SPI bus (hardware SPI).
 32spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
 33
 34# Setup SD card and mount it in the filesystem.
 35# Uncomment if your board doesn't support sdcardio
 36# sd_cs = digitalio.DigitalInOut(SD_CS_PIN)
 37# sdcard = adafruit_sdcard.SDCard(spi, sd_cs)
 38sdcard = sdcardio.SDCard(spi, SD_CS_PIN)  # Comment out if your board doesn't support sdcardio
 39
 40vfs = storage.VfsFat(sdcard)
 41storage.mount(vfs, "/sd")
 42
 43# Create a serial connection for the VC0706 connection, speed is auto-detected.
 44uart = busio.UART(board.TX, board.RX)
 45# Setup VC0706 camera
 46vc0706 = adafruit_vc0706.VC0706(uart)
 47
 48# Print the version string from the camera.
 49print("VC0706 version:")
 50print(vc0706.version)
 51
 52# Set the baud rate to 115200 for fastest transfer (its the max speed)
 53vc0706.baudrate = 115200
 54
 55# Set the image size.
 56vc0706.image_size = adafruit_vc0706.IMAGE_SIZE_640x480  # Or set IMAGE_SIZE_320x240 or
 57# IMAGE_SIZE_160x120
 58# Note you can also read the property and compare against those values to
 59# see the current size:
 60size = vc0706.image_size
 61if size == adafruit_vc0706.IMAGE_SIZE_640x480:
 62    print("Using 640x480 size image.")
 63elif size == adafruit_vc0706.IMAGE_SIZE_320x240:
 64    print("Using 320x240 size image.")
 65elif size == adafruit_vc0706.IMAGE_SIZE_160x120:
 66    print("Using 160x120 size image.")
 67
 68# Take a picture.
 69print("Taking a picture in 3 seconds...")
 70time.sleep(3)
 71print("SNAP!")
 72if not vc0706.take_picture():
 73    raise RuntimeError("Failed to take picture!")
 74
 75# Print size of picture in bytes.
 76frame_length = vc0706.frame_length
 77print(f"Picture size (bytes): {frame_length}")
 78
 79# Open a file for writing (overwriting it if necessary).
 80# This will write 50 bytes at a time using a small buffer.
 81# You MUST keep the buffer size under 100!
 82print(f"Writing image: {IMAGE_FILE}", end="")
 83stamp = time.monotonic()
 84with open(IMAGE_FILE, "wb") as outfile:
 85    wcount = 0
 86    while frame_length > 0:
 87        # Compute how much data is left to read as the lesser of remaining bytes
 88        # or the copy buffer size (32 bytes at a time).  Buffer size MUST be
 89        # a multiple of 4 and under 100.  Stick with 32!
 90        to_read = min(frame_length, 32)
 91        copy_buffer = bytearray(to_read)
 92        # Read picture data into the copy buffer.
 93        if vc0706.read_picture_into(copy_buffer) == 0:
 94            raise RuntimeError("Failed to read picture frame data!")
 95        # Write the data to SD card file and decrement remaining bytes.
 96        outfile.write(copy_buffer)
 97        frame_length -= 32
 98        # Print a dot every 2k bytes to show progress.
 99        wcount += 1
100        if wcount >= 64:
101            print(".", end="")
102            wcount = 0
103print()
104print("Finished in %0.1f seconds!" % (time.monotonic() - stamp))
105# Turn the camera back into video mode.
106vc0706.resume_video()