int main () { Joystick joystick; std::cout << joystick.init(); while(1) { std::cout << "Update:"; std::cout << joystick.update(); std::cout << " Axis0:"; std::cout << joystick.getAxis(0); std::cout << " Axis1:"; std::cout << joystick.getAxis(1); std::cout << " Button0:"; std::cout << joystick.getButton(0); std::cout << "\n"; } return 0; }
int w_Joystick_getAxis(lua_State *L) { Joystick *j = luax_checkjoystick(L, 1); int axisindex = luaL_checkint(L, 2) - 1; lua_pushnumber(L, j->getAxis(axisindex)); return 1; }
int main() { Joystick *myStick = new Joystick("/dev/input/js0"); while (true) { for (int i = 0; i<myStick->numberOfAxis(); i++) { printf("%.2f\t", myStick->getAxis(i)); } // for (int i = 0; i<myStick->numberOfButtons(); i++) { // printf("%d\t", myStick->getButton(i)); // } printf("\n"); usleep(10000); } }
/* * Callback function for enumerating the axes on a joystick. */ BOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE * pdidoi, void * pJoystickAsVoid ) { BW_GUARD; Joystick * pJoystick = reinterpret_cast<Joystick *>( pJoystickAsVoid ); DIPROPRANGE diprg; diprg.diph.dwSize = sizeof(DIPROPRANGE); diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); diprg.diph.dwHow = DIPH_BYOFFSET; diprg.diph.dwObj = pdidoi->dwOfs; // Specify the enumerated axis diprg.lMin = -DIRECT_INPUT_AXIS_MAX; diprg.lMax = +DIRECT_INPUT_AXIS_MAX; // Set the range for the axis if( FAILED( pJoystick->pDIJoystick()-> SetProperty( DIPROP_RANGE, &diprg.diph ) ) ) { return DIENUM_STOP; } // Set the UI to reflect what axes the joystick supports AxisEvent::Axis amap = AxisEvent::NUM_AXES; switch( pdidoi->dwOfs ) { // these are PlayStation mappings case DIJOFS_X: amap = AxisEvent::AXIS_LX; break; case DIJOFS_Y: amap = AxisEvent::AXIS_LY; break; case DIJOFS_Z: amap = AxisEvent::AXIS_RX; break; // case DIJOFS_RX: amap = AxisEvent::AXIS_; break; // case DIJOFS_RY: amap = AxisEvent::AXIS_; break; case DIJOFS_RZ: amap = AxisEvent::AXIS_RY; break; // case DIJOFS_SLIDER(0): amap = AxisEvent::AXIS_; break; // case DIJOFS_SLIDER(1): amap = AxisEvent::AXIS_; break; } if (amap != AxisEvent::NUM_AXES) pJoystick->getAxis( amap ).enabled( true ); return DIENUM_CONTINUE; }