peripherals  1.4.2
RoadNarrows Robotics Hardware Peripherals Package
xbox360diag.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: peripherals
4 //
5 // Program: xbox360diag
6 //
7 // File: xbox360diag.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2014-03-07 11:57:26 -0700 (Fri, 07 Mar 2014) $
12  * $Rev: 3594 $
13  *
14  * \brief Xbox360 diagnostic tool.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2014-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25 // Permission is hereby granted, without written agreement and without
26 // license or royalty fees, to use, copy, modify, and distribute this
27 // software and its documentation for any purpose, provided that
28 // (1) The above copyright notice and the following two paragraphs
29 // appear in all copies of the source code and (2) redistributions
30 // including binaries reproduces these notices in the supporting
31 // documentation. Substantial modifications to this software may be
32 // copyrighted by their authors and need not follow the licensing terms
33 // described here, provided that the new terms are clearly indicated in
34 // all files where they apply.
35 //
36 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
37 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
38 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
39 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
40 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
41 // THE POSSIBILITY OF SUCH DAMAGE.
42 //
43 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
44 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
45 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
46 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
47 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
48  * @EulaEnd@
49  */
50 //
51 ///////////////////////////////////////////////////////////////////////////////
52 
53 #include <sys/time.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <string.h>
58 #include <math.h>
59 #include <errno.h>
60 
61 #include <string>
62 #include <vector>
63 #include <map>
64 #include <fstream>
65 #include <sstream>
66 
67 #include "rnr/rnrconfig.h"
68 #include "rnr/log.h"
69 #include "rnr/opts.h"
70 
71 #include "rnr/hid/HID.h"
72 #include "rnr/hid/HIDXbox360.h"
73 
74 #include "version.h"
75 
76 using namespace std;
77 using namespace rnr;
78 
79 
80 //------------------------------------------------------------------------------
81 // Constants and types
82 //------------------------------------------------------------------------------
83 #define APP_EC_OK 0 ///< success exit code
84 #define APP_EC_GEN 1 ///< command-line options/arguments error exit code
85 #define APP_EC_ARGS 2 ///< command-line options/arguments error exit code
86 #define APP_EC_EXEC 4 ///< execution exit code
87 
88 
89 //------------------------------------------------------------------------------
90 // Command-Line Options
91 //------------------------------------------------------------------------------
92 static char *Argv0; ///< the command
93 static bool OptsDftThresholds = false; ///< do [not] use error thresholding
94 
95 //------------------------------------------------------------------------------
96 // Command-Line Arguments
97 //------------------------------------------------------------------------------
98 
99 
100 //------------------------------------------------------------------------------
101 // Global State Data
102 //------------------------------------------------------------------------------
103 
104 //
105 // Forward Declarations
106 //
107 
108 /*!
109  * \brief Program information.
110  */
111 static OptsPgmInfo_T AppPgmInfo =
112 {
113  // usage_args
114  NULL,
115 
116  // synopsis
117  "Xbox360 diagnostic tool.",
118 
119  // long_desc
120  "The %P tool debugs the libhid interface to Xbox360 controllers.\n"
121  " Button states are 0 or 1.\n"
122  " Triggers are 0 - 255.\n"
123  " Joysticks are -32767 - 32767.\n"
124  " LEDs and rumble motors are outputs to the Xbox360.\n"
125  " Link is determined by the libhid and is true or false.\n"
126  " Status is sent by the dongle when wireless link wth Xbox360 has changed.\n"
127  " Battery status is sent by Xbox360 occasionally.\n"
128  " Error counts:\n"
129  " ERTO consecutive Receive Error TimeOut count.\n"
130  " ER consecutive Receive Error count.\n"
131  " ES consecutive Send Error count.\n"
132  " ETOT Total Error count.",
133 
134  // diagnostics
135  NULL
136 };
137 
138 /*!
139  * \brief Command line options information.
140  */
141 static OptsInfo_T AppOptsInfo[] =
142 {
143  // --thresholds, -t
144  {
145  "thresholds", // long_opt
146  't', // short_opt
147  no_argument, // has_arg
148  true, // has_default
149  &OptsDftThresholds, // opt_addr
150  OptsCvtArgBool, // fn_cvt
151  OptsFmtBool, // fn_fmt
152  NULL, // arg_name
153  "Use error threshold defaults."
154  // opt desc
155  },
156 
157 
158  {NULL, }
159 };
160 
161 /*!
162  * \brief Main.
163  *
164  * \param argc Command-line argument count.
165  * \param argv Command-line argument list.
166  *
167  * \return Returns exit code.
168  */
169 int main(int argc, char* argv[])
170 {
171  int leftTrigger; // left trigger old value
172  int rightTrigger; // right trigger old value
173  int BButton; // B button value
174  int state; // current state of one button
175  int led; // led state
176  int rc; // return code
177 
178  // name of this process
179  Argv0 = basename(argv[0]);
180 
181  // parse input options
182  argv = OptsGet(Argv0, &PkgInfo, &AppPgmInfo, AppOptsInfo, true, &argc, argv);
183 
184  HIDXbox360 xbox;
185 
186  // turn off receive error thresholding for better diagnostics
187  if( !OptsDftThresholds )
188  {
189  xbox.setErrorThresholds(0, 0, 0);
190  }
191 
192  // connect to controller
193  if( (rc = xbox.open()) < 0 )
194  {
195  LOGERROR("Failed to open Xbox360 controller. Is it connected?");
196  return APP_EC_EXEC;
197  }
198 
199  leftTrigger = 0;
200  rightTrigger = 0;
201  BButton = 0;
202 
203  // start updated in thread
204  xbox.run();
205 
206  fprintf(stderr, " left trigger -> left rumble;"
207  " right trigger -> right rumble;"
208  " B button -> next LED pattern;"
209  " right bumper -> refresh;"
210  " X button -> exit\n");
211 
212  xbox.debugPrintHdr();
213 
214  //
215  //
216  while( xbox.isConnected() )
217  {
219 
220  if( state != leftTrigger )
221  {
222  leftTrigger = state;
223  xbox.setRumble(leftTrigger, rightTrigger);
224  }
225 
227 
228  if( state != rightTrigger )
229  {
230  rightTrigger = state;
231  xbox.setRumble(leftTrigger, rightTrigger);
232  }
233 
234  state = xbox.getFeatureVal(Xbox360FeatIdBButton);
235 
236  if( state && !BButton )
237  {
239  led = (led +1) % XBOX360_LED_PAT_NUMOF;
240  xbox.setLED(led);
241  }
242 
243  BButton = state;
244 
245  state = xbox.getFeatureVal(Xbox360FeatIdXButton);
246 
247  if( state )
248  {
249  break;
250  }
251 
253 
254  if( state )
255  {
256  fprintf(stderr, "\n\n");
257  xbox.debugPrintHdr();
258  }
259 
260  xbox.debugPrintState();
261 
262  usleep(20000);
263  }
264 
265  xbox.close();
266 
267  fprintf(stderr, "\n\nEnd Diagnostics\n");
268 
269  // exit
270  return APP_EC_OK;
271 }
#define APP_EC_OK
success exit code
Definition: xbox360diag.cxx:83
virtual int close()
Close connection to an opened USB Xbox360 controller.
Definition: HIDXbox360.cxx:230
B button [0,1].
Definition: HIDXbox360.h:631
virtual int open()
Open connection to an USB Xbox360 controller.
Definition: HIDXbox360.cxx:159
void debugPrintState()
Simple debug print state.
Xbox360 Controller C interface.
int main(int argc, char *argv[])
Main.
right trigger [0-255]
Definition: HIDXbox360.h:635
Common Human Interface Device Interface.
void setErrorThresholds(int nErrorRcvTimeoutTh=NErrorRcvTimeoutThDft, int nErrorRcvTh=NErrorRcvThDft, int nErrorTotalTh=NErrorTotalThDft)
Set error count thresholds.
Definition: HIDXbox360.h:912
void debugPrintHdr()
Simple debug print header.
int setRumble(int nLeftMot, int nRightMot)
Set the rumble instensity.
Definition: HIDXbox360.cxx:492
virtual int getFeatureVal(int iMnem)
Get the value associated with the mapped user mnemonic.
Definition: HIDXbox360.h:720
Xbox360 controller HID input class.
Definition: HIDXbox360.h:651
virtual bool isConnected()
Query if HID is connected.
Definition: HID.h:167
LED pattern feature.
Definition: HIDXbox360.h:642
static const PkgInfo_T PkgInfo
Definition: version.h:45
int setLED(int nPattern)
Set the LED pattern.
Definition: HIDXbox360.cxx:623
Package version information.
static bool OptsDftThresholds
do [not] use error thresholding
Definition: xbox360diag.cxx:93
#define APP_EC_EXEC
execution exit code
Definition: xbox360diag.cxx:86
virtual int run(float hz=30.0)
Create and run USB update in thread.
Definition: HIDXbox360.cxx:292
right bump (right shoulder) [0,1]
Definition: HIDXbox360.h:628
static char * Argv0
the command
Definition: xbox360diag.cxx:92
#define XBOX360_LED_PAT_NUMOF
number of patterns
Definition: HIDXbox360.h:586
static OptsPgmInfo_T AppPgmInfo
Program information.
RoadNarrows Robotics standard namespace.
Definition: HID.h:65
X button [0,1].
Definition: HIDXbox360.h:632
left trigger [0-255]
Definition: HIDXbox360.h:634
static OptsInfo_T AppOptsInfo[]
Command line options information.