-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_external_trigger.py
81 lines (56 loc) · 2.25 KB
/
read_external_trigger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# read_external_pin - By: Anton Pollak - Sat Jun 24 2023
# this script reads a high/low signal from a GPIO pin and takes a snapshot at a high value
# GPIO tutorial: https://docs.openmv.io/openmvcam/tutorial/gpio_control.html
import sensor, image, time, pyb, time, mjpeg
import os
if 'recorded_images' not in os.listdir():
os.mkdir('recorded_images')
if 'recorded_videos' not in os.listdir():
os.mkdir('recorded_videos')
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000) # to let the camera stabilize after changing settings
clock = time.clock()
counter = len(os.listdir('recorded_images')) + 1
video_counter = len(os.listdir('recorded_videos')) + 1
#print('counter:', counter)
pin = pyb.Pin("P0", pyb.Pin.IN)
mode_pin = pyb.Pin("P1", pyb.Pin.IN, pyb.Pin.PULL_UP)
# Initialize the LED
green_led = pyb.LED(2)
blue_led = pyb.LED(3)
green_led.off()
blue_led.off()
# Loop forever
while(True):
# print(pin.value()) # print GPIO value to serial terminal
if mode_pin.value() == 1:
blue_led.on()
m = mjpeg.Mjpeg("recorded_videos/"+ str(video_counter) + ".mjpeg")
while mode_pin.value() == 1:
clock.tick()
m.add_frame(sensor.snapshot())
#print(clock.fps())
m.close(clock.fps())
blue_led.off()
video_counter += 1
#while mode_pin.value() == 1:
#pass # do nothing to wait for the trigger going away, to make sure only one image is collected per trigger
else:
# collect image if GPIO pin detects a HIGH signal
if pin.value() == 1:
img = sensor.snapshot()
# toggle green LED after recording image to provide positive user feedback
green_led.on()
time.sleep_ms(100)
green_led.off()
# Saving the image
img.save('/recorded_images/' + str(counter))
counter += 1
# Stop continuing until the pin value has gone to low again
while pin.value() == 1:
pass # do nothing to wait for the trigger going away, to make sure only one image is collected per trigger
else:
# Do nothing
pass