//* Example illustrating Joystick access in Windows *

#include <windows.h> #include <limits.h> // We need winmm.h but it needs some more of windows.h // Add to the linked libraries list: winmm.lib // We need to print debug information in this particular example #include <iostream> #include <iomanip> using namespace std; int main() { bool keeprunning = true; while (keeprunning) { if (joyGetNumDevs()==0) { cerr << "Please connect a joystick" << endl; Sleep(5000); continue; } JOYCAPS jc; if (joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc))!=JOYERR_NOERROR) { cerr << "Please connect a compatible joystick" << endl; Sleep(1000); continue; } const unsigned int naxes = jc.wNumAxes; const unsigned int nbuts = jc.wNumButtons; const int MID_VALUE =int(USHRT_MAX/2); JOYINFOEX lastJoyState; lastJoyState.dwSize=sizeof(lastJoyState); lastJoyState.dwFlags=JOY_RETURNALL | JOY_RETURNPOVCTS | JOY_RETURNCENTERED | JOY_USEDEADZONE; int cur_b = 0; int cur_x = 0; int cur_y = 0; int cur_z = 0; int cur_r = 0; int cur_u = 0; int cur_v = 0; int cur_h = 0; int old_b = 0; int old_x = 0; int old_y = 0; int old_z = 0; int old_r = 0; int old_u = 0; int old_v = 0; int old_h = 0; int cnt = 0; while(keeprunning) { if (joyGetPosEx(JOYSTICKID1, &lastJoyState) != JOYERR_NOERROR) { cerr << "Please reconnect the joystick" << endl; Sleep(1000); break; // exit to the outer loop and check for a new joystick connection } old_b = cur_b; old_x = cur_x; old_y = cur_y; old_z = cur_z; old_r = cur_r; old_u = cur_u; old_v = cur_v; old_h = cur_h; cur_b = lastJoyState.dwButtons; cur_x = int(lastJoyState.dwXpos)-MID_VALUE; cur_y = int(lastJoyState.dwYpos)-MID_VALUE; cur_z = int(lastJoyState.dwZpos)-MID_VALUE; cur_r = int(lastJoyState.dwRpos)-MID_VALUE; cur_u = int(lastJoyState.dwUpos)-MID_VALUE; cur_v = int(lastJoyState.dwVpos)-MID_VALUE; cur_h = lastJoyState.dwPOV; cnt++; // to force display every so many samples even if no change if (cnt>91||cur_b!=old_b||cur_x!=old_x||cur_y!=old_y||cur_z!=old_z||cur_r!=old_r||cur_u!=old_u||cur_v!=old_v) { cout<<"B: "; cout<<static_cast<bool>(cur_b&0x200); // or check if ==0 or !=0 inside an IF statement... cout<<static_cast<bool>(cur_b&0x100); cout<<static_cast<bool>(cur_b&0x080); cout<<static_cast<bool>(cur_b&0x040); cout<<static_cast<bool>(cur_b&0x020); cout<<static_cast<bool>(cur_b&0x010); cout<<static_cast<bool>(cur_b&0x008); cout<<static_cast<bool>(cur_b&0x004); cout<<static_cast<bool>(cur_b&0x002); cout<<static_cast<bool>(cur_b&0x001); if (naxes>0) cout << " X:" << setw(7) << cur_x; if (naxes>1) cout << " Y:" << setw(7) << cur_y; if (naxes>2) cout << " Z:" << setw(7) << cur_z; if (naxes>3) cout << " R:" << setw(7) << cur_r; if (naxes>4) cout << " U:" << setw(7) << cur_u; if (naxes>5) cout << " V:" << setw(7) << cur_v; if (naxes>6) cout << " more skipped"; if (cur_h<=36000) cout << " POW: " << setw(7) << cur_h; cout << endl; Sleep(90); // wait at least 100ms between sending/outputing samples cnt=0; } if (cur_b==15) { cout << "End program button sequence initiated" << endl; keeprunning=false; } Sleep(10); // wait 10ms for the next sample } } return(0); }