show git hash (if available) when invoking -v key
[gpivtools.git] / src / dac / trig.c
blob0024603a6f8a236c1819607c0746af6177fc98d2
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*-----------------------------------------------------------------------------
4 gpiv_trig - triggers a (double Nd_YAGG) laser on a (CCD) camera
6 Copyright (C) 2005, 2006, 2007, 2008
7 Gerber van der Graaf <gerber_graaf@users.sourceforge.net
8 Julio Soria
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 -----------------------------------------------------------------------------*/
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <getopt.h>
29 #include "system.h"
30 #ifdef ENABLE_TRIG
31 #include <rtai.h>
32 #endif
33 #include <sys/ioctl.h>
34 #include <sys/time.h>
36 #include <gpiv.h>
37 #include "config.h"
38 #include "git-rev.h"
41 #ifdef ENABLE_TRIG
43 #define EXIT_FAILURE 1
45 /* char *xmalloc (); */
46 /* char *xrealloc (); */
47 /* char *xstrdup (); */
50 static void usage (int status);
52 * The name the program was run with, stripped of any leading path.
54 char *program_name;
56 /* getopt_long return codes */
57 enum {DUMMY_CODE=129
60 /* Option flags and variables */
62 gboolean interactive; /* --interactive */
63 gboolean verbose; /* -- verbose output during running process */
65 static struct option const long_options[] =
67 {"interactive", no_argument, 0, 'i'},
68 {"verbose", no_argument, 0, 'V'},
69 {"help", no_argument, 0, 'h'},
70 {"version", no_argument, 0, 'v'},
71 {NULL, 0, NULL, 0}
74 static int
75 command_args (int argc,
76 char **argv);
78 static int
79 interactive_func(void);
81 static int
82 initialise (GpivTrigPar *trig_par
83 /* GpivTrigTime *ttime */
87 * Set all the option flags according to the switches specified.
88 * Return the index of the first non-option argument.
91 static int
92 command_args (int argc,
93 char *argv[]
95 /*-----------------------------------------------------------------------------
98 int c;
101 while ((c = getopt_long (argc, argv,
102 "i" /* interactive */
103 "V" /* verbose */
104 "h" /* help */
105 "v", /* version */
106 long_options, (int *) 0)) != EOF) {
107 switch (c) {
108 case 'i': /* --interactive */
109 interactive = TRUE;
110 break;
111 case 'V': /* --verbose */
112 verbose = TRUE;
113 break;
114 case 'v':
115 printf ("gpiv_trig %s\n", VERSION);
116 exit (0);
117 case 'h':
118 usage (0);
119 default:
120 usage (EXIT_FAILURE);
124 return 0;
129 static void
130 usage (int status
132 /*-----------------------------------------------------------------------------
135 printf (_("%s - \
136 triggers a (double Nd_YAGG) laser on a CCD camera\n"), program_name);
137 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
138 printf (_("\
139 Options:\n\
140 -i, --interactive prompt for confirmation\n\
141 -V, --verbose print more information\n\
142 -h, --help display this help and exit\n\
143 -v, --version output version information and exit\n\
144 "));
145 exit (status);
150 static int
151 initialise (GpivTrigPar *trig_par
152 /* GpivTrigTime *ttime */
154 /*-----------------------------------------------------------------------------
157 int ok = 0;
158 float dtime, acq_time, increment;
159 int mode, cycles;
162 * private user input variables
163 * hardwire some of the timings ....
166 if (verbose) printf("\nEntering initialise\n");
167 trig_par->ttime.laser_trig_pw = (RTIME)(0.02*1000*1000); /* laser trigger pulse width 0.02ms */
168 trig_par->ttime.time2laser = (RTIME)(0.19*1000*1000); /* time from laser trigger->Hi
169 until laser pulse: 0.19ms */
172 printf("\n\tmode:\n");
173 printf("\t\t1 - indefinite periodic\n");
174 printf("\t\t2 - duration\n");
175 printf("\t\t3 - interrupt one shot\n");
176 printf("\t\t4 - interrupt periodic\n");
177 printf("\t\t5 - incremented dt\n");
178 printf("\t\t6 - double exposure\n");
179 printf("\t\t9 - exit\n");
180 printf("\t(1/2/3/4/5/6/9) > ");
181 scanf("%d", &mode);
182 trig_par->ttime.mode = mode;
184 if (trig_par->ttime.mode == 9) {
185 return 1;
189 * ask for other timings ....
191 printf("\n\tAcquisition parameters: \n");
193 printf("\ttime between 2 laser exposures in ms > ");
194 scanf("%f", &dtime);
195 trig_par->ttime.dt = (RTIME)1000 * 1000 * dtime;
197 /* gpiv_trig_test_parameter(GpivTrigPar * trig_par */
198 /* ); */
199 if ((trig_par->ttime.mode == GPIV_TIMER_MODE__PERIODIC)
200 || (trig_par->ttime.mode == GPIV_TIMER_MODE__DURATION)
201 || (trig_par->ttime.mode == GPIV_TIMER_MODE__TRIGGER_IRQ)
202 || (trig_par->ttime.mode == GPIV_TIMER_MODE__DOUBLE)) {
203 printf("\tdouble frame acquisition period in ms (typ. 250 ms) > ");
204 scanf("%f", &acq_time);
205 trig_par->ttime.cam_acq_period = (RTIME)1000 * 1000 * acq_time;
206 } else {
207 trig_par->ttime.cam_acq_period = (trig_par->ttime.dt) * 2;
210 if ((trig_par->ttime.mode == GPIV_TIMER_MODE__DURATION)
211 || (trig_par->ttime.mode == GPIV_TIMER_MODE__ONE_SHOT_IRQ)
212 || (trig_par->ttime.mode == GPIV_TIMER_MODE__TRIGGER_IRQ)
213 || (trig_par->ttime.mode == GPIV_TIMER_MODE__INCREMENT)) {
214 printf("\tnumber of cycles > ");
215 scanf("%d", &cycles);
216 trig_par->ttime.cycles = cycles;
217 } else {
218 trig_par->ttime.cycles = 1;
221 if (trig_par->ttime.mode == GPIV_TIMER_MODE__INCREMENT) {
222 printf("\tincrement in ms > ");
223 scanf("%f", &increment);
224 trig_par->ttime.increment = (RTIME)1000 * 1000 * increment;
225 } else {
226 trig_par->ttime.increment = 1;
229 ok = 1;
230 if (verbose) printf("\nLeaving initialise\n");
231 return ok;
236 static int
237 interactive_func (void
239 /*-----------------------------------------------------------------------------
242 GpivTrigPar trig_per;
243 /* GpivTrigTime ttime = trig_per.ttime; */
244 int init, start, stop, error; /* file pointer int */
245 int on = 1, off = 0, param_ok;
247 if (verbose) printf("\nEntering interactive_func\n");
248 if (!gpiv_trig_openrtfs(&init, &start, &stop, &error)) {
249 printf("\n\nFAIL IN FIFO OPEN, BYE ..... \n\n");
252 printf("\n\n\tProgram to control camera with external triggering & laser\n");
254 while (1) {
255 if(!initialise(&trig_per /* timing */)) {
256 printf("\n\nPROBLEM INITIALISING PROGRAM, BYE ..... \n\n");
257 exit(0);
260 if (trig_per.ttime.mode == 9) {
261 printf("\n\tEx(c)iting program\n\n");
262 break;
265 printf("\n\tWriting details to RT module\n\n");
267 * write the timing details to /dev/rtf/1
270 if((write(init, &trig_per.ttime, sizeof(GpivTrigTime))) < 0) {
271 printf("\n\nfail in setting Camera and Laser timing ...\n\n");
272 exit(0);
275 if((read(error, &param_ok, sizeof(int))) < 0) {
276 printf("\n\nfail in receipt of confirmation ...\n\n");
277 exit(0);
280 if (param_ok != 1) {
281 printf("\n\tInvalid parameters entered \n");
283 } else {
284 printf("\n\tParameters :\n");
285 printf("\t\tcam_acq_period: %lld\n", trig_per.ttime.cam_acq_period);
286 printf("\t\tlaser_trig_pw: %lld\n", trig_per.ttime.laser_trig_pw);
287 printf("\t\ttime2laser: %lld\n", trig_per.ttime.time2laser);
288 printf("\t\tdt: %lld\n", trig_per.ttime.dt);
289 printf("\t\tmode: %d\n", trig_per.ttime.mode);
290 printf("\t\tcycles: %d\n", trig_per.ttime.cycles);
291 printf("\t\tincrement: %d\n", (int) trig_per.ttime.increment);
293 printf("\n\n\tStart on <enter> >");
294 getchar();
295 getchar();
297 if((write(start, &on, sizeof(int))) < 0) {
298 printf("\n\nfail in starting camera and laser timing ...\n\n");
299 exit(0);
302 printf("\n\tStop on <enter> >");
303 getchar();
305 if((write(stop, &off, sizeof(int))) < 0) {
306 printf("\nfail in stopping camera and laser timing ...\n\n");
307 exit(0);
313 if (verbose) printf("\nLeaving interactive_func\n");
314 return 0;
320 main (int argc,
321 char *argv[]
323 /*-----------------------------------------------------------------------------
324 * Main program to send trigger pulses
327 int i, return_val = 0;
329 program_name = argv[0];
331 i = command_args (argc, argv);
332 if (interactive) {
333 return_val = interactive_func();
336 exit (return_val);
341 #else /* ENABLE_TRIG */
343 int
344 main(int argc,
345 char *argv[]
347 /*-----------------------------------------------------------------------------
350 g_error("%s: this is dead code as ENABLE_TRIG has not been set during building the program", argv[0]);
351 return -1;
354 #endif /* ENABLE_TRIG */