Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2803 from adafruit/picowbell_camera
adding PiCowbell Camera examples
- Loading branch information
Showing
4 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 Limor Fried for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
""" | ||
This demo is designed for the Raspberry Pi Pico and Camera PiCowbell | ||
It take an image when the shutter button is pressed and saves it to | ||
the microSD card. | ||
""" | ||
|
||
import os | ||
import time | ||
import busio | ||
import board | ||
import digitalio | ||
import adafruit_ov5640 | ||
import keypad | ||
import sdcardio | ||
import storage | ||
|
||
print("Initializing SD card") | ||
sd_spi = busio.SPI(clock=board.GP18, MOSI=board.GP19, MISO=board.GP16) | ||
sd_cs = board.GP17 | ||
sdcard = sdcardio.SDCard(sd_spi, sd_cs) | ||
vfs = storage.VfsFat(sdcard) | ||
storage.mount(vfs, "/sd") | ||
|
||
print("construct bus") | ||
i2c = busio.I2C(board.GP5, board.GP4) | ||
print("construct camera") | ||
reset = digitalio.DigitalInOut(board.GP14) | ||
cam = adafruit_ov5640.OV5640( | ||
i2c, | ||
data_pins=( | ||
board.GP6, | ||
board.GP7, | ||
board.GP8, | ||
board.GP9, | ||
board.GP10, | ||
board.GP11, | ||
board.GP12, | ||
board.GP13, | ||
), | ||
clock=board.GP3, | ||
vsync=board.GP0, | ||
href=board.GP2, | ||
mclk=None, | ||
shutdown=None, | ||
reset=reset, | ||
size=adafruit_ov5640.OV5640_SIZE_VGA, | ||
) | ||
print("print chip id") | ||
print(cam.chip_id) | ||
|
||
keys = keypad.Keys((board.GP22,), value_when_pressed=False, pull=True) | ||
|
||
def exists(filename): | ||
try: | ||
os.stat(filename) | ||
return True | ||
except OSError as _: | ||
return False | ||
|
||
|
||
_image_counter = 0 | ||
|
||
|
||
def open_next_image(): | ||
global _image_counter # pylint: disable=global-statement | ||
while True: | ||
filename = f"/sd/img{_image_counter:04d}.jpg" | ||
_image_counter += 1 | ||
if exists(filename): | ||
continue | ||
print("# writing to", filename) | ||
return open(filename, "wb") | ||
|
||
cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG | ||
cam.quality = 3 | ||
b = bytearray(cam.capture_buffer_size) | ||
jpeg = cam.capture(b) | ||
|
||
while True: | ||
shutter = keys.events.get() | ||
# event will be None if nothing has happened. | ||
if shutter: | ||
if shutter.pressed: | ||
time.sleep(0.01) | ||
jpeg = cam.capture(b) | ||
print(f"Captured {len(jpeg)} bytes of jpeg data") | ||
print(f" (had allocated {cam.capture_buffer_size} bytes") | ||
print(f"Resolution {cam.width}x{cam.height}") | ||
try: | ||
with open_next_image() as f: | ||
f.write(jpeg) | ||
print("# Wrote image") | ||
except OSError as e: | ||
print(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 Limor Fried for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
""" | ||
This demo is designed for the Raspberry Pi Pico, Camera PiCowbell, | ||
and ST7789 240x240 SPI TFT display | ||
It shows the camera image on the LCD | ||
""" | ||
|
||
import time | ||
import busio | ||
import board | ||
import digitalio | ||
import adafruit_ov5640 | ||
import adafruit_st7789 | ||
import displayio | ||
|
||
displayio.release_displays() | ||
spi = busio.SPI(clock=board.GP18, MOSI=board.GP19) | ||
display_bus = displayio.FourWire(spi, command=board.GP21, chip_select=board.GP17, reset=None) | ||
display = adafruit_st7789.ST7789(display_bus, width=240, height=240, rowstart=80, rotation=0) | ||
|
||
print("construct bus") | ||
i2c = busio.I2C(board.GP5, board.GP4) | ||
print("construct camera") | ||
reset = digitalio.DigitalInOut(board.GP14) | ||
cam = adafruit_ov5640.OV5640( | ||
i2c, | ||
data_pins=( | ||
board.GP6, | ||
board.GP7, | ||
board.GP8, | ||
board.GP9, | ||
board.GP10, | ||
board.GP11, | ||
board.GP12, | ||
board.GP13, | ||
), | ||
clock=board.GP3, | ||
vsync=board.GP0, | ||
href=board.GP2, | ||
mclk=None, | ||
shutdown=None, | ||
reset=reset, | ||
size=adafruit_ov5640.OV5640_SIZE_240X240, | ||
) | ||
print("print chip id") | ||
print(cam.chip_id) | ||
|
||
|
||
cam.colorspace = adafruit_ov5640.OV5640_COLOR_RGB | ||
cam.flip_y = False | ||
cam.flip_x = False | ||
cam.test_pattern = False | ||
|
||
width = display.width | ||
height = display.height | ||
|
||
#cam.test_pattern = OV7670_TEST_PATTERN_COLOR_BAR_FADE | ||
bitmap = displayio.Bitmap(cam.width, cam.height, 65535) | ||
print(width, height, cam.width, cam.height) | ||
if bitmap is None: | ||
raise SystemExit("Could not allocate a bitmap") | ||
|
||
g = displayio.Group(scale=1, x=(width-cam.width)//2, y=(height-cam.height)//2) | ||
tg = displayio.TileGrid(bitmap, | ||
pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED) | ||
) | ||
g.append(tg) | ||
display.root_group = 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 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import time | ||
import board | ||
import displayio | ||
import adafruit_displayio_ssd1306 | ||
import adafruit_imageload | ||
|
||
#display setup | ||
displayio.release_displays() | ||
|
||
i2c = board.STEMMA_I2C() | ||
# oled | ||
oled_reset = board.D9 | ||
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=oled_reset) | ||
WIDTH = 128 | ||
HEIGHT = 64 | ||
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT) | ||
|
||
# default group | ||
group = displayio.Group() | ||
display.root_group = group | ||
|
||
# graphics bitmap | ||
bitmap, palette_bit = adafruit_imageload.load( | ||
"/bongo.bmp", | ||
bitmap=displayio.Bitmap, | ||
palette=displayio.Palette, | ||
) | ||
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette_bit, | ||
width=1, height=1, | ||
tile_height=64, tile_width=105, | ||
default_tile=0, x = 7) | ||
group.append(tile_grid) | ||
|
||
bongo = 0 | ||
index = 0 | ||
delay = 0.15 | ||
|
||
while True: | ||
if (bongo + delay) < time.monotonic(): | ||
tile_grid[0] = index | ||
index = 1 if index == 0 else 0 | ||
bongo = time.monotonic() |