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
 9import busio
10import board
11import adafruit_vc0706
12
13# Set this to the full path to the file name to save the captured image. WILL OVERWRITE!
14# CircuitPython internal filesystem configuration:
15IMAGE_FILE = "/image.jpg"
16# USB to serial adapter configuration:
17# IMAGE_FILE = 'image.jpg'  # Full path to file name to save captured image. Will overwrite!
18# Raspberry Pi configuration:
19# IMAGE_FILE = '/home/pi/image.jpg'  # Full path to file name to save image. Will overwrite!
20
21
22# Create a serial connection for the VC0706 connection.
23uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=0.25)
24# Update the serial port name to match the serial connection for the camera!
25# For use with USB to serial adapter:
26# import serial
27# uart = serial.Serial("/dev/ttyUSB0", baudrate=115200, timeout=0.25)
28# For use with Raspberry Pi:
29# import serial
30# uart = serial.Serial("/dev/ttyS0", baudrate=115200, timeout=0.25)
31
32# Setup VC0706 camera
33vc0706 = adafruit_vc0706.VC0706(uart)
34
35# Print the version string from the camera.
36print("VC0706 version:")
37print(vc0706.version)
38
39# Set the image size.
40vc0706.image_size = adafruit_vc0706.IMAGE_SIZE_640x480
41# Or set IMAGE_SIZE_320x240 or IMAGE_SIZE_160x120
42
43# Note you can also read the property and compare against those values to
44# see the current size:
45size = vc0706.image_size
46if size == adafruit_vc0706.IMAGE_SIZE_640x480:
47    print("Using 640x480 size image.")
48elif size == adafruit_vc0706.IMAGE_SIZE_320x240:
49    print("Using 320x240 size image.")
50elif size == adafruit_vc0706.IMAGE_SIZE_160x120:
51    print("Using 160x120 size image.")
52
53# Take a picture.
54print("Taking a picture in 3 seconds...")
55time.sleep(3)
56print("SNAP!")
57if not vc0706.take_picture():
58    raise RuntimeError("Failed to take picture!")
59
60# Print size of picture in bytes.
61frame_length = vc0706.frame_length
62print("Picture size (bytes): {}".format(frame_length))
63
64# Open a file for writing (overwriting it if necessary).
65# This will write 50 bytes at a time using a small buffer.
66# You MUST keep the buffer size under 100!
67print("Writing image: {}".format(IMAGE_FILE), end="", flush=True)
68stamp = time.monotonic()
69# Pylint doesn't like the wcount variable being lowercase, but uppercase makes less sense
70# pylint: disable=invalid-name
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()
92# pylint: enable=invalid-name
93print("Finished in %0.1f seconds!" % (time.monotonic() - stamp))
94# Turn the camera back into video mode.
95vc0706.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
 17import digitalio
 18import storage
 19
 20import adafruit_sdcard
 21import adafruit_vc0706
 22
 23
 24# Configuration:
 25SD_CS_PIN = board.D10  # CS for SD card (SD_CS is for Feather Adalogger)
 26IMAGE_FILE = "/sd/image.jpg"  # Full path to file name to save captured image.
 27# Will overwrite!
 28
 29# Setup SPI bus (hardware SPI).
 30spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
 31
 32# Setup SD card and mount it in the filesystem.
 33sd_cs = digitalio.DigitalInOut(SD_CS_PIN)
 34sdcard = adafruit_sdcard.SDCard(spi, sd_cs)
 35vfs = storage.VfsFat(sdcard)
 36storage.mount(vfs, "/sd")
 37
 38# Create a serial connection for the VC0706 connection, speed is auto-detected.
 39uart = busio.UART(board.TX, board.RX)
 40# Setup VC0706 camera
 41vc0706 = adafruit_vc0706.VC0706(uart)
 42
 43# Print the version string from the camera.
 44print("VC0706 version:")
 45print(vc0706.version)
 46
 47# Set the baud rate to 115200 for fastest transfer (its the max speed)
 48vc0706.baudrate = 115200
 49
 50# Set the image size.
 51vc0706.image_size = adafruit_vc0706.IMAGE_SIZE_640x480  # Or set IMAGE_SIZE_320x240 or
 52# IMAGE_SIZE_160x120
 53# Note you can also read the property and compare against those values to
 54# see the current size:
 55size = vc0706.image_size
 56if size == adafruit_vc0706.IMAGE_SIZE_640x480:
 57    print("Using 640x480 size image.")
 58elif size == adafruit_vc0706.IMAGE_SIZE_320x240:
 59    print("Using 320x240 size image.")
 60elif size == adafruit_vc0706.IMAGE_SIZE_160x120:
 61    print("Using 160x120 size image.")
 62
 63# Take a picture.
 64print("Taking a picture in 3 seconds...")
 65time.sleep(3)
 66print("SNAP!")
 67if not vc0706.take_picture():
 68    raise RuntimeError("Failed to take picture!")
 69
 70# Print size of picture in bytes.
 71frame_length = vc0706.frame_length
 72print("Picture size (bytes): {}".format(frame_length))
 73
 74# Open a file for writing (overwriting it if necessary).
 75# This will write 50 bytes at a time using a small buffer.
 76# You MUST keep the buffer size under 100!
 77print("Writing image: {}".format(IMAGE_FILE), end="")
 78stamp = time.monotonic()
 79# pylint: disable=invalid-name
 80with open(IMAGE_FILE, "wb") as outfile:
 81    wcount = 0
 82    while frame_length > 0:
 83        # Compute how much data is left to read as the lesser of remaining bytes
 84        # or the copy buffer size (32 bytes at a time).  Buffer size MUST be
 85        # a multiple of 4 and under 100.  Stick with 32!
 86        to_read = min(frame_length, 32)
 87        copy_buffer = bytearray(to_read)
 88        # Read picture data into the copy buffer.
 89        if vc0706.read_picture_into(copy_buffer) == 0:
 90            raise RuntimeError("Failed to read picture frame data!")
 91        # Write the data to SD card file and decrement remaining bytes.
 92        outfile.write(copy_buffer)
 93        frame_length -= 32
 94        # Print a dot every 2k bytes to show progress.
 95        wcount += 1
 96        if wcount >= 64:
 97            print(".", end="")
 98            wcount = 0
 99# pylint: enable=invalid-name
100print()
101print("Finished in %0.1f seconds!" % (time.monotonic() - stamp))
102# Turn the camera back into video mode.
103vc0706.resume_video()