ViSP  2.8.0
parallelPort.cpp
1 /****************************************************************************
2  *
3  * $Id: parallelPort.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 keybord management.
36  *
37  * Author:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
48 #include <visp/vpConfig.h>
49 #include <visp/vpDebug.h>
50 
51 #if defined VISP_HAVE_PARPORT
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <iostream>
55 #include <signal.h>
56 
57 #include <visp/vpParallelPort.h>
58 #include <visp/vpParseArgv.h>
59 
60 // List of allowed command line options
61 #define GETOPTARGS "d:h"
62 
72 void usage(const char *name, const char *badparam, unsigned char &data)
73 {
74  fprintf(stdout, "\n\
75 Send a data to the parallel port.\n\
76 \n\
77 SYNOPSIS\n\
78  %s [-d <data>] [-h]\n\
79 ", name);
80 
81  fprintf(stdout, "\n\
82 OPTIONS: Default\n\
83  -d <data> %d\n\
84  Data to send to the parallel port.\n\
85  Value should be in [0:255].\n\
86 \n\
87  -h\n\
88  Print the help.\n\n",
89  data);
90 
91  if (badparam) {
92  fprintf(stderr, "ERROR: \n" );
93  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
94  }
95 
96 }
97 
109 bool getOptions(int argc, const char **argv, unsigned char &data)
110 {
111  const char *optarg;
112  int c;
113 
114  int value;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
116 
117  switch (c) {
118  case 'd': {
119  value = atoi(optarg);
120  if ((value < 0) || (value > 255)) {
121  usage(argv[0], optarg, data);
122  std::cerr << "ERROR: " << std::endl;
123  std::cerr << " Bad value \"-d " << optarg << "\""
124  << std::endl << std::endl;
125  return false;
126  }
127  else {
128  data = (unsigned char) value;
129  }
130  break;
131  }
132  case 'h': usage(argv[0], NULL, data); return false; break;
133 
134  default:
135  usage(argv[0], optarg, data); return false; break;
136  }
137  }
138 
139  if ((c == 1) || (c == -1)) {
140  // standalone param or error
141  usage(argv[0], NULL, data);
142  std::cerr << "ERROR: " << std::endl;
143  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
144  return false;
145  }
146 
147  return true;
148 }
149 
155 int
156 main(int argc, const char **argv)
157 {
158  // data to send to the parallel port
159  unsigned char data = 0;
160 
161  // Read the command line options
162  if (getOptions(argc, argv, data) == false) {
163  exit (-1);
164  }
165  try {
166 
167  vpParallelPort parport;
168 
169  printf("Send data \"%d\" to the parallel port\n", data);
170  parport.sendData(data);
171 
172 
173  }
174  catch (vpParallelPortException e) {
175  switch(e.getCode()) {
177  printf("Can't open the parallel port\n");
178  break;
180  printf("Can't close the parallel port\n");
181  break;
182  }
183  }
184  catch(...) {
185  printf("An error occurs...\n");
186  }
187  return 0;
188 }
189 #else
190 int
191 main()
192 {
193  vpTRACE("Sorry, for the moment, vpParallelPort class works only on unix...");
194  return 0;
195 }
196 #endif
#define vpTRACE
Definition: vpDebug.h:401
int getCode(void)
send the object code
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void sendData(unsigned char &data)
Error that can be emited by the vpParallelPort class and its derivates.
Parallel port management under unix.