Skip to content

Commit 211bac0

Browse files
authored
Merge pull request #5 from jepler/improve-examples
Improve examples
2 parents 90c3371 + 20a9fb5 commit 211bac0

File tree

4 files changed

+233
-10
lines changed

4 files changed

+233
-10
lines changed

adafruit_ov7670.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def test_pattern(self, pattern):
652652
ysc = self._read_register(_OV7670_REG_SCALING_YSC) & ~0x80
653653
if pattern & 1:
654654
xsc |= 0x80
655-
if pattern & 3:
655+
if pattern & 2:
656656
ysc |= 0x80
657657
# Write modified result back to SCALING_XSC and SCALING_YSC
658658
self._write_register(_OV7670_REG_SCALING_XSC, xsc)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
import time
7+
import board
8+
import busio
9+
import digitalio
10+
import displayio
11+
from adafruit_seesaw.tftshield18 import TFTShield18
12+
from adafruit_st7735r import ST7735R
13+
from adafruit_ov7670 import ( # pylint: disable=unused-import
14+
OV7670,
15+
OV7670_TEST_PATTERN_COLOR_BAR,
16+
OV7670_SIZE_DIV4,
17+
OV7670_SIZE_DIV8,
18+
OV7670_NIGHT_MODE_2,
19+
)
20+
21+
# Pylint is unable to see that the "size" property of OV7670_GrandCentral exists
22+
# pylint: disable=attribute-defined-outside-init
23+
24+
# Release any resources currently in use for the displays
25+
displayio.release_displays()
26+
27+
ss = TFTShield18()
28+
29+
spi = board.SPI()
30+
tft_cs = board.D10
31+
tft_dc = board.D8
32+
33+
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
34+
35+
ss.tft_reset()
36+
display = ST7735R(
37+
display_bus, width=160, height=128, rotation=90, bgr=True, auto_refresh=False
38+
)
39+
40+
ss.set_backlight(True)
41+
42+
43+
class OV7670_GrandCentral(OV7670):
44+
def __init__(self):
45+
with digitalio.DigitalInOut(board.D39) as shutdown:
46+
shutdown.switch_to_output(True)
47+
time.sleep(0.001)
48+
bus = busio.I2C(board.D24, board.D25)
49+
self._bus = bus
50+
OV7670.__init__(
51+
self,
52+
bus,
53+
mclk=board.PCC_XCLK,
54+
data0=board.PCC_D0,
55+
clock=board.PCC_CLK,
56+
vsync=board.PCC_DEN1,
57+
href=board.PCC_DEN2,
58+
shutdown=board.D39,
59+
reset=board.D38,
60+
)
61+
62+
def deinit(self):
63+
self._bus.deinit()
64+
OV7670.deinit(self)
65+
66+
67+
cam = OV7670_GrandCentral()
68+
69+
cam.size = OV7670_SIZE_DIV4
70+
cam.flip_x = False
71+
cam.flip_y = True
72+
pid = cam.product_id
73+
ver = cam.product_version
74+
print(f"Detected pid={pid:x} ver={ver:x}")
75+
# cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR
76+
77+
g = displayio.Group(scale=1)
78+
bitmap = displayio.Bitmap(160, 120, 65536)
79+
tg = displayio.TileGrid(
80+
bitmap,
81+
pixel_shader=displayio.ColorConverter(
82+
input_colorspace=displayio.Colorspace.RGB565_SWAPPED
83+
),
84+
)
85+
g.append(tg)
86+
display.show(g)
87+
buf = memoryview(bitmap)
88+
89+
t0 = time.monotonic_ns()
90+
display.auto_refresh = False
91+
while True:
92+
cam.capture(bitmap)
93+
bitmap.dirty()
94+
display.refresh(minimum_frames_per_second=0)
95+
t1 = time.monotonic_ns()
96+
print("fps", 1e9 / (t1 - t0))
97+
t0 = t1
98+
99+
cam.deinit()
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
"""
7+
Capture an image from the camera and display it on a supported LCD.
8+
"""
9+
10+
import time
11+
from adafruit_ov7670 import (
12+
OV7670,
13+
OV7670_SIZE_DIV1,
14+
OV7670_SIZE_DIV16,
15+
OV7670_TEST_PATTERN_COLOR_BAR,
16+
OV7670_TEST_PATTERN_SHIFTING_1,
17+
OV7670_TEST_PATTERN_COLOR_BAR_FADE,
18+
)
19+
from displayio import (
20+
Bitmap,
21+
Group,
22+
TileGrid,
23+
FourWire,
24+
release_displays,
25+
ColorConverter,
26+
Colorspace,
27+
)
28+
from adafruit_st7789 import ST7789
29+
import board
30+
import busio
31+
import digitalio
32+
33+
# Set up the display (You must customize this block for your display!)
34+
release_displays()
35+
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3)
36+
display_bus = FourWire(spi, command=board.GP0, chip_select=board.GP1, reset=None)
37+
display = ST7789(display_bus, width=320, height=240, rotation=270)
38+
39+
40+
# Ensure the camera is shut down, so that it releases the SDA/SCL lines,
41+
# then create the configuration I2C bus
42+
43+
with digitalio.DigitalInOut(board.GP10) as reset:
44+
reset.switch_to_output(False)
45+
time.sleep(0.001)
46+
bus = busio.I2C(board.GP9, board.GP8)
47+
48+
# Set up the camera (you must customize this for your board!)
49+
cam = OV7670(
50+
bus,
51+
data0=board.GP12, # [16] [org]
52+
clock=board.GP11, # [15] [blk]
53+
vsync=board.GP7, # [10] [brn]
54+
href=board.GP21, # [27/o14] [red]
55+
mclk=board.GP20, # [16/o15]
56+
shutdown=None,
57+
reset=board.GP10,
58+
) # [14]
59+
60+
width = display.width
61+
height = display.height
62+
63+
# cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR
64+
65+
bitmap = None
66+
# Select the biggest size for which we can allocate a bitmap successfully, and
67+
# which is not bigger than the display
68+
for size in range(OV7670_SIZE_DIV1, OV7670_SIZE_DIV16 + 1):
69+
cam.size = size
70+
if cam.width > width:
71+
continue
72+
if cam.height > height:
73+
continue
74+
try:
75+
bitmap = Bitmap(cam.width, cam.height, 65535)
76+
break
77+
except MemoryError:
78+
continue
79+
80+
print(width, height, cam.width, cam.height)
81+
if bitmap is None:
82+
raise SystemExit("Could not allocate a bitmap")
83+
84+
g = Group(scale=1, x=(width - cam.width) // 2, y=(height - cam.height) // 2)
85+
tg = TileGrid(
86+
bitmap, pixel_shader=ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)
87+
)
88+
g.append(tg)
89+
display.show(g)
90+
91+
t0 = time.monotonic_ns()
92+
display.auto_refresh = False
93+
while True:
94+
cam.capture(bitmap)
95+
bitmap.dirty()
96+
display.refresh(minimum_frames_per_second=0)
97+
t1 = time.monotonic_ns()
98+
print("fps", 1e9 / (t1 - t0))
99+
t0 = t1

examples/ov7670_simpletest.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,29 @@
66
"""Capture an image from the camera and display it as ASCII art.
77
88
The camera is placed in YUV mode, so the top 8 bits of each color
9-
value can be treated as "greyscale"."""
9+
value can be treated as "greyscale".
1010
11+
It's important that you use a terminal program that can interpret
12+
"ANSI" escape sequences. The demo uses them to "paint" each frame
13+
on top of the prevous one, rather than scrolling.
14+
15+
Remember to take the lens cap off, or un-comment the line setting
16+
the test pattern!
17+
"""
18+
19+
import sys
1120
import time
1221

1322
import digitalio
1423
import busio
1524
import board
1625

17-
from adafruit_ov7670 import OV7670, OV7670_SIZE_DIV16, OV7670_COLOR_YUV
26+
from 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+
)
1832

1933
# Ensure the camera is shut down, so that it releases the SDA/SCL lines,
2034
# then create the configuration I2C bus
@@ -36,14 +50,25 @@
3650
)
3751
cam.size = OV7670_SIZE_DIV16
3852
cam.colorspace = OV7670_COLOR_YUV
53+
cam.flip_y = True
54+
# cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR_FADE
3955

4056
buf = bytearray(2 * cam.width * cam.height)
41-
chars = " .:-=+*#%@"
57+
chars = b" .:-=+*#%@"
4258

43-
cam.capture(buf)
4459
width = cam.width
45-
for j in range(cam.height):
46-
for i in range(cam.width):
47-
b = buf[2 * (width * j + i)] * (len(chars) - 1) // 255
48-
print(end=chars[b] * 2)
49-
print()
60+
row = bytearray(2 * width)
61+
62+
sys.stdout.write("\033[2J")
63+
while 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)

0 commit comments

Comments
 (0)