-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
103 lines (93 loc) · 3.97 KB
/
main.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
import sys
import argparse
import archive
import pinIO
import config
import control
# main presents cl utility:
# - stop: kills any running cameras
# - archive: cleverly compresses from past time X
# - start: finds cameras and starts taking pictures every 10 seconds
# - info: retrieves camera device info
def stop():
pass
def start_poll(epoll, f_archive, f_toggle, psecs, rsecs):
config.toggle_capturing(False)
count = 0
psecs = psecs / config.poll_time
accelerometer_logger = open('/home/ubuntu/camcontrol/log_accel.txt', 'w')
helper = pinIO.open_ain_helper()
with f_archive, f_toggle:
archive.roll(rsecs)
while True:
if helper and config.is_capturing() and config.is_accelerometer():
vals = pinIO.accelerometer_values(helper)
accelerometer_logger.write('volts - x: %f, y: %f, z: %f\n' % (vals.get('x'), vals.get('y'), vals.get('z')))
accelerometer_logger.write('gs - x: %f, y: %f, z: %f\n' % (pinIO.g(vals.get('x')), pinIO.g(vals.get('y')), pinIO.g(vals.get('z'))))
accelerometer_logger.write('\n\n')
count += 1
events = epoll.poll(config.poll_time) #every 500 millisec
for fileno, event in events:
if fileno == f_archive.fileno():
pinIO.light(config.light_pins['archive'], True)
archive.save(config.archive_time)
pinIO.light(config.light_pins['archive'], False)
if fileno == f_toggle.fileno():
# config.toggle_capturing()
if config.is_capturing():
print 'Started Capturing'
else:
print 'Stopped Capturing'
pinIO.light(config.light_pins['toggle'], config.is_capturing())
if count % psecs == 0 and config.is_capturing():
control.capture(config.get_unixtime())
if count > rsecs/2 and config.is_capturing():
archive.roll(rsecs)
count = 0
if config.is_capturing() and config.is_sounding():
#hack to stop and record audio ... not needed right now
control.sound(config.get_unixtime(), 1)
def start_time():
pinIO.subprocess('echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device')
pinIO.subprocess('hwclock -s -f /dev/rtc1')
pinIO.subprocess('hwclock -w')
def start(psecs, rsecs=300):
# try:
# output, errors = pinIO.subprocess('ls /dev/')
# if not 'rtc1' in output.split('\n'):
# start_time()
# except Exception, e:
# print e
# sys.exit("Time not enabled")
try:
pinIO.open_pins()
except Exception, e:
print e
pinIO.close_pins()
sys.exit("Can't open the pins. Exiting")
try:
epoll, f_archive, f_toggle = pinIO.get_poll()
start_poll(epoll, f_archive, f_toggle, psecs, rsecs)
except Exception, e:
print e
pinIO.close_pins()
sys.exit("Starting poll failed. Exiting")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Control Cameras using Streamer',
usage='Precedence: Stop, Start, Archive, Roll')
parser.add_argument('--stop', help='terminate any running cameras', action="store_true")
parser.add_argument('--start', help='finds and starts all cameras', action="store_true")
parser.add_argument('--psecs', type=int, default=5, choices=[5,10,30,60], help='time between taking pictures')
parser.add_argument('--archive', type=int, choices=[30, 600, 3600], help='compresses recent data')
parser.add_argument('--roll', type=int, help='deletes older uncompressed data')
args = parser.parse_args()
if args.stop:
stop()
elif args.start:
start(args.psecs)
elif args.archive:
archive.save(args.archive)
elif args.roll:
archive.roll(args.roll)
else:
print 'Doing Nothing'