Skip to content

Commit 21d5653

Browse files
authored
Merge pull request #2710 from adafruit/ext_shutter
Adding code for the external shutter button
2 parents 8fceaeb + 0eeaf75 commit 21d5653

File tree

1 file changed

+185
-0
lines changed
  • MEMENTO/PyCamera_External_Shutter_Button

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# SPDX-FileCopyrightText: 2023 Jeff Epler for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import time
6+
import board
7+
import bitmaptools
8+
import displayio
9+
import gifio
10+
import ulab.numpy as np
11+
from adafruit_debouncer import Button
12+
import adafruit_pycamera
13+
from digitalio import DigitalInOut, Direction, Pull
14+
15+
pycam = adafruit_pycamera.PyCamera()
16+
# pycam.live_preview_mode()
17+
18+
settings = (None, "resolution", "effect", "mode", "led_level", "led_color")
19+
curr_setting = 0
20+
21+
pin = DigitalInOut(board.A0)
22+
pin.direction = Direction.INPUT
23+
pin.pull = Pull.UP
24+
ext_button = Button(pin, long_duration_ms=1000)
25+
26+
print("Starting!")
27+
# pycam.tone(200, 0.1)
28+
last_frame = displayio.Bitmap(pycam.camera.width, pycam.camera.height, 65535)
29+
onionskin = displayio.Bitmap(pycam.camera.width, pycam.camera.height, 65535)
30+
while True:
31+
if pycam.mode_text == "STOP" and pycam.stop_motion_frame != 0:
32+
# alpha blend
33+
new_frame = pycam.continuous_capture()
34+
bitmaptools.alphablend(
35+
onionskin, last_frame, new_frame, displayio.Colorspace.RGB565_SWAPPED
36+
)
37+
pycam.blit(onionskin)
38+
elif pycam.mode_text == "GBOY":
39+
bitmaptools.dither(
40+
last_frame, pycam.continuous_capture(), displayio.Colorspace.RGB565_SWAPPED
41+
)
42+
pycam.blit(last_frame)
43+
else:
44+
pycam.blit(pycam.continuous_capture())
45+
# print("\t\t", capture_time, blit_time)
46+
47+
pycam.keys_debounce()
48+
ext_button.update()
49+
# test shutter button
50+
if pycam.shutter.long_press or ext_button.long_press:
51+
print("FOCUS")
52+
print(pycam.autofocus_status)
53+
pycam.autofocus()
54+
print(pycam.autofocus_status)
55+
if pycam.shutter.short_count or ext_button.short_count:
56+
print("Shutter released")
57+
if pycam.mode_text == "STOP":
58+
pycam.capture_into_bitmap(last_frame)
59+
pycam.stop_motion_frame += 1
60+
try:
61+
pycam.display_message("Snap!", color=0x0000FF)
62+
pycam.capture_jpeg()
63+
except TypeError as e:
64+
pycam.display_message("Failed", color=0xFF0000)
65+
time.sleep(0.5)
66+
except RuntimeError as e:
67+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
68+
time.sleep(0.5)
69+
pycam.live_preview_mode()
70+
71+
if pycam.mode_text == "GBOY":
72+
try:
73+
f = pycam.open_next_image("gif")
74+
except RuntimeError as e:
75+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
76+
time.sleep(0.5)
77+
continue
78+
79+
with gifio.GifWriter(
80+
f,
81+
pycam.camera.width,
82+
pycam.camera.height,
83+
displayio.Colorspace.RGB565_SWAPPED,
84+
dither=True,
85+
) as g:
86+
g.add_frame(last_frame, 1)
87+
88+
if pycam.mode_text == "GIF":
89+
try:
90+
f = pycam.open_next_image("gif")
91+
except RuntimeError as e:
92+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
93+
time.sleep(0.5)
94+
continue
95+
i = 0
96+
ft = []
97+
pycam._mode_label.text = "RECORDING" # pylint: disable=protected-access
98+
99+
pycam.display.refresh()
100+
with gifio.GifWriter(
101+
f,
102+
pycam.camera.width,
103+
pycam.camera.height,
104+
displayio.Colorspace.RGB565_SWAPPED,
105+
dither=True,
106+
) as g:
107+
t00 = t0 = time.monotonic()
108+
while (i < 15) or not pycam.shutter_button.value:
109+
i += 1
110+
_gifframe = pycam.continuous_capture()
111+
g.add_frame(_gifframe, 0.12)
112+
pycam.blit(_gifframe)
113+
t1 = time.monotonic()
114+
ft.append(1 / (t1 - t0))
115+
print(end=".")
116+
t0 = t1
117+
pycam._mode_label.text = "GIF" # pylint: disable=protected-access
118+
print(f"\nfinal size {f.tell()} for {i} frames")
119+
print(f"average framerate {i/(t1-t00)}fps")
120+
print(f"best {max(ft)} worst {min(ft)} std. deviation {np.std(ft)}")
121+
f.close()
122+
pycam.display.refresh()
123+
124+
if pycam.mode_text == "JPEG":
125+
pycam.tone(200, 0.1)
126+
try:
127+
pycam.display_message("Snap!", color=0x0000FF)
128+
pycam.capture_jpeg()
129+
pycam.live_preview_mode()
130+
except TypeError as e:
131+
pycam.display_message("Failed", color=0xFF0000)
132+
time.sleep(0.5)
133+
pycam.live_preview_mode()
134+
except RuntimeError as e:
135+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
136+
time.sleep(0.5)
137+
if pycam.card_detect.fell:
138+
print("SD card removed")
139+
pycam.unmount_sd_card()
140+
pycam.display.refresh()
141+
if pycam.card_detect.rose:
142+
print("SD card inserted")
143+
pycam.display_message("Mounting\nSD Card", color=0xFFFFFF)
144+
for _ in range(3):
145+
try:
146+
print("Mounting card")
147+
pycam.mount_sd_card()
148+
print("Success!")
149+
break
150+
except OSError as e:
151+
print("Retrying!", e)
152+
time.sleep(0.5)
153+
else:
154+
pycam.display_message("SD Card\nFailed!", color=0xFF0000)
155+
time.sleep(0.5)
156+
pycam.display.refresh()
157+
158+
if pycam.up.fell:
159+
print("UP")
160+
key = settings[curr_setting]
161+
if key:
162+
setattr(pycam, key, getattr(pycam, key) + 1)
163+
if pycam.down.fell:
164+
print("DN")
165+
key = settings[curr_setting]
166+
if key:
167+
setattr(pycam, key, getattr(pycam, key) - 1)
168+
if pycam.left.fell:
169+
print("LF")
170+
curr_setting = (curr_setting + 1) % len(settings)
171+
print(settings[curr_setting])
172+
# new_res = min(len(pycam.resolutions)-1, pycam.get_resolution()+1)
173+
# pycam.set_resolution(pycam.resolutions[new_res])
174+
pycam.select_setting(settings[curr_setting])
175+
if pycam.right.fell:
176+
print("RT")
177+
curr_setting = (curr_setting - 1 + len(settings)) % len(settings)
178+
print(settings[curr_setting])
179+
pycam.select_setting(settings[curr_setting])
180+
# new_res = max(1, pycam.get_resolution()-1)
181+
# pycam.set_resolution(pycam.resolutions[new_res])
182+
if pycam.select.fell:
183+
print("SEL")
184+
if pycam.ok.fell:
185+
print("OK")

0 commit comments

Comments
 (0)