-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcameraTraps.c
141 lines (112 loc) · 2.55 KB
/
cameraTraps.c
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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include "config.h"
#include "v4l2_camera.h"
#include "image_writer.h"
#include "sensor.h"
#include "errors.h"
#define DEFAULT_PHOTO_DELAY 200
#define DEFAULT_CONFIG_FILE "/etc/cameraTraps.conf"
struct config_t *cfg = NULL;
struct camera_t *cam = NULL;
struct sensor_t *snr = NULL;
int shouldExit = 0;
static void signal_cleanup(int signum)
{
shouldExit = 1;
return;
}
static void atexit_cleanup(void)
{
if (cam) {
CAM_unprepare(cam);
CAM_destroy(cam);
}
if (snr)
SNR_destroy(snr);
if (cfg)
CFG_destroy(cfg);
return;
}
static void write_file(char *filename, struct camera_buffer_t buff, struct config_t *cfg)
{
const struct image_t img = {
.width = 640,
.height = 480,
.num_pixel_components = 3,
.format = IMG_WR_FMT_RGB,
.data = buff.buffer,
};
write_image(filename, &img, cfg);
}
int main(int argc, char *argv[])
{
char *config_file = NULL;
unsigned long long int delay;
atexit(atexit_cleanup);
signal(SIGINT, signal_cleanup);
if (argc < 2)
config_file = DEFAULT_CONFIG_FILE;
else
config_file = argv[1];
cfg = CFG_ParseConfigFile(config_file);
if (!cfg)
exit(-1);
char *photo_dir = CFG_GetValue(cfg, "photo_dir");
if (!photo_dir) {
ERR_NOCFG("photo_dir");
exit(-1);
}
char *str_delay = NULL;
if ((str_delay = CFG_GetValue(cfg, "photo_delay")) != NULL) {
delay = atoi(str_delay);
} else {
delay = DEFAULT_PHOTO_DELAY;
}
if (delay < 10 || delay > 1000) {
WARN(EINVAL, "Photo delay is either invalid or not in range [100, 1000]");
exit(EXIT_FAILURE);
}
cam = CAM_open(cfg);
if (!cam)
exit(-1);
snr = SNR_open(cfg);
if (!snr)
exit(-1);
char filename[256];
char date[64];
struct camera_buffer_t buff;
struct timespec ts = {0, 100000000};
struct timespec long_ts = {delay / 1000, delay * 1000000 % 1000000000};
int isActive = 0, old_isActive = 0;
int counter = 0;
CAM_prepare(cam);
while (1) {
nanosleep(&ts, NULL);
if (shouldExit)
break;
isActive = SNR_IsActive(snr);
if (isActive && !old_isActive)
counter = 0;
old_isActive = isActive;
if (!isActive)
continue;
counter++;
buff = CAM_capture(cam);
if (buff.length <= 0)
continue;
time_t t = time(NULL);
strftime(date, sizeof(date), "%Y%m%d_%H%M", localtime(&t));
snprintf(filename, sizeof(filename), "%s/%s_%d.jpg", photo_dir, date, counter);
printf("Making photo %s\n", filename);
write_file(filename, buff, cfg);
nanosleep(&long_ts, NULL);
}
return 0;
}