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
 17
 18# import digitalio # Uncomment if your board doesn't support sdcardio
 19import storage
 20
 21# import adafruit_sdcard # Uncomment if your board doesn't support sdcardio
 22import sdcardio  # Comment out if your board doesn't support sdcardio
 23import adafruit_vc0706
 24
 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(
 39    spi, SD_CS_PIN
 40)  # Comment out if your board doesn't support sdcardio
 41
 42vfs = storage.VfsFat(sdcard)
 43storage.mount(vfs, "/sd")
 44
 45# Create a serial connection for the VC0706 connection, speed is auto-detected.
 46uart = busio.UART(board.TX, board.RX)
 47# Setup VC0706 camera
 48vc0706 = adafruit_vc0706.VC0706(uart)
 49
 50# Print the version string from the camera.
 51print("VC0706 version:")
 52print(vc0706.version)
 53
 54# Set the baud rate to 115200 for fastest transfer (its the max speed)
 55vc0706.baudrate = 115200
 56
 57# Set the image size.
 58vc0706.image_size = adafruit_vc0706.IMAGE_SIZE_640x480  # Or set IMAGE_SIZE_320x240 or
 59# IMAGE_SIZE_160x120
 60# Note you can also read the property and compare against those values to
 61# see the current size:
 62size = vc0706.image_size
 63if size == adafruit_vc0706.IMAGE_SIZE_640x480:
 64    print("Using 640x480 size image.")
 65elif size == adafruit_vc0706.IMAGE_SIZE_320x240:
 66    print("Using 320x240 size image.")
 67elif size == adafruit_vc0706.IMAGE_SIZE_160x120:
 68    print("Using 160x120 size image.")
 69
 70# Take a picture.
 71print("Taking a picture in 3 seconds...")
 72time.sleep(3)
 73print("SNAP!")
 74if not vc0706.take_picture():
 75    raise RuntimeError("Failed to take picture!")
 76
 77# Print size of picture in bytes.
 78frame_length = vc0706.frame_length
 79print("Picture size (bytes): {}".format(frame_length))
 80
 81# Open a file for writing (overwriting it if necessary).
 82# This will write 50 bytes at a time using a small buffer.
 83# You MUST keep the buffer size under 100!
 84print("Writing image: {}".format(IMAGE_FILE), end="")
 85stamp = time.monotonic()
 86# pylint: disable=invalid-name
 87with open(IMAGE_FILE, "wb") as outfile:
 88    wcount = 0
 89    while frame_length > 0:
 90        # Compute how much data is left to read as the lesser of remaining bytes
 91        # or the copy buffer size (32 bytes at a time).  Buffer size MUST be
 92        # a multiple of 4 and under 100.  Stick with 32!
 93        to_read = min(frame_length, 32)
 94        copy_buffer = bytearray(to_read)
 95        # Read picture data into the copy buffer.
 96        if vc0706.read_picture_into(copy_buffer) == 0:
 97            raise RuntimeError("Failed to read picture frame data!")
 98        # Write the data to SD card file and decrement remaining bytes.
 99        outfile.write(copy_buffer)
100        frame_length -= 32
101        # Print a dot every 2k bytes to show progress.
102        wcount += 1
103        if wcount >= 64:
104            print(".", end="")
105            wcount = 0
106# pylint: enable=invalid-name
107print()
108print("Finished in %0.1f seconds!" % (time.monotonic() - stamp))
109# Turn the camera back into video mode.
110vc0706.resume_video()