Skip to content

Commit a4f5493

Browse files
authored
Merge pull request #2803 from adafruit/picowbell_camera
adding PiCowbell Camera examples
2 parents 21ca6d8 + 814642e commit a4f5493

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
7+
It take an image when the shutter button is pressed and saves it to
8+
the microSD card.
9+
"""
10+
11+
import os
12+
import time
13+
import busio
14+
import board
15+
import digitalio
16+
import adafruit_ov5640
17+
import keypad
18+
import sdcardio
19+
import storage
20+
21+
print("Initializing SD card")
22+
sd_spi = busio.SPI(clock=board.GP18, MOSI=board.GP19, MISO=board.GP16)
23+
sd_cs = board.GP17
24+
sdcard = sdcardio.SDCard(sd_spi, sd_cs)
25+
vfs = storage.VfsFat(sdcard)
26+
storage.mount(vfs, "/sd")
27+
28+
print("construct bus")
29+
i2c = busio.I2C(board.GP5, board.GP4)
30+
print("construct camera")
31+
reset = digitalio.DigitalInOut(board.GP14)
32+
cam = adafruit_ov5640.OV5640(
33+
i2c,
34+
data_pins=(
35+
board.GP6,
36+
board.GP7,
37+
board.GP8,
38+
board.GP9,
39+
board.GP10,
40+
board.GP11,
41+
board.GP12,
42+
board.GP13,
43+
),
44+
clock=board.GP3,
45+
vsync=board.GP0,
46+
href=board.GP2,
47+
mclk=None,
48+
shutdown=None,
49+
reset=reset,
50+
size=adafruit_ov5640.OV5640_SIZE_VGA,
51+
)
52+
print("print chip id")
53+
print(cam.chip_id)
54+
55+
keys = keypad.Keys((board.GP22,), value_when_pressed=False, pull=True)
56+
57+
def exists(filename):
58+
try:
59+
os.stat(filename)
60+
return True
61+
except OSError as _:
62+
return False
63+
64+
65+
_image_counter = 0
66+
67+
68+
def open_next_image():
69+
global _image_counter # pylint: disable=global-statement
70+
while True:
71+
filename = f"/sd/img{_image_counter:04d}.jpg"
72+
_image_counter += 1
73+
if exists(filename):
74+
continue
75+
print("# writing to", filename)
76+
return open(filename, "wb")
77+
78+
cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG
79+
cam.quality = 3
80+
b = bytearray(cam.capture_buffer_size)
81+
jpeg = cam.capture(b)
82+
83+
while True:
84+
shutter = keys.events.get()
85+
# event will be None if nothing has happened.
86+
if shutter:
87+
if shutter.pressed:
88+
time.sleep(0.01)
89+
jpeg = cam.capture(b)
90+
print(f"Captured {len(jpeg)} bytes of jpeg data")
91+
print(f" (had allocated {cam.capture_buffer_size} bytes")
92+
print(f"Resolution {cam.width}x{cam.height}")
93+
try:
94+
with open_next_image() as f:
95+
f.write(jpeg)
96+
print("# Wrote image")
97+
except OSError as e:
98+
print(e)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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, Camera PiCowbell,
6+
and ST7789 240x240 SPI TFT display
7+
8+
It shows the camera image on the LCD
9+
"""
10+
11+
import time
12+
import busio
13+
import board
14+
import digitalio
15+
import adafruit_ov5640
16+
import adafruit_st7789
17+
import displayio
18+
19+
displayio.release_displays()
20+
spi = busio.SPI(clock=board.GP18, MOSI=board.GP19)
21+
display_bus = displayio.FourWire(spi, command=board.GP21, chip_select=board.GP17, reset=None)
22+
display = adafruit_st7789.ST7789(display_bus, width=240, height=240, rowstart=80, rotation=0)
23+
24+
print("construct bus")
25+
i2c = busio.I2C(board.GP5, board.GP4)
26+
print("construct camera")
27+
reset = digitalio.DigitalInOut(board.GP14)
28+
cam = adafruit_ov5640.OV5640(
29+
i2c,
30+
data_pins=(
31+
board.GP6,
32+
board.GP7,
33+
board.GP8,
34+
board.GP9,
35+
board.GP10,
36+
board.GP11,
37+
board.GP12,
38+
board.GP13,
39+
),
40+
clock=board.GP3,
41+
vsync=board.GP0,
42+
href=board.GP2,
43+
mclk=None,
44+
shutdown=None,
45+
reset=reset,
46+
size=adafruit_ov5640.OV5640_SIZE_240X240,
47+
)
48+
print("print chip id")
49+
print(cam.chip_id)
50+
51+
52+
cam.colorspace = adafruit_ov5640.OV5640_COLOR_RGB
53+
cam.flip_y = False
54+
cam.flip_x = False
55+
cam.test_pattern = False
56+
57+
width = display.width
58+
height = display.height
59+
60+
#cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR_FADE
61+
bitmap = displayio.Bitmap(cam.width, cam.height, 65535)
62+
print(width, height, cam.width, cam.height)
63+
if bitmap is None:
64+
raise SystemExit("Could not allocate a bitmap")
65+
66+
g = displayio.Group(scale=1, x=(width-cam.width)//2, y=(height-cam.height)//2)
67+
tg = displayio.TileGrid(bitmap,
68+
pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED)
69+
)
70+
g.append(tg)
71+
display.root_group = g
72+
73+
t0 = time.monotonic_ns()
74+
display.auto_refresh = False
75+
while True:
76+
cam.capture(bitmap)
77+
bitmap.dirty()
78+
display.refresh(minimum_frames_per_second=0)
79+
t1 = time.monotonic_ns()
80+
print("fps", 1e9 / (t1 - t0))
81+
t0 = t1

QT2040_Trinkey/bongo_cat/bongo.bmp

7.7 KB
Binary file not shown.

QT2040_Trinkey/bongo_cat/code.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import displayio
8+
import adafruit_displayio_ssd1306
9+
import adafruit_imageload
10+
11+
#display setup
12+
displayio.release_displays()
13+
14+
i2c = board.STEMMA_I2C()
15+
# oled
16+
oled_reset = board.D9
17+
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=oled_reset)
18+
WIDTH = 128
19+
HEIGHT = 64
20+
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT)
21+
22+
# default group
23+
group = displayio.Group()
24+
display.root_group = group
25+
26+
# graphics bitmap
27+
bitmap, palette_bit = adafruit_imageload.load(
28+
"/bongo.bmp",
29+
bitmap=displayio.Bitmap,
30+
palette=displayio.Palette,
31+
)
32+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette_bit,
33+
width=1, height=1,
34+
tile_height=64, tile_width=105,
35+
default_tile=0, x = 7)
36+
group.append(tile_grid)
37+
38+
bongo = 0
39+
index = 0
40+
delay = 0.15
41+
42+
while True:
43+
if (bongo + delay) < time.monotonic():
44+
tile_grid[0] = index
45+
index = 1 if index == 0 else 0
46+
bongo = time.monotonic()

0 commit comments

Comments
 (0)