Simple test

Ensure your device works with this simple test.

examples/ov7670_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6"""Capture an image from the camera and display it as ASCII art.
 7
 8The camera is placed in YUV mode, so the top 8 bits of each color
 9value can be treated as "greyscale".
10
11It's important that you use a terminal program that can interpret
12"ANSI" escape sequences.  The demo uses them to "paint" each frame
13on top of the prevous one, rather than scrolling.
14
15Remember to take the lens cap off, or un-comment the line setting
16the test pattern!
17"""
18
19import sys
20import time
21
22import digitalio
23import busio
24import board
25
26from adafruit_ov7670 import (  # pylint: disable=unused-import
27    OV7670,
28    OV7670_SIZE_DIV16,
29    OV7670_COLOR_YUV,
30    OV7670_TEST_PATTERN_COLOR_BAR_FADE,
31)
32
33# Ensure the camera is shut down, so that it releases the SDA/SCL lines,
34# then create the configuration I2C bus
35
36with digitalio.DigitalInOut(board.D39) as shutdown:
37    shutdown.switch_to_output(True)
38    time.sleep(0.001)
39    bus = busio.I2C(board.D24, board.D25)
40
41cam = OV7670(
42    bus,
43    data0=board.PCC_D0,
44    clock=board.PCC_CLK,
45    vsync=board.PCC_DEN1,
46    href=board.PCC_DEN2,
47    mclk=board.D29,
48    shutdown=board.D39,
49    reset=board.D38,
50)
51cam.size = OV7670_SIZE_DIV16
52cam.colorspace = OV7670_COLOR_YUV
53cam.flip_y = True
54# cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR_FADE
55
56buf = bytearray(2 * cam.width * cam.height)
57chars = b" .:-=+*#%@"
58
59width = cam.width
60row = bytearray(2 * width)
61
62sys.stdout.write("\033[2J")
63while True:
64    cam.capture(buf)
65    for j in range(cam.height):
66        sys.stdout.write(f"\033[{j}H")
67        for i in range(cam.width):
68            row[i * 2] = row[i * 2 + 1] = chars[
69                buf[2 * (width * j + i)] * (len(chars) - 1) // 255
70            ]
71        sys.stdout.write(row)
72        sys.stdout.write("\033[K")
73    sys.stdout.write("\033[J")
74    time.sleep(0.05)