Skip to content

Improve examples #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adafruit_ov7670.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def test_pattern(self, pattern):
ysc = self._read_register(_OV7670_REG_SCALING_YSC) & ~0x80
if pattern & 1:
xsc |= 0x80
if pattern & 3:
if pattern & 2:
ysc |= 0x80
# Write modified result back to SCALING_XSC and SCALING_YSC
self._write_register(_OV7670_REG_SCALING_XSC, xsc)
Expand Down
99 changes: 99 additions & 0 deletions examples/ov7670_displayio_gcm4_tftshield18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

import time
import board
import busio
import digitalio
import displayio
from adafruit_seesaw.tftshield18 import TFTShield18
from adafruit_st7735r import ST7735R
from adafruit_ov7670 import ( # pylint: disable=unused-import
OV7670,
OV7670_TEST_PATTERN_COLOR_BAR,
OV7670_SIZE_DIV4,
OV7670_SIZE_DIV8,
OV7670_NIGHT_MODE_2,
)

# Pylint is unable to see that the "size" property of OV7670_GrandCentral exists
# pylint: disable=attribute-defined-outside-init

# Release any resources currently in use for the displays
displayio.release_displays()

ss = TFTShield18()

spi = board.SPI()
tft_cs = board.D10
tft_dc = board.D8

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)

ss.tft_reset()
display = ST7735R(
display_bus, width=160, height=128, rotation=90, bgr=True, auto_refresh=False
)

ss.set_backlight(True)


class OV7670_GrandCentral(OV7670):
def __init__(self):
with digitalio.DigitalInOut(board.D39) as shutdown:
shutdown.switch_to_output(True)
time.sleep(0.001)
bus = busio.I2C(board.D24, board.D25)
self._bus = bus
OV7670.__init__(
self,
bus,
mclk=board.PCC_XCLK,
data0=board.PCC_D0,
clock=board.PCC_CLK,
vsync=board.PCC_DEN1,
href=board.PCC_DEN2,
shutdown=board.D39,
reset=board.D38,
)

def deinit(self):
self._bus.deinit()
OV7670.deinit(self)


cam = OV7670_GrandCentral()

cam.size = OV7670_SIZE_DIV4
cam.flip_x = False
cam.flip_y = True
pid = cam.product_id
ver = cam.product_version
print(f"Detected pid={pid:x} ver={ver:x}")
# cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR

g = displayio.Group(scale=1)
bitmap = displayio.Bitmap(160, 120, 65536)
tg = displayio.TileGrid(
bitmap,
pixel_shader=displayio.ColorConverter(
input_colorspace=displayio.Colorspace.RGB565_SWAPPED
),
)
g.append(tg)
display.show(g)
buf = memoryview(bitmap)

t0 = time.monotonic_ns()
display.auto_refresh = False
while True:
cam.capture(bitmap)
bitmap.dirty()
display.refresh(minimum_frames_per_second=0)
t1 = time.monotonic_ns()
print("fps", 1e9 / (t1 - t0))
t0 = t1

cam.deinit()
99 changes: 99 additions & 0 deletions examples/ov7670_displayio_pico_st7789_2in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

"""
Capture an image from the camera and display it on a supported LCD.
"""

import time
from adafruit_ov7670 import (
OV7670,
OV7670_SIZE_DIV1,
OV7670_SIZE_DIV16,
OV7670_TEST_PATTERN_COLOR_BAR,
OV7670_TEST_PATTERN_SHIFTING_1,
OV7670_TEST_PATTERN_COLOR_BAR_FADE,
)
from displayio import (
Bitmap,
Group,
TileGrid,
FourWire,
release_displays,
ColorConverter,
Colorspace,
)
from adafruit_st7789 import ST7789
import board
import busio
import digitalio

# Set up the display (You must customize this block for your display!)
release_displays()
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3)
display_bus = FourWire(spi, command=board.GP0, chip_select=board.GP1, reset=None)
display = ST7789(display_bus, width=320, height=240, rotation=270)


# Ensure the camera is shut down, so that it releases the SDA/SCL lines,
# then create the configuration I2C bus

with digitalio.DigitalInOut(board.GP10) as reset:
reset.switch_to_output(False)
time.sleep(0.001)
bus = busio.I2C(board.GP9, board.GP8)

# Set up the camera (you must customize this for your board!)
cam = OV7670(
bus,
data0=board.GP12, # [16] [org]
clock=board.GP11, # [15] [blk]
vsync=board.GP7, # [10] [brn]
href=board.GP21, # [27/o14] [red]
mclk=board.GP20, # [16/o15]
shutdown=None,
reset=board.GP10,
) # [14]

width = display.width
height = display.height

# cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR

bitmap = None
# Select the biggest size for which we can allocate a bitmap successfully, and
# which is not bigger than the display
for size in range(OV7670_SIZE_DIV1, OV7670_SIZE_DIV16 + 1):
cam.size = size
if cam.width > width:
continue
if cam.height > height:
continue
try:
bitmap = Bitmap(cam.width, cam.height, 65535)
break
except MemoryError:
continue

print(width, height, cam.width, cam.height)
if bitmap is None:
raise SystemExit("Could not allocate a bitmap")

g = Group(scale=1, x=(width - cam.width) // 2, y=(height - cam.height) // 2)
tg = TileGrid(
bitmap, pixel_shader=ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)
)
g.append(tg)
display.show(g)

t0 = time.monotonic_ns()
display.auto_refresh = False
while True:
cam.capture(bitmap)
bitmap.dirty()
display.refresh(minimum_frames_per_second=0)
t1 = time.monotonic_ns()
print("fps", 1e9 / (t1 - t0))
t0 = t1
43 changes: 34 additions & 9 deletions examples/ov7670_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@
"""Capture an image from the camera and display it as ASCII art.

The camera is placed in YUV mode, so the top 8 bits of each color
value can be treated as "greyscale"."""
value can be treated as "greyscale".

It's important that you use a terminal program that can interpret
"ANSI" escape sequences. The demo uses them to "paint" each frame
on top of the prevous one, rather than scrolling.

Remember to take the lens cap off, or un-comment the line setting
the test pattern!
"""

import sys
import time

import digitalio
import busio
import board

from adafruit_ov7670 import OV7670, OV7670_SIZE_DIV16, OV7670_COLOR_YUV
from adafruit_ov7670 import ( # pylint: disable=unused-import
OV7670,
OV7670_SIZE_DIV16,
OV7670_COLOR_YUV,
OV7670_TEST_PATTERN_COLOR_BAR_FADE,
)

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

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

cam.capture(buf)
width = cam.width
for j in range(cam.height):
for i in range(cam.width):
b = buf[2 * (width * j + i)] * (len(chars) - 1) // 255
print(end=chars[b] * 2)
print()
row = bytearray(2 * width)

sys.stdout.write("\033[2J")
while True:
cam.capture(buf)
for j in range(cam.height):
sys.stdout.write(f"\033[{j}H")
for i in range(cam.width):
row[i * 2] = row[i * 2 + 1] = chars[
buf[2 * (width * j + i)] * (len(chars) - 1) // 255
]
sys.stdout.write(row)
sys.stdout.write("\033[K")
sys.stdout.write("\033[J")
time.sleep(0.05)