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