ViSP
ringLight.cpp
1 /****************************************************************************
2  *
3  * $Id: ringLight.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Example of ring light control.
36  *
37  * Author:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
50 #include <visp/vpConfig.h>
51 #include <visp/vpDebug.h>
52 #include <cmath> // std::fabs
53 #include <limits> // numeric_limits
54 #if defined VISP_HAVE_PARPORT
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <iostream>
58 
59 #include <visp/vpRingLight.h>
60 #include <visp/vpParseArgv.h>
61 #include <visp/vpTime.h>
62 
63 // List of allowed command line options
64 #define GETOPTARGS "d:hn:ot:"
65 
76 void usage(const char *name, const char *badparam, int nsec, double nmsec)
77 {
78  fprintf(stdout, "\n\
79 Send a pulse to activate the ring light or turn on the ring light \n\
80 during %d s.\n\
81 \n\
82 By default, that means without parameters, send a pulse which duration\n\
83 is fixed by the harware. To control the duration of the pulse, use \n\
84 \"-t <pulse width in ms>\" option. To turn on the light permanently, \n\
85 use \"-o -n <on duration in second>]\"\n \
86 \n\
87 SYNOPSIS\n\
88  %s [-o] [-n <on duration in second>] [-t <pulse width in ms>] [-h]\n\
89 ", nsec, name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93 \n\
94  -o\n\
95  Turn the ring light on during %d s.\n\
96  If this option is not set, send a short pulse\n\
97  to activate the light.\n\
98 \n\
99  -t %%g : <pulse width in ms> %g\n\
100  Pulse width in milli-second.\n\
101  Send a pulse which duration is fixed by this parameter.\n\
102  Without this option, the pulse width is fixed by the \n\
103  harware.\n\
104 \n\
105  -n %%d : <on duration in second> %d\n\
106  Time in second while the ring light is turned on.\n\
107  This option is to make into realtion with option \"-o\".\n\
108 \n\
109  -h\n\
110  Print the help.\n\n", nsec, nmsec, nsec);
111 
112  if (badparam) {
113  fprintf(stderr, "ERROR: \n" );
114  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
115  }
116 
117 }
118 
132 bool getOptions(int argc, const char **argv, bool &on, int &nsec, double &nmsec)
133 {
134  const char *optarg;
135  int c;
136 
137  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
138 
139  switch (c) {
140  case 'n': nsec = atoi(optarg); break;
141  case 'o': on = true; break;
142  case 't': nmsec = atof(optarg); break;
143  case 'h': usage(argv[0], NULL, nsec, nmsec); return false; break;
144 
145  default:
146  usage(argv[0], optarg, nsec, nmsec); return false; break;
147  }
148  }
149 
150  if ((c == 1) || (c == -1)) {
151  // standalone param or error
152  usage(argv[0], NULL, nsec, nmsec);
153  std::cerr << "ERROR: " << std::endl;
154  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
155  return false;
156  }
157 
158  return true;
159 }
160 
166 int
167 main(int argc, const char **argv)
168 {
169  try {
170  bool on = false;
171  int nsec = 5; // Time while the ring light is turned on
172  double nmsec = 0; // Pulse duration
173 
174  // Read the command line options
175  if (getOptions(argc, argv, on, nsec, nmsec) == false) {
176  exit (-1);
177  }
178 
179  vpRingLight light;
180 
181  //if (nmsec == 0.)
182  if (std::fabs(nmsec) <= std::numeric_limits<double>::epsilon())
183  light.pulse();
184  else
185  light.pulse(nmsec);
186 
187  if (on) {
188  printf("Turn on ring light\n");
189  light.on(); // Turn the ring light on
190  vpTime::wait(nsec * 1000); // Wait 5 s
191  light.off(); // and then turn the ring light off
192  }
193  else {
194  printf("Send a pulse to activate the ring light\n");
195  light.pulse();
196  }
197  }
198  catch (vpParallelPortException e) {
199  switch(e.getCode()) {
201  printf("Can't open the parallel port to access to the ring light device\n");
202  break;
204  printf("Can't close the parallel port\n");
205  break;
206  }
207  }
208  catch(...) {
209  printf("An error occurs...\n");
210  }
211  return 0;
212 }
213 #else
214 int
215 main()
216 {
217  vpTRACE("Sorry, for the moment, vpRingLight class works only on unix...");
218  return 0;
219 }
220 #endif
#define vpTRACE
Definition: vpDebug.h:418
int getCode(void)
send the object code
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
Ring light management under unix.
Definition: vpRingLight.h:115
Error that can be emited by the vpParallelPort class and its derivates.