peripherals  1.4.2
RoadNarrows Robotics Hardware Peripherals Package
Xbox360 Unit Tests

Fine-grained testing of xbox360 library. More...

Functions

static int testXboxSanity ()
 Sanity test Xbox interface. More...
 
static int testXboxInput ()
 Test Xbox input interface. More...
 
static int testXboxRumble ()
 Tactically test Xbox rumble interface. More...
 
static int testXboxLED ()
 Visually test Xbox LED interface. More...
 
 TEST (Xbox360, XboxSanity)
 Sanity test Xbox360 controller interface. More...
 
 TEST (Xbox360, XboxInput)
 Visually test Xbox360 controller input interface. More...
 
 TEST (Xbox360, XboxRumble)
 Tactually test Xbox360 controller rumble interface. More...
 
 TEST (Xbox360, XboxLED)
 Visually test Xbox360 controller LED interface. More...
 

Detailed Description

Fine-grained testing of xbox360 library.

Function Documentation

TEST ( Xbox360  ,
XboxSanity   
)

Sanity test Xbox360 controller interface.

The Test:
Construct HIDXbox360 object.
Open.
Ping.
Close.
Destroy HIDXbox360 object.

Definition at line 338 of file ut-HIDXbox360.cxx.

References testXboxSanity().

Referenced by testMotDummyGetAttrSpeed(), testMotDummySetSpeed(), and testRoboteqSmallSanity().

339 {
340  EXPECT_TRUE( testXboxSanity() == 0 );
341 }
static int testXboxSanity()
Sanity test Xbox interface.
TEST ( Xbox360  ,
XboxInput   
)

Visually test Xbox360 controller input interface.

The Test:
Construct HIDXbox360 object.
Open.
User interaction.
Close.
Destroy HIDXbox360 object.

Definition at line 354 of file ut-HIDXbox360.cxx.

References testXboxInput().

355 {
356  EXPECT_TRUE( testXboxInput() == 0 );
357 }
static int testXboxInput()
Test Xbox input interface.
TEST ( Xbox360  ,
XboxRumble   
)

Tactually test Xbox360 controller rumble interface.

The Test:
Construct HIDXbox360 object.
Open.
User interaction.
Close.
Destroy HIDXbox360 object.

Definition at line 369 of file ut-HIDXbox360.cxx.

References testXboxRumble().

370 {
371  EXPECT_TRUE( testXboxRumble() == 0 );
372 }
static int testXboxRumble()
Tactically test Xbox rumble interface.
TEST ( Xbox360  ,
XboxLED   
)

Visually test Xbox360 controller LED interface.

The Test:
Construct HIDXbox360 object.
Open.
User interaction.
Close.
Destroy HIDXbox360 object.

Definition at line 384 of file ut-HIDXbox360.cxx.

References testXboxLED().

385 {
386  EXPECT_TRUE( testXboxLED() == 0 );
387 }
static int testXboxLED()
Visually test Xbox LED interface.
static int testXboxInput ( )
static

Test Xbox input interface.

Returns
Returns 0 if test succeeds, else returns < 0.

Definition at line 113 of file ut-HIDXbox360.cxx.

References rnr::HIDInput::assocFeature(), rnr::HIDXbox360::debugPrintHdr(), rnr::HIDXbox360::debugPrintState(), rnr::HIDXbox360::getFeatureVal(), HID_BTTN_DOWN, rnr::HIDInput::MNEM_START, rnr::HIDXbox360::open(), rnr::HIDXbox360::update(), rnr::Xbox360FeatIdXButton, rnr::Xbox360FeatIdYButton, and XBOX_LIBUSB_DEBUG_WARN.

Referenced by TEST().

114 {
115  int bttnSuccess = HIDInput::MNEM_START;
116  int bttnFail = bttnSuccess + 1;
117  int rc;
118 
119  // manually set rnr log level
120  LOG_SET_THRESHOLD(LOG_LEVEL_DIAG3);
121 
123 
124  if( (rc = xbox.open()) < 0 )
125  {
126  LOGERROR("Failed to open Xbox360 controller.");
127  return -1;
128  }
129 
130  xbox.assocFeature(Xbox360FeatIdXButton, bttnSuccess);
131  xbox.assocFeature(Xbox360FeatIdYButton, bttnFail);
132 
133  fprintf(stderr,
134  "\nTesting Xbox360 Input Interface.\n"
135  " Press the Xbox 'X' button to terminate test with success.\n"
136  " Press the Xbox 'Y' button to terminate test with failure.\n\n");
137 
138  xbox.debugPrintHdr();
139 
140  while( true )
141  {
142  if( (rc = xbox.update()) < 0 )
143  {
144  LOGERROR("Failed to update Xbox360 controller state.");
145  break;
146  }
147 
148  xbox.debugPrintState();
149 
150  if( xbox.getFeatureVal(bttnSuccess) == HID_BTTN_DOWN )
151  {
152  rc = 0;
153  break;
154  }
155  else if( xbox.getFeatureVal(bttnFail) == HID_BTTN_DOWN )
156  {
157  rc = -1;
158  break;
159  }
160  }
161 
162  fprintf(stderr, "\n\n");
163 
164  return rc;
165 }
Y button [0,1].
Definition: HIDXbox360.h:633
#define HID_BTTN_DOWN
button/key state is down or pressed
Definition: HID.h:53
Xbox360 controller HID input class.
Definition: HIDXbox360.h:651
#define XBOX_LIBUSB_DEBUG_WARN
log libusb warnings and errors
Definition: HIDXbox360.h:243
X button [0,1].
Definition: HIDXbox360.h:632
static int testXboxLED ( )
static

Visually test Xbox LED interface.

Returns
Returns 0 if test succeeds, else returns < 0.

Definition at line 248 of file ut-HIDXbox360.cxx.

References rnr::HIDInput::assocFeature(), rnr::HIDXbox360::debugPrintHdr(), rnr::HIDXbox360::debugPrintState(), rnr::HIDXbox360::getFeatureVal(), HID_BTTN_DOWN, rnr::HIDInput::MNEM_START, rnr::HIDXbox360::open(), rnr::HIDXbox360::setLED(), rnr::HIDXbox360::update(), XBOX360_LED_PAT_NUMOF, rnr::Xbox360FeatIdLeftBump, rnr::Xbox360FeatIdRightBump, rnr::Xbox360FeatIdXButton, rnr::Xbox360FeatIdYButton, and XBOX_LIBUSB_DEBUG_WARN.

Referenced by TEST().

249 {
250  int bttnSuccess = HIDInput::MNEM_START;
251  int bttnFail = bttnSuccess + 1;
252  int nLeftBump = 0;
253  int nRightBump = 0;
254  int nPattern = 0;
255  int rc;
256 
257  // manually set rnr log level
258  LOG_SET_THRESHOLD(LOG_LEVEL_DIAG3);
259 
261 
262  if( (rc = xbox.open()) < 0 )
263  {
264  LOGERROR("Failed to open Xbox360 controller.");
265  return -1;
266  }
267 
268  xbox.assocFeature(Xbox360FeatIdXButton, bttnSuccess);
269  xbox.assocFeature(Xbox360FeatIdYButton, bttnFail);
270 
271  fprintf(stderr,
272  "\nTesting Xbox360 LED Interface.\n"
273  " Press left (right) bumper to increment (decrement) the LED pattern.\n"
274  " Press the Xbox 'X' button to terminate test with success.\n"
275  " Press the Xbox 'Y' button to terminate test with failure.\n\n");
276 
277  xbox.debugPrintHdr();
278 
279  while( true )
280  {
281  if( (rc = xbox.update()) < 0 )
282  {
283  LOGERROR("Failed to update Xbox360 controller state.");
284  break;
285  }
286 
287  xbox.debugPrintState();
288 
289  if( xbox.getFeatureVal(bttnSuccess) == HID_BTTN_DOWN )
290  {
291  rc = 0;
292  break;
293  }
294  else if( xbox.getFeatureVal(bttnFail) == HID_BTTN_DOWN )
295  {
296  rc = -1;
297  break;
298  }
299  else if( nLeftBump != xbox.getFeatureVal(Xbox360FeatIdLeftBump) )
300  {
301  nLeftBump = xbox.getFeatureVal(Xbox360FeatIdLeftBump);
302 
303  if( nLeftBump )
304  {
305  nPattern = (nPattern + 1) % XBOX360_LED_PAT_NUMOF;
306  xbox.setLED(nPattern);
307  }
308  }
309  else if( nRightBump != xbox.getFeatureVal(Xbox360FeatIdRightBump) )
310  {
311  nRightBump = xbox.getFeatureVal(Xbox360FeatIdRightBump);
312 
313  if( nRightBump )
314  {
315  nPattern = (nPattern-1 + XBOX360_LED_PAT_NUMOF)%XBOX360_LED_PAT_NUMOF;
316  xbox.setLED(nPattern);
317  }
318  }
319  }
320 
321  fprintf(stderr, "\n\n");
322 
323  return rc;
324 }
Y button [0,1].
Definition: HIDXbox360.h:633
#define HID_BTTN_DOWN
button/key state is down or pressed
Definition: HID.h:53
Xbox360 controller HID input class.
Definition: HIDXbox360.h:651
left bump (left shoulder) [0,1]
Definition: HIDXbox360.h:627
#define XBOX_LIBUSB_DEBUG_WARN
log libusb warnings and errors
Definition: HIDXbox360.h:243
right bump (right shoulder) [0,1]
Definition: HIDXbox360.h:628
#define XBOX360_LED_PAT_NUMOF
number of patterns
Definition: HIDXbox360.h:586
X button [0,1].
Definition: HIDXbox360.h:632
static int testXboxRumble ( )
static

Tactically test Xbox rumble interface.

Returns
Returns 0 if test succeeds, else returns < 0.

Definition at line 172 of file ut-HIDXbox360.cxx.

References rnr::HIDInput::assocFeature(), rnr::HIDXbox360::debugPrintHdr(), rnr::HIDXbox360::debugPrintState(), rnr::HIDXbox360::getFeatureVal(), HID_BTTN_DOWN, rnr::HIDInput::MNEM_START, rnr::HIDXbox360::open(), rnr::HIDXbox360::setRumble(), rnr::HIDXbox360::update(), rnr::Xbox360FeatIdLeftRumble, rnr::Xbox360FeatIdLeftTrigger, rnr::Xbox360FeatIdRightRumble, rnr::Xbox360FeatIdRightTrigger, rnr::Xbox360FeatIdXButton, rnr::Xbox360FeatIdYButton, and XBOX_LIBUSB_DEBUG_WARN.

Referenced by TEST().

173 {
174  int bttnSuccess = HIDInput::MNEM_START;
175  int bttnFail = bttnSuccess + 1;
176  int nLeftTrigger = 0;
177  int nRightTrigger = 0;
178  int nLeftRumble = 0;
179  int nRightRumble = 0;
180  int rc;
181 
182  // manually set rnr log level
183  LOG_SET_THRESHOLD(LOG_LEVEL_DIAG3);
184 
186 
187  if( (rc = xbox.open()) < 0 )
188  {
189  LOGERROR("Failed to open Xbox360 controller.");
190  return -1;
191  }
192 
193  xbox.assocFeature(Xbox360FeatIdXButton, bttnSuccess);
194  xbox.assocFeature(Xbox360FeatIdYButton, bttnFail);
195 
196  fprintf(stderr,
197  "\nTesting Xbox360 Rumble Interface.\n"
198  " Squeeze left (right) trigger for left (right) rumble motor.\n"
199  " Press the Xbox 'X' button to terminate test with success.\n"
200  " Press the Xbox 'Y' button to terminate test with failure.\n\n");
201 
202  xbox.debugPrintHdr();
203 
204  while( true )
205  {
206  if( (rc = xbox.update()) < 0 )
207  {
208  LOGERROR("Failed to update Xbox360 controller state.");
209  break;
210  }
211 
212  xbox.debugPrintState();
213 
214  if( xbox.getFeatureVal(bttnSuccess) == HID_BTTN_DOWN )
215  {
216  rc = 0;
217  break;
218  }
219  else if( xbox.getFeatureVal(bttnFail) == HID_BTTN_DOWN )
220  {
221  rc = -1;
222  break;
223  }
224  else
225  {
226  nLeftTrigger = xbox.getFeatureVal(Xbox360FeatIdLeftTrigger);
227  nRightTrigger = xbox.getFeatureVal(Xbox360FeatIdRightTrigger);
228  nLeftRumble = xbox.getFeatureVal(Xbox360FeatIdLeftRumble);
229  nRightRumble = xbox.getFeatureVal(Xbox360FeatIdRightRumble);
230 
231  if( (nLeftTrigger != nLeftRumble) || (nRightTrigger != nRightRumble) )
232  {
233  xbox.setRumble(nLeftTrigger, nRightTrigger);
234  }
235  }
236  }
237 
238  fprintf(stderr, "\n\n");
239 
240  return rc;
241 }
right trigger [0-255]
Definition: HIDXbox360.h:635
Y button [0,1].
Definition: HIDXbox360.h:633
#define HID_BTTN_DOWN
button/key state is down or pressed
Definition: HID.h:53
right hi-freq rumble motor [0-255]
Definition: HIDXbox360.h:641
Xbox360 controller HID input class.
Definition: HIDXbox360.h:651
#define XBOX_LIBUSB_DEBUG_WARN
log libusb warnings and errors
Definition: HIDXbox360.h:243
left low-freq rumble motor [0-255]
Definition: HIDXbox360.h:640
X button [0,1].
Definition: HIDXbox360.h:632
left trigger [0-255]
Definition: HIDXbox360.h:634
static int testXboxSanity ( )
static

Sanity test Xbox interface.

Returns
Returns 0 if test succeeds, else returns < 0.

Definition at line 72 of file ut-HIDXbox360.cxx.

References rnr::HIDXbox360::close(), rnr::HIDInput::isConnected(), rnr::HIDXbox360::open(), rnr::HIDXbox360::ping(), and XBOX_LIBUSB_DEBUG_WARN.

Referenced by TEST().

73 {
74  HIDXbox360 *pXbox;
75  int rc;
76 
77  // manually set rnr log level
78  LOG_SET_THRESHOLD(LOG_LEVEL_DIAG3);
79 
81 
82  if( (rc = pXbox->open()) < 0 )
83  {
84  LOGERROR("Failed to open Xbox360 controller.");
85  }
86 
87  else if( !pXbox->ping() )
88  {
89  LOGERROR("Failed to ping Xbox360 controller.");
90  rc = -1;
91  }
92 
93  else
94  {
95  rc = 0;
96  }
97 
98  if( pXbox->isConnected() )
99  {
100  pXbox->close();
101  }
102 
103  delete pXbox;
104 
105  return rc;
106 }
virtual int close()
Close connection to an opened USB Xbox360 controller.
Definition: HIDXbox360.cxx:230
virtual int open()
Open connection to an USB Xbox360 controller.
Definition: HIDXbox360.cxx:159
virtual bool ping()
Ping device if it is connected and is responding.
Definition: HIDXbox360.cxx:414
Xbox360 controller HID input class.
Definition: HIDXbox360.h:651
virtual bool isConnected()
Query if HID is connected.
Definition: HID.h:167
#define XBOX_LIBUSB_DEBUG_WARN
log libusb warnings and errors
Definition: HIDXbox360.h:243