/
recorder.c
148 lines (138 loc) · 3.69 KB
/
recorder.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
142
143
144
145
146
147
148
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <sys/time.h>
#include <arpa/inet.h>
#ifdef SELF_BULK
#include "sb.h"
#endif
#include "ASICamera2.h"
#include "ser.h"
#define CHECK1(X) do { \
int r=(X); \
if (r != ASI_SUCCESS) { \
fprintf(stderr, "ASI Error on '%s': %d\n", #X, r); \
exit(1); \
} \
} while(0)
#define CHECK(X) do { \
fprintf(stderr, "%s\n", #X); \
int r=(X); \
if (r != ASI_SUCCESS) { \
fprintf(stderr, "ASI Error on '%s': %d\n", #X, r); \
exit(1); \
} \
} while(0)
static unsigned int cidx;
static void set_cap(int idx, int val) {
CHECK(ASISetControlValue(cidx, idx, val, 0));
}
int main(int argc, char *argv[]) {
unsigned int exposure, gain, mode, nzwo, bin, lm;
ASI_CAMERA_INFO prop;
unsigned char *buff;
int bsize, n=0, max = 0;
float sum = 0.0;
int width, height;
if (argc != 9) {
printf("Usage: %s [camera idx] [mode] [bin] [exposure in ms] [gain] [long mode] [w if >0] [h if>0]\n", argv[0]);
exit(1);
}
cidx = strtoul(argv[1], NULL, 0);
mode = strtoul(argv[2], NULL, 0);
bin = strtoul(argv[3], NULL, 0);
exposure = strtoul(argv[4], NULL, 0);
gain = strtoul(argv[5], NULL, 0);
lm = strtoul(argv[6], NULL, 0);
width = strtol(argv[7], NULL, 0);
height = strtol(argv[8], NULL, 0);
nzwo = ASIGetNumOfConnectedCameras();
if (cidx >= nzwo) {
fprintf(stderr, "Camera out of range %d/%d\n", cidx, nzwo);
exit(1);
}
CHECK(ASIGetCameraProperty(&prop, cidx));
CHECK(ASIOpenCamera(cidx));
CHECK(ASIInitCamera(cidx));
if (width <= 0)
width = prop.MaxWidth/bin;
if (height <= 0)
height = prop.MaxHeight/bin;
fprintf(stderr, "%d x %d\n", width, height);
bsize = width * height;
if (mode == 1)
bsize *= 3;
else if (mode == 2)
bsize *= 2;
buff = malloc(bsize);
CHECK(ASISetROIFormat(cidx, width, height, bin, mode));
set_cap(ASI_EXPOSURE, exposure * 1000);
set_cap(ASI_GAIN, gain);
set_cap(ASI_HARDWARE_BIN, ASI_TRUE);
#ifdef SELF_BULK
sb_init(bsize);
#endif
if (lm == 0)
CHECK(ASIStartVideoCapture(cidx));
while (1) {
int r;
struct timespec start, stop;
long delta;
clock_gettime(CLOCK_MONOTONIC, &start);
if (lm == 1) {
ASI_EXPOSURE_STATUS s;
CHECK1(ASIGetExpStatus(cidx, &s));
if (s != ASI_EXP_IDLE && s != ASI_EXP_FAILED) {
fprintf(stderr, "Not idle or failed: %d\n", s);
exit(1);
}
CHECK(ASIStartExposure(cidx, 0));
fprintf(stderr, "START\n");
r = 99; /* timeout */
do {
usleep(10*1000);
clock_gettime(CLOCK_MONOTONIC, &stop);
delta = (stop.tv_sec - start.tv_sec) * 1000 +
(stop.tv_nsec - start.tv_nsec) / 1000000;
CHECK1(ASIGetExpStatus(cidx, &s));
if (s == ASI_EXP_IDLE) {
fprintf(stderr, "start\n");
CHECK(ASIStartExposure(cidx, 0));
}
else if (s == ASI_EXP_SUCCESS) {
r = 0;
fprintf(stderr, "FINISHED\n");
CHECK(ASIGetDataAfterExp(cidx, buff, bsize));
break;
} else if (s == ASI_EXP_FAILED) {
r = 98;
break;
}
else if (s != ASI_EXP_WORKING) {
fprintf(stderr, "Unexpected status\n");
exit(1);
}
} while (delta < 20 * exposure + 500);
}
else {
fprintf(stderr, "ASIGetVideoData\n");
r = ASIGetVideoData(cidx, buff, bsize, 20 * exposure + 500);
clock_gettime(CLOCK_MONOTONIC, &stop);
delta = (stop.tv_sec - start.tv_sec) * 1000 +
(stop.tv_nsec - start.tv_nsec) / 1000000;
}
n++;
if (delta > max)
max = delta;
sum += delta;
fprintf(stderr, ":%2d %6.0f(%6d) %ld\n",
r, sum / n, max, delta);
}
return 0;
}