Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <time.h>
00012
00013 #include <iostream>
00014
00015 #include "linux-input/linux-input.h"
00016 #include "perf/perf.h"
00017
00018
00019
00020
00021
00022
00023
00024
00025 static int
00026 getIntDigits
00027 (
00028 IN int x
00029 )
00030 throw()
00031 {
00032 if (x <= 9)
00033 return 1;
00034
00035 int nDigits = 1;
00036 while (x > 10) {
00037 x /= 10;
00038 nDigits++;
00039 }
00040
00041 return nDigits;
00042 }
00043
00044
00045
00046 static int
00047 getPotDigits
00048 (
00049 IN int min,
00050 IN int max
00051 )
00052 throw()
00053 {
00054 int minDigits = 0;
00055 if (min < 0) {
00056 minDigits = getIntDigits(-min) + 1;
00057 }
00058 int maxDigits = getIntDigits(max);
00059
00060 if (minDigits > maxDigits) {
00061 return minDigits;
00062 }
00063 return maxDigits;
00064 }
00065
00066
00067 static void
00068 printPot
00069 (
00070 IN int idx,
00071 IN const gamepad::pot_value_t& pv
00072 )
00073 throw()
00074 {
00075 int valDigits = getPotDigits(pv.minSeen, pv.maxSeen);
00076
00077
00078
00079 const int bufsize = 64;
00080 char format[bufsize];
00081
00082
00083 snprintf(format, bufsize, "%%%dd ", valDigits);
00084
00085
00086
00087 printf(format, pv.value);
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 }
00105
00106
00107
00108 static void
00109 doTest
00110 (
00111 void
00112 )
00113 {
00114
00115 bool wantForceFeedbackEvenThoughExtremelyDangerous = true;
00116 smart_ptr<gamepad::SourceDeviceFactory> factory =
00117 gamepad::getLinuxInputSourceDeviceFactory(
00118 wantForceFeedbackEvenThoughExtremelyDangerous);
00119 ASSERT_THROW(factory,
00120 "failed to create linux input source device factory");
00121
00122
00123 int nDevices = factory->getCount();
00124 DPRINTF("Found %d linux joystick source devices", nDevices);
00125 ASSERT_THROW(nDevices > 0, "Bad or no joystick devices found?");
00126
00127 #if 1
00128
00129 for (int i = 0; i < nDevices; ++i) {
00130 smart_ptr<gamepad::SourceDevice> device =
00131 factory->getSourceDevice(i);
00132 ASSERT_THROW(device, "Should have a device here");
00133
00134
00135 DPRINTF("Device %d: '%s'", i, device->getPublicName());
00136 DPRINTF(" unique ID: '%s'", device->getUniqueId());
00137
00138 DPRINTF(" state: %d", device->getState());
00139 DPRINTF(" total potentiometers: %d", device->getNumPots());
00140 DPRINTF(" total buttons: %d", device->getNumButtons());
00141 }
00142 #endif
00143
00144
00145 smart_ptr<gamepad::SourceDevice> device = factory->getSourceDevice(0);
00146 ASSERT_THROW(device, "should have a device");
00147
00148
00149 const int desiredHz = 100;
00150 long msPoll = 1000 / desiredHz;
00151 DPRINTF("Want sampling at %d Hz, so polling every %ld ms",
00152 desiredHz, msPoll);
00153 ASSERT_THROW(desiredHz > 1, "Need a higher sampling frequency!");
00154 struct timespec ts;
00155 ts.tv_sec = 0;
00156 ts.tv_nsec = msPoll * 1000 * 1000;
00157
00158
00159 const int connectedHz = 5;
00160 int checkCounter = desiredHz / connectedHz;
00161 DPRINTF("Checking connection status every %d iterations",
00162 checkCounter);
00163 int counter = 0;
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 int nCols = 100;
00178 int nCharsPerPot = (nCols / device->getNumPots()) - 1;
00179 if (nCharsPerPot > 6) {
00180 nCharsPerPot = 6;
00181 }
00182 if (nCharsPerPot < 2) {
00183 nCharsPerPot = 2;
00184 }
00185
00186
00187
00188
00189
00190 DPRINTF("\n\nDetach the device to stop the program!\n");
00191 while (gamepad::eDevice_Detached != device->getState()) {
00192 nanosleep(&ts, NULL);
00193
00194 counter++;
00195 if (counter >= checkCounter) {
00196 counter = 0;
00197 factory->getCount();
00198 }
00199
00200 device->poll();
00201 printf("\r");
00202 for (int i = 0; i < device->getNumPots(); ++i) {
00203 const gamepad::pot_value_t& pv = device->getPotValue(i);
00204 printPot(i, pv);
00205
00206 }
00207
00208 printf(" pressed:");
00209 bool needLinefeed = false;
00210 for (int i = 0; i < device->getNumButtons(); ++i) {
00211 if (gamepad::eButtonWasPushed & device->getButtonValue(i)) {
00212 printf(" b%d", i);
00213 needLinefeed = true;
00214 }
00215 }
00216 if (needLinefeed) {
00217 printf("\n");
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 }
00236 printf("\n");
00237 DPRINTF("Device appears to be detached--exiting!");
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 int
00249 main
00250 (
00251 IN int argc,
00252 IN const char * argv[]
00253 )
00254 {
00255 int retval = 0;
00256
00257 try {
00258 perf::Timer timer("overall timer");
00259
00260 doTest();
00261
00262 } catch (std::exception& e) {
00263 DPRINTF("EXCEPTION: %s", e.what());
00264 retval = 1;
00265 }
00266
00267 perf::dumpTimingSummary(std::cerr);
00268
00269 return retval;
00270 }
00271
00272