void PSX64::read_gamepad(bool motor1, uint8_t motor2) { double temp = millis() - last_read; if (temp > 1500) //waited to long reconfig_gamepad(); if(temp < read_delay) //waited too short delay(read_delay - temp); last_buttons = buttons; //store the previous buttons states if(motor2 != 0x00) motor2 = map(motor2,0,255,0x40,0xFF); //noting below 40 will make it spin SET(*_cmd_oreg,_cmd_mask); SET(*_clk_oreg,_clk_mask); CLR(*_att_oreg,_att_mask); // low enable joystick delayMicroseconds(CTRL_BYTE_DELAY); //Send the command to send button and joystick data; char dword[9] = {0x01,0x42,0,motor1,motor2,0,0,0,0}; uint8_t dword2[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; for (int i = 0; i<9; i++) { PS2data[i] = _gamepad_shiftinout(dword[i]); } if(PS2data[1] == 0x79) { //if controller is in full data return mode, get the rest of data for (int i = 0; i<12; i++) { PS2data[i+9] = _gamepad_shiftinout(dword2[i]); } } SET(*_att_oreg,_att_mask); // HI disable joystick #ifdef PSX64_COM_DEBUG Serial.println("OUT:IN"); for(int i=0; i<9; i++){ Serial.print(dword[i], HEX); Serial.print(":"); Serial.print(PS2data[i], HEX); Serial.print(" "); } for (int i = 0; i<12; i++) { Serial.print(dword2[i], HEX); Serial.print(":"); Serial.print(PS2data[i+9], HEX); Serial.print(" "); } Serial.println(""); #endif buttons = *(uint16_t*)(PS2data+3); //store as one value for multiple functions last_read = millis(); }
void PS2X::read_gamepad(boolean motor1, byte motor2) { double temp = millis() - last_read; SaveInterruptState; // *** KJE **** save away the current state of interrupts - *** *** KJE *** *** if (temp > 1500) //waited to long reconfig_gamepad(); if(temp < read_delay) //waited too short delay(read_delay - temp); last_buttons = buttons; //store the previous buttons states if(motor2 != 0x00) motor2 = map(motor2,0,255,0x40,0xFF); //noting below 40 will make it spin DisableInterrupts; //*** KJE *** CMD_SET(); CLK_SET(); ATT_CLR(); // low enable joystick RestoreInterrupts; // *** KJE *** - Interrupts may be enabled again delayMicroseconds(CTRL_BYTE_DELAY); //Send the command to send button and joystick data; char dword[9] = {0x01,0x42,0,motor1,motor2,0,0,0,0}; byte dword2[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; for (int i = 0; i<9; i++) { PS2data[i] = _gamepad_shiftinout(dword[i]); } if(PS2data[1] == 0x79) { //if controller is in full data return mode, get the rest of data for (int i = 0; i<12; i++) { PS2data[i+9] = _gamepad_shiftinout(dword2[i]); } } DisableInterrupts; ATT_SET(); // HI disable joystick RestoreInterrupts; // Interrupts may be enabled again #ifdef PS2X_COM_DEBUG Serial.println("OUT:IN"); for(int i=0; i<9; i++){ Serial.print(dword[i], HEX); Serial.print(":"); Serial.print(PS2data[i], HEX); Serial.print(" "); } for (int i = 0; i<12; i++) { Serial.print(dword2[i], HEX); Serial.print(":"); Serial.print(PS2data[i+9], HEX); Serial.print(" "); } Serial.println(""); #endif //buttons = *(uint16_t*)(PS2data+3); // FAULTS on chipkit!!! store as one value for multiple functions buttons = (uint16_t)(PS2data[4] << 8) + PS2data[3]; //store as one value for multiple functions last_read = millis(); }
boolean PS2X::read_gamepad(boolean motor1, byte motor2) { double temp = millis() - last_read; if (temp > 1500) //waited to long reconfig_gamepad(); if(temp < read_delay) //waited too short delay(read_delay - temp); if(motor2 != 0x00) motor2 = map(motor2,0,255,0x40,0xFF); //noting below 40 will make it spin char dword[9] = {0x01,0x42,0,motor1,motor2,0,0,0,0}; byte dword2[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; // Try a few times to get valid data... for (byte RetryCnt = 0; RetryCnt < 5; RetryCnt++) { CMD_SET(); CLK_SET(); ATT_CLR(); // low enable joystick delayMicroseconds(CTRL_BYTE_DELAY); //Send the command to send button and joystick data; for (int i = 0; i<9; i++) { PS2data[i] = _gamepad_shiftinout(dword[i]); } if(PS2data[1] == 0x79) { //if controller is in full data return mode, get the rest of data for (int i = 0; i<12; i++) { PS2data[i+9] = _gamepad_shiftinout(dword2[i]); } } ATT_SET(); // HI disable joystick // Check to see if we received valid data or not. // We should be in analog mode for our data to be valid (analog == 0x7_) if ((PS2data[1] & 0xf0) == 0x70) break; // If we got to here, we are not in analog mode, try to recover... reconfig_gamepad(); // try to get back into Analog mode. delay(read_delay); } // If we get here and still not in analog mode (=0x7_), try increasing the read_delay... if ((PS2data[1] & 0xf0) != 0x70) { if (read_delay < 10) read_delay++; // see if this helps out... } #ifdef PS2X_COM_DEBUG Serial.println("OUT:IN"); for(int i=0; i<9; i++){ Serial.print(dword[i], HEX); Serial.print(":"); Serial.print(PS2data[i], HEX); Serial.print(" "); } for (int i = 0; i<12; i++) { Serial.print(dword2[i], HEX); Serial.print(":"); Serial.print(PS2data[i+9], HEX); Serial.print(" "); } Serial.println(""); #endif last_buttons = buttons; //store the previous buttons states #if defined(__AVR__) buttons = *(uint16_t*)(PS2data+3); //store as one value for multiple functions #else buttons = (uint16_t)(PS2data[4] << 8) + PS2data[3]; //store as one value for multiple functions #endif last_read = millis(); return ((PS2data[1] & 0xf0) == 0x70); // 1 = OK = analog mode - 0 = NOK }