-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpishot.py
145 lines (105 loc) · 3.13 KB
/
pishot.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Main PiShot script.
"""
from __future__ import print_function
import os
import subprocess
import time
import signal
import argparse
import RPi.GPIO as GPIO
INPUT_PIN = 17 # Trigger pin.
CLONE_PIN = 23 # Mirror trigger pin.
GPIO.setmode(GPIO.BCM)
GPIO.setup(INPUT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(CLONE_PIN, GPIO.OUT)
raspivid_process = None
trigger_process = None
def non_frex_shot(fn="test.jpg"):
process = subprocess.Popen(
("raspistill -o %s --nopreview --exposure sports --timeout 1" % fn).split()
)
process.wait()
def write_frex_registers():
"""Enables FREX mode on the ov5647 camera module. And sets the integration
time to the maximum allowed.
"""
process = subprocess.Popen("./i2cwrite /dev/i2c-0 3002 ff".split())
process.wait()
process = subprocess.Popen(
"./i2cwrite /dev/i2c-0 3b01 00 08 00 ff ff 14 1d".split()
)
process.wait()
def open_shutter():
"""Start integrating FREX frames.
"""
global raspivid_process
global trigger_process
if os.path.exists("temp.264"):
os.remove("temp.264")
raspivid_process = subprocess.Popen(
"raspivid -md 2 -fps 1 --awb flash -pts test.pts -t 800000 -o temp.264".split()
)
time.sleep(1)
write_frex_registers()
def close_shutter(filename="temp.264"):
"""Close the shutter and save the FREX frames.
"""
global raspivid_process
global trigger_process
if raspivid_process is None:
return
os.kill(raspivid_process.pid, signal.SIGTERM)
print("Shutter closed!")
def one_shot(t):
"""Open the shutter, wait for a bit and then close it.
"""
open_shutter()
time.sleep(t)
close_shutter("test.png")
def slave_loop():
"""Operate the Pi in slave mode.
"""
while True:
GPIO.wait_for_edge(INPUT_PIN, GPIO.RISING)
GPIO.output(CLONE_PIN, 1)
open_shutter()
GPIO.wait_for_edge(INPUT_PIN, GPIO.FALLING)
GPIO.output(CLONE_PIN, 0)
close_shutter("test.png")
def master_loop():
"""Operate the Pi in master mode.
"""
while True:
command = raw_input(">> ")
if command == "o" or command == "open":
GPIO.output(CLONE_PIN, 1)
open_shutter()
if command == "c" or command == "close":
GPIO.output(CLONE_PIN, 0)
close_shutter("test.png")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PiShot main script.")
parser.add_argument(
"--one", default=False, action="store_true", help="Take a single shot"
)
parser.add_argument(
"-t",
help="The exposure time for --one mode.",
action="store",
dest="t",
type=int,
default=10,
)
parser.add_argument(
"--slave", default=False, action="store_true", help="Run daemon in Slave mode."
)
parser.add_argument(
"--master", default=True, action="store_true", help="Run daemon in Master mode."
)
args = parser.parse_args()
if args.one:
one_shot(args.t)
elif args.slave:
slave_loop()
elif args.master:
master_loop()