static int initializePad(int port, int slot) { int ret; int modes; int i; waitPadReady(port, slot); modes = padInfoMode(port, slot, PAD_MODETABLE, -1); if (modes > 0) { for (i = 0; i < modes; i++) { } } if (modes == 0) { return 1; } i = 0; do { if (padInfoMode(port, slot, PAD_MODETABLE, i) == PAD_TYPE_DUALSHOCK) break; i++; } while (i < modes); if (i >= modes) { return 1; } ret = padInfoMode(port, slot, PAD_MODECUREXID, 0); if (ret == 0) { return 1; } padSetMainMode(port, slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK); waitPadReady(port, slot); padInfoPressMode(port, slot); waitPadReady(port, slot); padEnterPressMode(port, slot); waitPadReady(port, slot); actuators = padInfoAct(port, slot, -1, 0); if (actuators != 0) { actAlign[0] = 0; actAlign[1] = 1; actAlign[2] = 0xff; actAlign[3] = 0xff; actAlign[4] = 0xff; actAlign[5] = 0xff; waitPadReady(port, slot); padSetActAlign(port, slot, actAlign); } else { //printf("Did not find any actuators.\n"); } return 1; }
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) { int i, ret; int port, slot; int rjoy_h, rjoy_v; int ljoy_h, ljoy_v; struct padButtonStatus buttons; u32 paddata; u32 old_pad; u32 new_pad; port = joystick->hwdata->port; slot = joystick->hwdata->slot; if (wait_pad(port, slot, 10) < 0) { /* no pad information available */ return; } if (joystick->hwdata->deviceid < 0) { int id; /* see if we can probe the MODECURID now */ id = padInfoMode(port, slot, PAD_MODECURID, 0); if (id != 0) { int ext; ext = padInfoMode(port, slot, PAD_MODECUREXID, 0); if (ext != 0) { id = ext; } if (id == PAD_TYPE_DIGITAL) { printf("SDL_Joystick: digital pad detected\n"); } else if (id == PAD_TYPE_DUALSHOCK) { printf("SDL_Joystick: dualshock detected\n"); } else { printf("SDL_Joystick: unknown identifier %d detected\n", id); } if (id == PAD_TYPE_DIGITAL || id == PAD_TYPE_DUALSHOCK) { ret = padSetMainMode(port, slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK); if (ret == 1) { printf("JoystickInit: Request received\n"); } else { printf("JoystickInit: padSetMainMode failed\n"); } } joystick->hwdata->deviceid = id; if (wait_pad(port, slot, 10) < 0) { /* no pad information available */ return; } } } ret = padRead(port, slot, &buttons); if (ret != 0) { int changed, hat; old_pad = joystick->hwdata->prev_buttons; paddata = 0xffff ^ buttons.btns; new_pad = paddata; changed = paddata ^ old_pad; joystick->hwdata->prev_buttons = paddata; hat = SDL_HAT_CENTERED; if (new_pad & PAD_LEFT) { hat = hat | SDL_HAT_LEFT; } if (new_pad & PAD_RIGHT) { hat = hat | SDL_HAT_RIGHT; } if (new_pad & PAD_DOWN) { hat = hat | SDL_HAT_DOWN; } if (new_pad & PAD_UP) { hat = hat | SDL_HAT_UP; } for (i=0; i<MAX_BUTTONS; i++) { if (changed & sdl_buttons[i]) { int status = (new_pad & sdl_buttons[i]) ? SDL_PRESSED : SDL_RELEASED; SDL_PrivateJoystickButton(joystick, i, status); } } if (changed & (PAD_LEFT|PAD_RIGHT|PAD_UP|PAD_DOWN)) { SDL_PrivateJoystickHat(joystick, 0, hat); } /* now do axis */ ljoy_h = buttons.ljoy_h - 128; ljoy_v = buttons.ljoy_v - 128; rjoy_h = buttons.rjoy_h - 128; rjoy_v = buttons.rjoy_v - 128; /* printf("rjoy_h %d rjoy_v %d ljoy_h %d ljoy_v %d (%d, %d, %d, %d)\n", rjoy_h, rjoy_v, ljoy_h, ljoy_v, joystick->hwdata->prev_rjoy_h, joystick->hwdata->prev_rjoy_v, joystick->hwdata->prev_ljoy_h, joystick->hwdata->prev_ljoy_v); */ /* left analog stick */ if (abs(joystick->hwdata->prev_ljoy_h - ljoy_h) > AXIS_THRESHOLD) { SDL_PrivateJoystickAxis(joystick, 0, ljoy_h * 127); joystick->hwdata->prev_ljoy_h = ljoy_h; } if (abs(joystick->hwdata->prev_ljoy_v - ljoy_v) > AXIS_THRESHOLD) { SDL_PrivateJoystickAxis(joystick, 1, ljoy_v * 127); joystick->hwdata->prev_ljoy_v = ljoy_v; } /* right analog stick */ if (abs(joystick->hwdata->prev_rjoy_h - rjoy_h) > AXIS_THRESHOLD) { SDL_PrivateJoystickAxis(joystick, 2, rjoy_h * 127); joystick->hwdata->prev_rjoy_h = rjoy_h; } if (abs(joystick->hwdata->prev_rjoy_v - rjoy_v) > AXIS_THRESHOLD) { SDL_PrivateJoystickAxis(joystick, 3, rjoy_v * 127); joystick->hwdata->prev_rjoy_v = rjoy_v; } } }
static int initializePad(struct pad_data_t* pad) { int tmp; int modes; int i; // is there any device connected to that port? if (waitPadReady(pad) == PAD_STATE_DISCONN) { return 1; // nope, don't waste your time here! } // How many different modes can this device operate in? // i.e. get # entrys in the modetable modes = padInfoMode(pad->port, pad->slot, PAD_MODETABLE, -1); LOG("PAD: The device has %d modes\n", modes); if (modes > 0) { LOG("( "); for (i = 0; i < modes; i++) { tmp = padInfoMode(pad->port, pad->slot, PAD_MODETABLE, i); LOG("%d ", tmp); } LOG(")"); } tmp = padInfoMode(pad->port, pad->slot, PAD_MODECURID, 0); LOG("PAD: It is currently using mode %d\n", tmp); // If modes == 0, this is not a Dual shock controller // (it has no actuator engines) if (modes == 0) { LOG("PAD: This is a digital controller?\n"); return 1; } // Verify that the controller has a DUAL SHOCK mode i = 0; do { if (padInfoMode(pad->port, pad->slot, PAD_MODETABLE, i) == PAD_TYPE_DUALSHOCK) break; i++; } while (i < modes); if (i >= modes) { LOG("PAD: This is no Dual Shock controller\n"); return 1; } // If ExId != 0x0 => This controller has actuator engines // This check should always pass if the Dual Shock test above passed tmp = padInfoMode(pad->port, pad->slot, PAD_MODECUREXID, 0); if (tmp == 0) { LOG("PAD: This is no Dual Shock controller??\n"); return 1; } LOG("PAD: Enabling dual shock functions\n"); // When using MMODE_LOCK, user cant change mode with Select button padSetMainMode(pad->port, pad->slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK); waitPadReady(pad); tmp = padInfoPressMode(pad->port, pad->slot); LOG("PAD: infoPressMode: %d\n", tmp); waitPadReady(pad); tmp = padEnterPressMode(pad->port, pad->slot); LOG("PAD: enterPressMode: %d\n", tmp); waitPadReady(pad); pad->actuators = padInfoAct(pad->port, pad->slot, -1, 0); LOG("PAD: # of actuators: %d\n", pad->actuators); if (pad->actuators != 0) { pad->actAlign[0] = 0; // Enable small engine pad->actAlign[1] = 1; // Enable big engine pad->actAlign[2] = 0xff; pad->actAlign[3] = 0xff; pad->actAlign[4] = 0xff; pad->actAlign[5] = 0xff; waitPadReady(pad); tmp = padSetActAlign(pad->port, pad->slot, pad->actAlign); LOG("PAD: padSetActAlign: %d\n", tmp); } else { LOG("PAD: Did not find any actuators.\n"); } waitPadReady(pad); return 1; }
bool Pad::init() { int ret; int modes; int i; waitReady(); // How many different modes can this device operate in? // i.e. get # entrys in the modetable modes = padInfoMode(m_port, m_slot, PAD_MODETABLE, -1); //printf("The device has %d modes\n", modes); if (modes > 0) { //printf("( "); for (i = 0; i < modes; i++) { //printf("%d ", padInfoMode(m_port, m_slot, PAD_MODETABLE, i)); } //printf(")"); } //printf("It is currently using mode %d\n", padInfoMode(m_port, m_slot, PAD_MODECURID, 0)); // If modes == 0, this is not a Dual shock controller // (it has no actuator engines) if (modes == 0) { //printf("This is a digital controller?\n"); return false; } // Verify that the controller has a DUAL SHOCK mode i = 0; do { if (padInfoMode( m_port, m_slot, PAD_MODETABLE, i) == PAD_TYPE_DUALSHOCK) break; i++; } while (i < modes); if (i >= modes) { //printf("This is no Dual Shock controller\n"); return false; } // If ExId != 0x0 => This controller has actuator engines // This check should always pass if the Dual Shock test above passed ret = padInfoMode(m_port, m_slot, PAD_MODECUREXID, 0); if (ret == 0) { //printf("This is no Dual Shock controller??\n"); return false; } //printf("Enabling dual shock functions\n"); // When using MMODE_LOCK, user cant change mode with Select button padSetMainMode(m_port, m_slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK); waitReady(); //printf("infoPressMode: %d\n", padInfoPressMode(m_port, m_slot)); waitReady(); //printf("enterPressMode: %d\n", padEnterPressMode(m_port, m_slot)); waitReady(); s_actuators = padInfoAct(m_port, m_slot, -1, 0); //printf("# of m_actuators: %d\n",ms_actuators); if (s_actuators != 0) { s_actAlign[0] = 0; // Enable small engine s_actAlign[1] = 1; // Enable big engine s_actAlign[2] = 0xff; s_actAlign[3] = 0xff; s_actAlign[4] = 0xff; s_actAlign[5] = 0xff; waitReady(); //printf("padSetActAlign: %d\n", //padSetActAlign(m_port, m_slot, ms_actAlign)); } else { //printf("Did not find any m_actuators.\n"); } waitReady(); update(); return true; }
void Ps2Pad::initPad(void) { int modes = 0; if (_padStatus == STAT_NONE) { if (padPortOpen(_port, _slot, _padBuf) == 1) { _padStatus = STAT_OPEN; _padInitTime = _system->getMillis(); } else { padPortClose(_port, _slot); printf("Unable to open port (%d/%d)!\n", _port, _slot); } } else { if (checkPadReady(_port, _slot)) { switch (_padStatus) { case STAT_OPEN: _padStatus = STAT_DETECT; break; case STAT_DETECT: _isDualShock = false; modes = padInfoMode(_port, _slot, PAD_MODETABLE, -1); // Verify that the controller has a DUAL SHOCK mode for (int cnt = 0; cnt < modes; cnt++) if (padInfoMode(_port, _slot, PAD_MODETABLE, cnt) == PAD_TYPE_DUALSHOCK) _isDualShock = true; // If ExId != 0x0 => This controller has actuator engines // This check should always pass if the Dual Shock test above passed if (_isDualShock) if (padInfoMode(_port, _slot, PAD_MODECUREXID, 0) == 0) _isDualShock = false; if (_isDualShock) { // When using MMODE_LOCK, user cant change mode with Select button padSetMainMode(_port, _slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK); _padStatus = STAT_INIT_DSHOCK; } else _padStatus = STAT_WAIT_READY; break; case STAT_INIT_DSHOCK: padEnterPressMode(_port, _slot); _padStatus = STAT_CHECK_ACT; break; case STAT_CHECK_ACT: _actuators = padInfoAct(_port, _slot, -1, 0); if (_actuators != 0) _padStatus = STAT_INIT_ACT; else { _isDualShock = false; _padStatus = STAT_WAIT_READY; } break; case STAT_INIT_ACT: char actAlign[6]; actAlign[0] = 0; actAlign[1] = 1; actAlign[2] = actAlign[3] = actAlign[4] = actAlign[5] = 0xff; padSetActAlign(_port, _slot, actAlign); _padStatus = STAT_WAIT_READY; break; case STAT_WAIT_READY: _padStatus = STAT_OKAY; break; case STAT_OKAY: case STAT_NONE: // pad is already initialized (or not there) break; } } else { // check for timeout... if (_system->getMillis() - _padInitTime > 5000) { // still no pad, give up. if (padPortClose(_port, _slot) != 1) printf("WARNING: can't close port: %d/%d\n", _port, _slot); printf("looking for pad, gave up and closed port\n"); _padStatus = STAT_NONE; } } } }
int main(int argc, char **argv) { u32 port; struct padButtonStatus buttons; int dualshock[2]; int acts[2]; SifInitRpc(0); printf("libpadx sample"); if((argc == 2) && (strncmp(argv[1], "free", 4) == 0)) { printf(" - Using PS2SDK freesio2.irx and freepad.irx modules.\n"); loadmodules(1); } else { printf(" - Using ROM XSIO2MAN and XPADMAN modules.\n"); printf("Start this sample with 'free' as an argument to load freesio2.irx and freepad.irx\n"); printf("Example: ps2client execee host:padx_sample.elf free\n"); loadmodules(0); } padInit(0); padBuf[0] = memalign(64, 256); padBuf[1] = memalign(64, 256); old_pad[0] = 0; old_pad[1] = 0; portConnected[0] = 0; portConnected[1] = 0; dualshock[0] = 0; dualshock[1] = 0; acts[0] = 0; acts[1] = 0; padPortOpen(0, 0, padBuf[0]); padPortOpen(1, 0, padBuf[1]); while(1) { for(port=0; port < 2; port++) { s32 state = padGetState(port, 0); if((state == PAD_STATE_STABLE) && (portConnected[port] == 0)) { u32 i; u8 mTable[8]; u32 ModeCurId; u32 ModeCurOffs; u32 ModeCurExId; u32 ModeTableNum = padInfoMode(port, 0, PAD_MODETABLE, -1); printf("Controller (%i) connected\n", port); /* Check if dualshock and if so, activate analog mode */ for(i = 0; i < ModeTableNum; i++) mTable[i] = padInfoMode(port, 0, PAD_MODETABLE, i); /* Works for dualshock2 */ if((mTable[0] == 4) && (mTable[1] == 7) && (ModeTableNum == 2)) dualshock[port] = 1; /* Active and lock analog mode */ if(dualshock[port] == 1) { padSetMainMode(port, 0, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK); padWait(port); } ModeCurId = padInfoMode(port, 0, PAD_MODECURID, 0); ModeCurOffs = padInfoMode(port, 0, PAD_MODECUROFFS, 0); ModeCurExId = padInfoMode(port, 0, PAD_MODECUREXID, 0); ModeTableNum = padInfoMode(port, 0, PAD_MODETABLE, -1); acts[port] = padInfoAct(port, 0, -1, 0); printf(" ModeCurId : %i (%s)\n", (int)ModeCurId, padTypeStr[ModeCurId]); printf(" ModeCurExId : %i\n", (int)ModeCurExId); printf(" ModeTable : "); for(i = 0; i < ModeTableNum; i++) { mTable[i] = padInfoMode(port, 0, PAD_MODETABLE, i); printf("%i ", (int)mTable[i]); } printf("\n"); printf(" ModeTableNum : %i\n", (int)ModeTableNum); printf(" ModeCurOffs : %i\n", (int)ModeCurOffs); printf(" NumOfAct : %i\n", (int)acts[port]); printf(" PressMode : %i\n", (int)padInfoPressMode(port, 0)); if(acts[port] > 0) { u8 actAlign[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; u32 i; /* Set offsets for motor parameters for SetActDirect. */ for(i=0; i < acts[port]; i++) actAlign[i] = i; padSetActAlign(port, 0, actAlign); padWait(port); } printf(" EnterPressMode : %i\n", (int)padEnterPressMode(port, 0)); padWait(port); printf("Ready\n"); portConnected[port] = 1; } if((state == PAD_STATE_DISCONN) && (portConnected[port] == 1)) { printf("Controller (%i) disconnected\n", port); portConnected[port] = 0; } if(portConnected[port] == 1) { s32 ret = padRead(port, 0, &buttons); if(ret != 0) { paddata[port] = 0xffff ^ buttons.btns; new_pad[port] = paddata[port] & ~old_pad[port]; old_pad[port] = paddata[port]; // Values 50 and 200 used because my controllers are worn out :-) if((buttons.ljoy_h <= 50) || (buttons.ljoy_h >= 200)) printf("Left Analog X: %i\n", (int)buttons.ljoy_h); if((buttons.ljoy_v <= 50) || (buttons.ljoy_v >= 200)) printf("Left Analog Y: %i\n", (int)buttons.ljoy_v); if((buttons.rjoy_h <= 50) || (buttons.rjoy_h >= 200)) printf("Right Analog X: %i\n", (int)buttons.rjoy_h); if((buttons.rjoy_v <= 50) || (buttons.rjoy_v >= 200)) printf("Right Analog Y: %i\n", (int)buttons.rjoy_v); if(new_pad[port]) printf("Controller (%i) button(s) pressed: ", (int)port); if(new_pad[port] & PAD_LEFT) printf("LEFT "); if(new_pad[port] & PAD_RIGHT) printf("RIGHT "); if(new_pad[port] & PAD_UP) printf("UP "); if(new_pad[port] & PAD_DOWN) printf("DOWN "); if(new_pad[port] & PAD_START) printf("START "); if(new_pad[port] & PAD_SELECT) printf("SELECT "); if(new_pad[port] & PAD_SQUARE) printf("SQUARE (Pressure: %i) ", (int)buttons.square_p); if(new_pad[port] & PAD_TRIANGLE) printf("TRIANGLE (Pressure: %i) ", (int)buttons.triangle_p); if(new_pad[port] & PAD_CIRCLE) printf("CIRCLE (Pressure: %i) ", (int)buttons.circle_p); if(new_pad[port] & PAD_CROSS) printf("CROSS (Pressure: %i) ", (int)buttons.cross_p); if(new_pad[port] & PAD_L1) { printf("L1 (Start Little Motor) "); padStartAct(port, 0, 1); } if(new_pad[port] & PAD_L2) { printf("L2 (Stop Little Motor) "); padStartAct(port, 0, 0); } if(new_pad[port] & PAD_L3) printf("L3 "); if(new_pad[port] & PAD_R1) { printf("R1 (Start Big Motor) "); padStartAct(port, 1, 255); } if(new_pad[port] & PAD_R2) { printf("R2 (Stop Big Motor) "); padStopAct(port, 1); } if(new_pad[port] & PAD_R3) printf("R3 "); if(new_pad[port]) printf("\n"); } } } wait_vsync(); } return 0; }