boolean sonyPS2_setAnalogMode(SONY_PS2* controller){ if(sonyPS2_read(controller)){ int8_t retries=100; while(controller->mode!=PS2_MODE_ANALOGUE_PRESSURE && --retries){ sendCmd(controller,cmd_config,sizeof(cmd_config)); sendCmd(controller,cmd_analog,sizeof(cmd_analog)); sendCmd(controller,cmd_normal,sizeof(cmd_normal)); sonyPS2_read(controller); sonyPS2_read(controller); if(controller->mode==PS2_MODE_ANALOGUE_RED){ sendCmd(controller,cmd_config,sizeof(cmd_config)); sendCmd(controller,cmd_enterDS2,sizeof(cmd_enterDS2)); // sendCmd(controller,cmd_exitDS2,sizeof(cmd_exitDS2)); sendCmd(controller,cmd_normal,sizeof(cmd_normal)); } sonyPS2_read(controller); sonyPS2_read(controller); } } return sonyPS2_isAnalogMode(controller); }
// This routine is called repeatedly - its your main loop TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){ if(sonyPS2_read(&controller1)) { xButton_pressure = sonyPS2_buttonPressure(&controller1, PS2_BTN_X); SqButton_pressure = sonyPS2_buttonPressure(&controller1, PS2_BTN_SQUARE); DL_Button_pressure = sonyPS2_buttonPressure(&controller1, PS2_DPAD_LEFT); DR_Button_pressure = sonyPS2_buttonPressure(&controller1, PS2_DPAD_RIGHT); if(sonyPS2_setAnalogMode(&controller1)) { left_x = sonyPS2_joystick(&controller1, PS2_STICK_LEFT_X); left_y = sonyPS2_joystick(&controller1, PS2_STICK_LEFT_Y); right_x = sonyPS2_joystick(&controller1, PS2_STICK_RIGHT_X); right_y = sonyPS2_joystick(&controller1, PS2_STICK_RIGHT_Y); if((left_x+left_y+right_x+right_y) != 0) { SendData(PS2_B_BTN_NONE, PS2_B_BTN_NONE); } } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_R1)) { LED_on(&RightRed); SendData(PS2_B_BTN_R1, PS2_B_BTN_R1); } else if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_RIGHT)) { LED_on(&RightRed); SendData(PS2_B_DPAD_RIGHT, DR_Button_pressure); } else { LED_off(&RightRed); } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_L1)) { LED_on(&LeftRed); rprintfChar(SYNC_PACKET); rprintfChar(PS2_B_BTN_L1); rprintfChar(right_x); rprintfChar(right_y); rprintfChar(left_x); rprintfChar(left_y); rprintfChar(END_PACKET); } else if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_LEFT)) { LED_on(&LeftRed); SendData(PS2_B_DPAD_LEFT, DL_Button_pressure); } else { LED_off(&LeftRed); } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_R2)) { SendData(PS2_B_BTN_R2, PS2_B_BTN_R2); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_L2)) { SendData(PS2_B_BTN_L2, PS2_B_BTN_L2); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_TRIANGLE)) { SpotLightState++; if(SpotLightState % 2 == 0) { LED_off(&SpotLightWhite); } else { LED_on(&SpotLightWhite); } SendData(PS2_B_BTN_TRIANGLE, PS2_B_BTN_TRIANGLE); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_CIRCLE)) { SendData(PS2_B_BTN_CIRCLE, PS2_B_BTN_CIRCLE); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_SQUARE)) { SendData(PS2_B_BTN_SQUARE, SqButton_pressure); if(SqButton_pressure == 255) { sonyPS_setRumble(&controller1, SqButton_pressure/2, false); } else { sonyPS_setRumble(&controller1, SqButton_pressure/2, false); } } else if(sonyPS2_buttonHeld(&controller1, PS2_BTN_X)) { SendData(PS2_B_BTN_X, xButton_pressure); if(xButton_pressure == 255) { sonyPS_setRumble(&controller1, xButton_pressure/2, false); } else { sonyPS_setRumble(&controller1, xButton_pressure/2, false); } } else { sonyPS_setRumble(&controller1, 0, false); } if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_UP)) { SendData(PS2_B_DPAD_UP, PS2_B_DPAD_UP); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_DOWN)) { SendData(PS2_B_DPAD_DOWN, PS2_B_DPAD_DOWN); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_SELECT)) { SendData(PS2_B_BTN_SELECT, PS2_B_BTN_SELECT); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_START)) { SendData(PS2_B_BTN_START, PS2_B_BTN_START); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_L3)) { SendData(PS2_B_BTN_L3, PS2_B_BTN_L3); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_R3)) { SendData(PS2_B_BTN_R3, PS2_B_BTN_R3); } else { } } else { } //act_setSpeed(&engine, FrontSpeed); //act_setSpeed(&rudder, RudderPos); return 200000; // wait for 1 second before calling me again. 1000000us = 1 //second }
// This routine is called repeatedly - its your main loop TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){ if(sonyPS2_read(&controller1)) { char xButton_pressure = sonyPS2_buttonPressure(&controller1, PS2_BTN_X); char SqButton_pressure = sonyPS2_buttonPressure(&controller1, PS2_BTN_SQUARE); char DL_Button_pressure = sonyPS2_buttonPressure(&controller1, PS2_DPAD_LEFT); char DR_Button_pressure = sonyPS2_buttonPressure(&controller1, PS2_DPAD_RIGHT); if(sonyPS2_buttonHeld(&controller1, PS2_BTN_R1)) { LED_on(&RightRed); rprintf("PS2_BTN_R1"); } else { LED_off(&RightRed); } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_L1)) { LED_on(&LeftRed); rprintf("PS2_BTN_L1"); } else { LED_off(&LeftRed); } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_R2)) { rprintf("PS2_BTN_R2"); } else { LED_off(&RightRed); } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_L2)) { rprintf("PS2_BTN_L2"); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_TRIANGLE)) { LED_on(&SpotLightWhite); rprintf("PS2_BTN_TRIANGLE"); } else { LED_off(&SpotLightWhite); } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_CIRCLE)) { rprintf("PS2_BTN_CIRCLE"); } else { } if((sonyPS2_buttonHeld(&controller1, PS2_BTN_X)) || (sonyPS2_buttonHeld(&controller1, PS2_BTN_SQUARE))) { if(sonyPS2_buttonHeld(&controller1, PS2_BTN_SQUARE)) { if(SqButton_pressure == 255) { sonyPS_setRumble(&controller1, SqButton_pressure, true); rprintf("Sq Button Max Speed"); // FrontSpeed = 128; } else { sonyPS_setRumble(&controller1, SqButton_pressure, false); rprintf("Sq Button held"); //FrontSpeed = 157-(SqButton_pressure/9); } } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_X)) { if(xButton_pressure == 255) { sonyPS_setRumble(&controller1, xButton_pressure, true); rprintf("xButton Max Speed"); //FrontSpeed = 127; } else { sonyPS_setRumble(&controller1, xButton_pressure, false); rprintf("xButton held"); //FrontSpeed = 98+(xButton_pressure/9); } } } else { //FrontSpeed = 0; LED_off(&SpotLightWhite); sonyPS_setRumble(&controller1, 0, false); } if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_UP)) { rprintf("PS2_DPAD_UP"); // if(FrontSpeed <= 255) // { // FrontSpeed++; //} // } // else // { } if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_DOWN)) { rprintf("PS2_DPAD_DOWN"); //if(FrontSpeed > 0) //{ // FrontSpeed--; // //} } else { } if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_LEFT)) { rprintf("PS2_DPAD_LEFT"); if(DL_Button_pressure == 255) { rprintf("DL_Button Max Speed"); //RudderPos = 128; } else { rprintf("DL_Button held"); // RudderDex = DL_Button_pressure/DexConstant; // if(RudderPos > (128+RudderDex) && RudderPos <= 255) // { // RudderPos -= RudderDex; // } // else if(RudderPos >= 0 && RudderPos <= 127) // { // RudderPos -= RudderDex; // } } } else { } if(sonyPS2_buttonHeld(&controller1, PS2_DPAD_RIGHT)) { rprintf("PS2_DPAD_RIGHT"); if(DR_Button_pressure == 255) { rprintf("DR_Button Max Speed"); //RudderPos = 127; } else { //rprintf("DR_Button held"); //RudderDex = DR_Button_pressure/DexConstant; //if(RudderPos >=0 && RudderPos <= (127-RudderDex)) //{ // RudderPos += RudderDex; // } // else if(RudderPos >= 128 && RudderPos <= 255) // { // RudderPos += RudderDex; // } } } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_SELECT)) { rprintf("PS2_B_BTN_SELECT"); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_START)) { rprintf("PS2_B_BTN_START"); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_L3)) { rprintf("PS2_B_BTN_L3"); } else { } if(sonyPS2_buttonHeld(&controller1, PS2_BTN_R3)) { rprintf("PS2_B_BTN_R3"); } else { } } else { } //act_setSpeed(&engine, FrontSpeed); //act_setSpeed(&rudder, RudderPos); return 200000; // wait for 1 second before calling me again. 1000000us = 1 //second }