Skip to content

Commit 751422b

Browse files
committed
Update camera demos
1 parent e0a0722 commit 751422b

File tree

4 files changed

+221
-0
lines changed
  • OV5640_Breakout
    • CircuitPython_Kaluga-ascii-mirror
    • CircuitPython_Kaluga-lcd-mirror
    • CircuitPython_Pico-ascii-mirror
    • CircuitPython_Pico-lcd-mirror

4 files changed

+221
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
"""
6+
This demo is designed for the Kaluga development kit version 1.3.
7+
8+
To fix the MemoryError when creating a Camera object, Place the line
9+
```toml
10+
CIRCUITPY_RESERVED_PSRAM=1048576
11+
```
12+
in the file **CIRCUITPY/settings.toml** and restart.
13+
"""
14+
15+
import sys
16+
17+
import board
18+
import keypad
19+
import displayio
20+
import espcamera
21+
import espidf
22+
23+
# The demo runs very slowly if the LCD display is enabled!
24+
# It's intended to be viewed on the REPL on a host computer
25+
displayio.release_displays()
26+
27+
if espidf.get_reserved_psram() < 1047586:
28+
print("""Place the following line in CIRCUITPY/settings.toml, then hard-reset the board:
29+
```
30+
CIRCUITPY_RESERVED_PSRAM
31+
```
32+
""")
33+
raise SystemExit
34+
35+
print("Initializing camera")
36+
cam = espcamera.Camera(
37+
data_pins=board.CAMERA_DATA,
38+
external_clock_pin=board.CAMERA_XCLK,
39+
pixel_clock_pin=board.CAMERA_PCLK,
40+
vsync_pin=board.CAMERA_VSYNC,
41+
href_pin=board.CAMERA_HREF,
42+
pixel_format=espcamera.PixelFormat.GRAYSCALE,
43+
frame_size=espcamera.FrameSize.QQVGA,
44+
i2c=board.I2C(),
45+
external_clock_frequency=20_000_000,
46+
framebuffer_count=2)
47+
print("initialized")
48+
49+
k = keypad.Keys([board.IO0], value_when_pressed=False)
50+
51+
chars = b" .:-=+*#%@"
52+
remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)]
53+
width = cam.width
54+
row = bytearray(width//2)
55+
56+
sys.stdout.write("\033[2J")
57+
58+
while True:
59+
if (e := k.events.get()) is not None and e.pressed:
60+
cam.colorbar = not cam.colorbar
61+
62+
frame = cam.take(1)
63+
64+
for j in range(0, cam.height, 5):
65+
sys.stdout.write(f"\033[{j//5}H")
66+
for i in range(cam.width // 2):
67+
row[i] = remap[frame[width * j + 2 * i]]
68+
sys.stdout.write(row)
69+
sys.stdout.write("\033[K")
70+
sys.stdout.write("\033[J")
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.
6+
7+
It shows the camera image as ASCII art on the USB REPL.
8+
"""
9+
10+
11+
import sys
12+
import time
13+
import busio
14+
import board
15+
import digitalio
16+
import adafruit_ov5640
17+
18+
print("construct bus")
19+
bus = busio.I2C(board.GP9, board.GP8)
20+
print("construct camera")
21+
reset = digitalio.DigitalInOut(board.GP10)
22+
cam = adafruit_ov5640.OV5640(
23+
bus,
24+
data_pins=(
25+
board.GP12,
26+
board.GP13,
27+
board.GP14,
28+
board.GP15,
29+
board.GP16,
30+
board.GP17,
31+
board.GP18,
32+
board.GP19,
33+
),
34+
clock=board.GP11,
35+
vsync=board.GP7,
36+
href=board.GP21,
37+
mclk=board.GP20,
38+
shutdown=None,
39+
reset=reset,
40+
size=adafruit_ov5640.OV5640_SIZE_QQVGA,
41+
)
42+
print("print chip id")
43+
print(cam.chip_id)
44+
45+
46+
cam.colorspace = adafruit_ov5640.OV5640_COLOR_YUV
47+
cam.flip_y = True
48+
cam.flip_x = True
49+
cam.test_pattern = False
50+
51+
buf = bytearray(cam.capture_buffer_size)
52+
chars = b" .':-+=*%$#"
53+
remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)]
54+
55+
width = cam.width
56+
row = bytearray(width)
57+
58+
print("capturing")
59+
cam.capture(buf)
60+
print("capture complete")
61+
62+
sys.stdout.write("\033[2J")
63+
while True:
64+
cam.capture(buf)
65+
for j in range(0, cam.height, 2):
66+
sys.stdout.write(f"\033[{j//2}H")
67+
for i in range(cam.width):
68+
row[i] = remap[buf[2 * (width * j + i)]]
69+
sys.stdout.write(row)
70+
sys.stdout.write("\033[K")
71+
sys.stdout.write("\033[J")
72+
time.sleep(0.1)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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. with 240x240 SPI TFT display
6+
7+
It shows the camera image on the LCD
8+
"""
9+
import time
10+
import busio
11+
import board
12+
import digitalio
13+
import adafruit_ov5640
14+
import adafruit_st7789
15+
import displayio
16+
17+
# Set up the display (You must customize this block for your display!)
18+
displayio.release_displays()
19+
spi = busio.SPI(clock=board.GP2, MOSI=board.GP3)
20+
display_bus = displayio.FourWire(spi, command=board.GP0, chip_select=board.GP1, reset=None)
21+
display = adafruit_st7789.ST7789(display_bus, width=240, height=240, rowstart=80, rotation=0)
22+
23+
print("construct bus")
24+
bus = busio.I2C(board.GP9, board.GP8)
25+
print("construct camera")
26+
reset = digitalio.DigitalInOut(board.GP10)
27+
cam = adafruit_ov5640.OV5640(
28+
bus,
29+
data_pins=(
30+
board.GP12,
31+
board.GP13,
32+
board.GP14,
33+
board.GP15,
34+
board.GP16,
35+
board.GP17,
36+
board.GP18,
37+
board.GP19,
38+
),
39+
clock=board.GP11,
40+
vsync=board.GP7,
41+
href=board.GP21,
42+
mclk=board.GP20,
43+
shutdown=None,
44+
reset=reset,
45+
size=adafruit_ov5640.OV5640_SIZE_240X240,
46+
)
47+
print("print chip id")
48+
print(cam.chip_id)
49+
50+
cam.colorspace = adafruit_ov5640.OV5640_COLOR_RGB
51+
cam.flip_y = False
52+
cam.flip_x = False
53+
cam.test_pattern = False
54+
55+
width = display.width
56+
height = display.height
57+
58+
#cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR_FADE
59+
bitmap = displayio.Bitmap(cam.width, cam.height, 65535)
60+
print(width, height, cam.width, cam.height)
61+
if bitmap is None:
62+
raise SystemExit("Could not allocate a bitmap")
63+
64+
g = displayio.Group(scale=1, x=(width-cam.width)//2, y=(height-cam.height)//2)
65+
tg = displayio.TileGrid(bitmap,
66+
pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED)
67+
)
68+
g.append(tg)
69+
display.show(g)
70+
71+
t0 = time.monotonic_ns()
72+
display.auto_refresh = False
73+
while True:
74+
cam.capture(bitmap)
75+
bitmap.dirty()
76+
display.refresh(minimum_frames_per_second=0)
77+
t1 = time.monotonic_ns()
78+
print("fps", 1e9 / (t1 - t0))
79+
t0 = t1

0 commit comments

Comments
 (0)