Skip to content

Commit c02956c

Browse files
committed
add another example for manual focus
1 parent a311de1 commit c02956c

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
This demo is designed for the Raspberry Pi Pico and Camera PiCowbell
6+
When the shutter is pressed, a prompt is given to enter a step
7+
for the focus. Enter a value 0-255 to set the focus or
8+
enter a value >255 to execute an autofocus.
9+
After the value is input, an image is captured and saved
10+
to the microSD card.
11+
"""
12+
13+
import os
14+
import time
15+
import busio
16+
import board
17+
import digitalio
18+
import keypad
19+
import sdcardio
20+
import storage
21+
import adafruit_ov5640
22+
23+
print("Initializing SD card")
24+
sd_spi = busio.SPI(clock=board.GP18, MOSI=board.GP19, MISO=board.GP16)
25+
sd_cs = board.GP17
26+
sdcard = sdcardio.SDCard(sd_spi, sd_cs)
27+
vfs = storage.VfsFat(sdcard)
28+
storage.mount(vfs, "/sd")
29+
30+
print("construct bus")
31+
i2c = busio.I2C(board.GP5, board.GP4)
32+
print("construct camera")
33+
reset = digitalio.DigitalInOut(board.GP14)
34+
cam = adafruit_ov5640.OV5640(
35+
i2c,
36+
data_pins=(
37+
board.GP6,
38+
board.GP7,
39+
board.GP8,
40+
board.GP9,
41+
board.GP10,
42+
board.GP11,
43+
board.GP12,
44+
board.GP13,
45+
),
46+
clock=board.GP3,
47+
vsync=board.GP0,
48+
href=board.GP2,
49+
mclk=None,
50+
shutdown=None,
51+
reset=reset,
52+
size=adafruit_ov5640.OV5640_SIZE_VGA,
53+
)
54+
print("print chip id")
55+
print(cam.chip_id)
56+
57+
keys = keypad.Keys((board.GP22,), value_when_pressed=False, pull=True)
58+
59+
60+
def exists(filename):
61+
try:
62+
os.stat(filename)
63+
return True
64+
except OSError as _:
65+
return False
66+
67+
68+
_image_counter = 0
69+
70+
71+
def open_next_image():
72+
global _image_counter # pylint: disable=global-statement
73+
while True:
74+
filename = f"/sd/img{_image_counter:04d}.jpg"
75+
_image_counter += 1
76+
if exists(filename):
77+
continue
78+
print("# writing to", filename)
79+
return open(filename, "wb")
80+
81+
82+
cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG
83+
cam.quality = 3
84+
b = bytearray(cam.capture_buffer_size)
85+
86+
cam.autofocus()
87+
print("AF Status: ", cam.autofocus_status, cam.autofocus_vcm_step)
88+
89+
jpeg = cam.capture(b)
90+
91+
while True:
92+
shutter = keys.events.get()
93+
# event will be None if nothing has happened.
94+
if shutter:
95+
if shutter.pressed:
96+
"""Captures an image and send it to Adafruit IO."""
97+
step = int(input("enter AF step:"))
98+
if step > 255:
99+
# Force autofocus and capture a JPEG image
100+
cam.autofocus()
101+
print("AF Status: ", cam.autofocus_status, cam.autofocus_vcm_step)
102+
else:
103+
cam.autofocus_vcm_step = step
104+
print("AF Status: ", cam.autofocus_status, cam.autofocus_vcm_step)
105+
time.sleep(0.01)
106+
jpeg = cam.capture(b)
107+
print(f"Captured {len(jpeg)} bytes of jpeg data")
108+
print(f" (had allocated {cam.capture_buffer_size} bytes")
109+
print(f"Resolution {cam.width}x{cam.height}")
110+
try:
111+
with open_next_image() as f:
112+
f.write(jpeg)
113+
print("# Wrote image")
114+
except OSError as e:
115+
print(e)

0 commit comments

Comments
 (0)