Skip to content

Commit 5e82048

Browse files
committed
simpletest from ladyada
1 parent a8e59e9 commit 5e82048

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

examples/pico_ov5640_simpletest.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import sys
2+
import time
3+
import busio
4+
import board
5+
import digitalio
6+
import adafruit_ov5640
7+
8+
print("construct bus")
9+
bus = busio.I2C(board.GP9, board.GP8)
10+
print("construct camera")
11+
reset = digitalio.DigitalInOut(board.GP10)
12+
cam = adafruit_ov5640.OV5640(
13+
bus,
14+
data_pins=(board.GP12,board.GP13,board.GP14,board.GP15,board.GP16,board.GP17,board.GP18,board.GP19), # [16] [org]
15+
clock=board.GP11, # [15] [blk]
16+
vsync=board.GP7, # [10] [brn]
17+
href=board.GP21, # [27/o14] [red]
18+
mclk=board.GP20, # [16/o15]
19+
shutdown=None,
20+
reset=reset,
21+
size=adafruit_ov5640.OV5640_SIZE_QQVGA,
22+
)
23+
print("print chip id")
24+
print(cam.chip_id)
25+
26+
27+
cam.colorspace = adafruit_ov5640.OV5640_COLOR_YUV
28+
cam.flip_y = True
29+
cam.flip_x = True
30+
cam.test_pattern = False
31+
32+
buf = bytearray(cam.capture_buffer_size)
33+
chars = b" .':-+=*%$#"
34+
remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)]
35+
36+
width = cam.width
37+
row = bytearray(width)
38+
39+
print("capturing")
40+
cam.capture(buf)
41+
print("capture complete")
42+
43+
sys.stdout.write("\033[2J")
44+
while True:
45+
cam.capture(buf)
46+
for j in range(0, cam.height, 2):
47+
sys.stdout.write(f"\033[{j//2}H")
48+
for i in range(cam.width):
49+
row[i] = remap[buf[2 * (width * j + i)]]
50+
sys.stdout.write(row)
51+
sys.stdout.write("\033[K")
52+
sys.stdout.write("\033[J")
53+
time.sleep(0.1)
54+
55+
56+
import time
57+
from adafruit_ov7670 import (
58+
OV7670,
59+
OV7670_SIZE_DIV1,
60+
OV7670_SIZE_DIV16,
61+
OV7670_TEST_PATTERN_COLOR_BAR,
62+
OV7670_TEST_PATTERN_SHIFTING_1,
63+
OV7670_TEST_PATTERN_COLOR_BAR_FADE,
64+
)
65+
from displayio import (
66+
Bitmap,
67+
Group,
68+
TileGrid,
69+
FourWire,
70+
release_displays,
71+
ColorConverter,
72+
Colorspace,
73+
)
74+
from adafruit_st7789 import ST7789
75+
import board
76+
import busio
77+
import digitalio
78+
79+
# Set up the display (You must customize this block for your display!)
80+
release_displays()
81+
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3)
82+
display_bus = FourWire(spi, command=board.GP0, chip_select=board.GP1, reset=None)
83+
display = ST7789(display_bus, width=240, height=240, rowstart=80, rotation=270)
84+
85+
86+
# Ensure the camera is shut down, so that it releases the SDA/SCL lines,
87+
# then create the configuration I2C bus
88+
89+
with digitalio.DigitalInOut(board.GP10) as reset:
90+
reset.switch_to_output(False)
91+
time.sleep(0.001)
92+
bus = busio.I2C(board.GP9, board.GP8)
93+
94+
# Set up the camera (you must customize this for your board!)
95+
cam = OV7670(
96+
bus,
97+
data0=board.GP12, # [16] [org]
98+
clock=board.GP11, # [15] [blk]
99+
vsync=board.GP7, # [10] [brn]
100+
href=board.GP21, # [27/o14] [red]
101+
mclk=board.GP20, # [16/o15]
102+
shutdown=None,
103+
reset=board.GP10,
104+
) # [14]
105+
106+
width = display.width
107+
height = display.height
108+
109+
#cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR_FADE
110+
bitmap = None
111+
# Select the biggest size for which we can allocate a bitmap successfully, and
112+
# which is not bigger than the display
113+
for size in range(OV7670_SIZE_DIV1, OV7670_SIZE_DIV16 + 1):
114+
cam.size = size
115+
if cam.width > width:
116+
continue
117+
if cam.height > height:
118+
continue
119+
try:
120+
bitmap = Bitmap(cam.width, cam.height, 65535)
121+
break
122+
except MemoryError:
123+
continue
124+
125+
print(width, height, cam.width, cam.height)
126+
if bitmap is None:
127+
raise SystemExit("Could not allocate a bitmap")
128+
129+
g = Group(scale=1, x=(width-cam.width)//2, y=(height-cam.height)//2)
130+
tg = TileGrid(
131+
bitmap, pixel_shader=ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)
132+
)
133+
g.append(tg)
134+
display.show(g)
135+
136+
t0 = time.monotonic_ns()
137+
display.auto_refresh = False
138+
while True:
139+
cam.capture(bitmap)
140+
bitmap.dirty()
141+
display.refresh(minimum_frames_per_second=0)
142+
t1 = time.monotonic_ns()
143+
print("fps", 1e9 / (t1 - t0))
144+
t0 = t1

0 commit comments

Comments
 (0)