Merge pull request #5 from StefQM/master
Removed occasional incorrect reading of L2 button
Esse commit está contido em:
+244
-255
@@ -4,14 +4,12 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#if ARDUINO > 22
|
#if ARDUINO > 22
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#else
|
#else
|
||||||
#include "WProgram.h"
|
#include "WProgram.h"
|
||||||
#include "pins_arduino.h"
|
#include "pins_arduino.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static byte enter_config[]={0x01,0x43,0x00,0x01,0x00};
|
static byte enter_config[]={0x01,0x43,0x00,0x01,0x00};
|
||||||
static byte set_mode[]={0x01,0x44,0x00,0x01,0x03,0x00,0x00,0x00,0x00};
|
static byte set_mode[]={0x01,0x44,0x00,0x01,0x03,0x00,0x00,0x00,0x00};
|
||||||
static byte set_bytes_large[]={0x01,0x4F,0x00,0xFF,0xFF,0x03,0x00,0x00,0x00};
|
static byte set_bytes_large[]={0x01,0x4F,0x00,0xFF,0xFF,0x03,0x00,0x00,0x00};
|
||||||
@@ -19,50 +17,57 @@ static byte exit_config[]={0x01,0x43,0x00,0x00,0x5A,0x5A,0x5A,0x5A,0x5A};
|
|||||||
static byte enable_rumble[]={0x01,0x4D,0x00,0x00,0x01};
|
static byte enable_rumble[]={0x01,0x4D,0x00,0x00,0x01};
|
||||||
static byte type_read[]={0x01,0x45,0x00,0x5A,0x5A,0x5A,0x5A,0x5A,0x5A};
|
static byte type_read[]={0x01,0x45,0x00,0x5A,0x5A,0x5A,0x5A,0x5A,0x5A};
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
boolean PS2X::NewButtonState() {
|
boolean PS2X::NewButtonState() {
|
||||||
return ((last_buttons ^ buttons) > 0);
|
return ((last_buttons ^ buttons) > 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
boolean PS2X::NewButtonState(unsigned int button) {
|
boolean PS2X::NewButtonState(unsigned int button) {
|
||||||
return (((last_buttons ^ buttons) & button) > 0);
|
return (((last_buttons ^ buttons) & button) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
boolean PS2X::ButtonPressed(unsigned int button) {
|
boolean PS2X::ButtonPressed(unsigned int button) {
|
||||||
return(NewButtonState(button) & Button(button));
|
return(NewButtonState(button) & Button(button));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
boolean PS2X::ButtonReleased(unsigned int button) {
|
boolean PS2X::ButtonReleased(unsigned int button) {
|
||||||
return((NewButtonState(button)) & ((~last_buttons & button) > 0));
|
return((NewButtonState(button)) & ((~last_buttons & button) > 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
boolean PS2X::Button(uint16_t button) {
|
boolean PS2X::Button(uint16_t button) {
|
||||||
return ((~buttons & button) > 0);
|
return ((~buttons & button) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
unsigned int PS2X::ButtonDataByte() {
|
unsigned int PS2X::ButtonDataByte() {
|
||||||
return (~buttons);
|
return (~buttons);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
byte PS2X::Analog(byte button) {
|
byte PS2X::Analog(byte button) {
|
||||||
return PS2data[button];
|
return PS2data[button];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
unsigned char PS2X::_gamepad_shiftinout (char byte) {
|
unsigned char PS2X::_gamepad_shiftinout (char byte) {
|
||||||
|
|
||||||
|
|
||||||
unsigned char tmp = 0;
|
unsigned char tmp = 0;
|
||||||
for(i=0;i<8;i++) {
|
for(unsigned char i=0;i<8;i++) {
|
||||||
|
if(CHK(byte,i)) CMD_SET();
|
||||||
|
else CMD_CLR();
|
||||||
|
|
||||||
if(CHK(byte,i)) CMD_SET();
|
CLK_CLR();
|
||||||
else CMD_CLR();
|
delayMicroseconds(CTRL_CLK);
|
||||||
CLK_CLR();
|
|
||||||
|
|
||||||
delayMicroseconds(CTRL_CLK);
|
//if(DAT_CHK()) SET(tmp,i);
|
||||||
|
if(DAT_CHK()) bitSet(tmp,i);
|
||||||
|
|
||||||
if(DAT_CHK()) SET(tmp,i);
|
CLK_SET();
|
||||||
CLK_SET();
|
|
||||||
#if CTRL_CLK_HIGH
|
#if CTRL_CLK_HIGH
|
||||||
delayMicroseconds(CTRL_CLK_HIGH);
|
delayMicroseconds(CTRL_CLK_HIGH);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
CMD_SET();
|
CMD_SET();
|
||||||
@@ -70,84 +75,80 @@ unsigned char PS2X::_gamepad_shiftinout (char byte) {
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
void PS2X::read_gamepad() {
|
void PS2X::read_gamepad() {
|
||||||
read_gamepad(false, 0x00);
|
read_gamepad(false, 0x00);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
boolean PS2X::read_gamepad(boolean motor1, byte motor2) {
|
boolean PS2X::read_gamepad(boolean motor1, byte motor2) {
|
||||||
double temp = millis() - last_read;
|
double temp = millis() - last_read;
|
||||||
|
|
||||||
if (temp > 1500) //waited to long
|
if (temp > 1500) //waited to long
|
||||||
reconfig_gamepad();
|
reconfig_gamepad();
|
||||||
|
|
||||||
if(temp < read_delay) //waited too short
|
if(temp < read_delay) //waited too short
|
||||||
delay(read_delay - temp);
|
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};
|
||||||
|
|
||||||
if(motor2 != 0x00)
|
// Try a few times to get valid data...
|
||||||
motor2 = map(motor2,0,255,0x40,0xFF); //noting below 40 will make it spin
|
for (byte RetryCnt = 0; RetryCnt < 5; RetryCnt++) {
|
||||||
|
CMD_SET();
|
||||||
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();
|
CLK_SET();
|
||||||
ATT_CLR(); // low enable joystick
|
ATT_CLR(); // low enable joystick
|
||||||
|
|
||||||
delayMicroseconds(CTRL_BYTE_DELAY);
|
delayMicroseconds(CTRL_BYTE_DELAY);
|
||||||
//Send the command to send button and joystick data;
|
//Send the command to send button and joystick data;
|
||||||
|
for (int i = 0; i<9; i++) {
|
||||||
|
PS2data[i] = _gamepad_shiftinout(dword[i]);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i<9; i++) {
|
if(PS2data[1] == 0x79) { //if controller is in full data return mode, get the rest of data
|
||||||
PS2data[i] = _gamepad_shiftinout(dword[i]);
|
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(PS2data[1] == 0x79) { //if controller is in full data return mode, get the rest of data
|
// If we got to here, we are not in analog mode, try to recover...
|
||||||
for (int i = 0; i<12; i++) {
|
reconfig_gamepad(); // try to get back into Analog mode.
|
||||||
PS2data[i+9] = _gamepad_shiftinout(dword2[i]);
|
delay(read_delay);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ATT_SET(); // HI disable joystick
|
// If we get here and still not in analog mode (=0x7_), try increasing the read_delay...
|
||||||
// Check to see if we received valid data or not. We should be in analog mode for our data
|
if ((PS2data[1] & 0xf0) != 0x70) {
|
||||||
// to be valie
|
if (read_delay < 10)
|
||||||
if ((PS2data[1] & 0xf0) == 0x70)
|
read_delay++; // see if this helps out...
|
||||||
break;
|
}
|
||||||
|
|
||||||
// If we got to here, we are not in analog mode, try to recover...
|
#ifdef PS2X_COM_DEBUG
|
||||||
reconfig_gamepad(); // try to get back into Analog mode.
|
Serial.println("OUT:IN");
|
||||||
delay(read_delay);
|
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
|
||||||
|
|
||||||
// If we get here and still not in analog mode, try increasing the read_delay...
|
last_buttons = buttons; //store the previous buttons states
|
||||||
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__)
|
#if defined(__AVR__)
|
||||||
buttons = *(uint16_t*)(PS2data+3); //store as one value for multiple functions
|
buttons = *(uint16_t*)(PS2data+3); //store as one value for multiple functions
|
||||||
@@ -155,48 +156,47 @@ boolean PS2X::read_gamepad(boolean motor1, byte motor2) {
|
|||||||
buttons = (uint16_t)(PS2data[4] << 8) + PS2data[3]; //store as one value for multiple functions
|
buttons = (uint16_t)(PS2data[4] << 8) + PS2data[3]; //store as one value for multiple functions
|
||||||
#endif
|
#endif
|
||||||
last_read = millis();
|
last_read = millis();
|
||||||
return ((PS2data[1] & 0xf0) == 0x70);
|
return ((PS2data[1] & 0xf0) == 0x70); // 1 = OK = analog mode - 0 = NOK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat) {
|
byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat) {
|
||||||
return config_gamepad(clk, cmd, att, dat, false, false);
|
return config_gamepad(clk, cmd, att, dat, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat, bool pressures, bool rumble) {
|
byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat, bool pressures, bool rumble) {
|
||||||
|
|
||||||
byte temp[sizeof(type_read)];
|
byte temp[sizeof(type_read)];
|
||||||
|
|
||||||
#ifdef __AVR__
|
#ifdef __AVR__
|
||||||
_clk_mask = digitalPinToBitMask(clk);
|
_clk_mask = digitalPinToBitMask(clk);
|
||||||
_clk_oreg = portOutputRegister(digitalPinToPort(clk));
|
_clk_oreg = portOutputRegister(digitalPinToPort(clk));
|
||||||
_cmd_mask = digitalPinToBitMask(cmd);
|
_cmd_mask = digitalPinToBitMask(cmd);
|
||||||
_cmd_oreg = portOutputRegister(digitalPinToPort(cmd));
|
_cmd_oreg = portOutputRegister(digitalPinToPort(cmd));
|
||||||
_att_mask = digitalPinToBitMask(att);
|
_att_mask = digitalPinToBitMask(att);
|
||||||
_att_oreg = portOutputRegister(digitalPinToPort(att));
|
_att_oreg = portOutputRegister(digitalPinToPort(att));
|
||||||
_dat_mask = digitalPinToBitMask(dat);
|
_dat_mask = digitalPinToBitMask(dat);
|
||||||
_dat_ireg = portInputRegister(digitalPinToPort(dat));
|
_dat_ireg = portInputRegister(digitalPinToPort(dat));
|
||||||
#else
|
#else
|
||||||
|
uint32_t lport; // Port number for this pin
|
||||||
|
_clk_mask = digitalPinToBitMask(clk);
|
||||||
|
lport = digitalPinToPort(clk);
|
||||||
|
_clk_lport_set = portOutputRegister(lport) + 2;
|
||||||
|
_clk_lport_clr = portOutputRegister(lport) + 1;
|
||||||
|
|
||||||
uint32_t lport; // Port number for this pin
|
_cmd_mask = digitalPinToBitMask(cmd);
|
||||||
_clk_mask = digitalPinToBitMask(clk);
|
lport = digitalPinToPort(cmd);
|
||||||
lport = digitalPinToPort(clk);
|
_cmd_lport_set = portOutputRegister(lport) + 2;
|
||||||
_clk_lport_set = portOutputRegister(lport) + 2;
|
_cmd_lport_clr = portOutputRegister(lport) + 1;
|
||||||
_clk_lport_clr = portOutputRegister(lport) + 1;
|
|
||||||
|
|
||||||
_cmd_mask = digitalPinToBitMask(cmd);
|
_att_mask = digitalPinToBitMask(att);
|
||||||
lport = digitalPinToPort(cmd);
|
lport = digitalPinToPort(att);
|
||||||
_cmd_lport_set = portOutputRegister(lport) + 2;
|
_att_lport_set = portOutputRegister(lport) + 2;
|
||||||
_cmd_lport_clr = portOutputRegister(lport) + 1;
|
_att_lport_clr = portOutputRegister(lport) + 1;
|
||||||
|
|
||||||
_att_mask = digitalPinToBitMask(att);
|
|
||||||
lport = digitalPinToPort(att);
|
|
||||||
_att_lport_set = portOutputRegister(lport) + 2;
|
|
||||||
_att_lport_clr = portOutputRegister(lport) + 1;
|
|
||||||
|
|
||||||
_dat_mask = digitalPinToBitMask(dat);
|
|
||||||
_dat_lport = portInputRegister(digitalPinToPort(dat));
|
|
||||||
|
|
||||||
|
_dat_mask = digitalPinToBitMask(dat);
|
||||||
|
_dat_lport = portInputRegister(digitalPinToPort(dat));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pinMode(clk, OUTPUT); //configure ports
|
pinMode(clk, OUTPUT); //configure ports
|
||||||
@@ -208,86 +208,80 @@ byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat, bo
|
|||||||
digitalWrite(dat, HIGH); //enable pull-up
|
digitalWrite(dat, HIGH); //enable pull-up
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
CMD_SET(); // SET(*_cmd_oreg,_cmd_mask);
|
CMD_SET(); // SET(*_cmd_oreg,_cmd_mask);
|
||||||
CLK_SET();
|
CLK_SET();
|
||||||
|
|
||||||
//new error checking. First, read gamepad a few times to see if it's talking
|
//new error checking. First, read gamepad a few times to see if it's talking
|
||||||
read_gamepad();
|
read_gamepad();
|
||||||
read_gamepad();
|
read_gamepad();
|
||||||
|
|
||||||
//see if it talked
|
//see if it talked - see if mode came back.
|
||||||
if(PS2data[1] != 0x41 && PS2data[1] != 0x73 && PS2data[1] != 0x79){ //see if mode came back. If still anything but 41, 73 or 79, then it's not talking
|
//If still anything but 41, 73 or 79, then it's not talking
|
||||||
#ifdef PS2X_DEBUG
|
if(PS2data[1] != 0x41 && PS2data[1] != 0x73 && PS2data[1] != 0x79){
|
||||||
Serial.println("Controller mode not matched or no controller found");
|
#ifdef PS2X_DEBUG
|
||||||
Serial.print("Expected 0x41 or 0x73, got ");
|
Serial.println("Controller mode not matched or no controller found");
|
||||||
Serial.println(PS2data[1], HEX);
|
Serial.print("Expected 0x41, 0x73 or 0x79, but got ");
|
||||||
#endif
|
Serial.println(PS2data[1], HEX);
|
||||||
|
#endif
|
||||||
return 1; //return error code 1
|
return 1; //return error code 1
|
||||||
}
|
}
|
||||||
|
|
||||||
//try setting mode, increasing delays if need be.
|
//try setting mode, increasing delays if need be.
|
||||||
read_delay = 1;
|
read_delay = 1;
|
||||||
|
|
||||||
for(int y = 0; y <= 10; y++)
|
for(int y = 0; y <= 10; y++) {
|
||||||
{
|
sendCommandString(enter_config, sizeof(enter_config)); //start config run
|
||||||
sendCommandString(enter_config, sizeof(enter_config)); //start config run
|
|
||||||
|
|
||||||
//read type
|
//read type
|
||||||
delayMicroseconds(CTRL_BYTE_DELAY);
|
delayMicroseconds(CTRL_BYTE_DELAY);
|
||||||
|
|
||||||
CMD_SET();
|
CMD_SET();
|
||||||
CLK_SET();
|
CLK_SET();
|
||||||
ATT_CLR(); // low enable joystick
|
ATT_CLR(); // low enable joystick
|
||||||
|
|
||||||
delayMicroseconds(CTRL_BYTE_DELAY);
|
delayMicroseconds(CTRL_BYTE_DELAY);
|
||||||
|
|
||||||
for (int i = 0; i<9; i++) {
|
for (int i = 0; i<9; i++) {
|
||||||
temp[i] = _gamepad_shiftinout(type_read[i]);
|
temp[i] = _gamepad_shiftinout(type_read[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ATT_SET(); // HI disable joystick
|
ATT_SET(); // HI disable joystick
|
||||||
|
|
||||||
controller_type = temp[3];
|
controller_type = temp[3];
|
||||||
|
|
||||||
sendCommandString(set_mode, sizeof(set_mode));
|
sendCommandString(set_mode, sizeof(set_mode));
|
||||||
if(rumble){ sendCommandString(enable_rumble, sizeof(enable_rumble)); en_Rumble = true; }
|
if(rumble){ sendCommandString(enable_rumble, sizeof(enable_rumble)); en_Rumble = true; }
|
||||||
if(pressures){ sendCommandString(set_bytes_large, sizeof(set_bytes_large)); en_Pressures = true; }
|
if(pressures){ sendCommandString(set_bytes_large, sizeof(set_bytes_large)); en_Pressures = true; }
|
||||||
sendCommandString(exit_config, sizeof(exit_config));
|
sendCommandString(exit_config, sizeof(exit_config));
|
||||||
|
|
||||||
read_gamepad();
|
read_gamepad();
|
||||||
|
|
||||||
if(pressures){
|
if(pressures){
|
||||||
if(PS2data[1] == 0x79)
|
if(PS2data[1] == 0x79)
|
||||||
break;
|
break;
|
||||||
if(PS2data[1] == 0x73)
|
if(PS2data[1] == 0x73)
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(PS2data[1] == 0x73)
|
if(PS2data[1] == 0x73)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if(y == 10){
|
if(y == 10){
|
||||||
#ifdef PS2X_DEBUG
|
#ifdef PS2X_DEBUG
|
||||||
Serial.println("Controller not accepting commands");
|
Serial.println("Controller not accepting commands");
|
||||||
Serial.print("mode stil set at");
|
Serial.print("mode stil set at");
|
||||||
Serial.println(PS2data[1], HEX);
|
Serial.println(PS2data[1], HEX);
|
||||||
#endif
|
#endif
|
||||||
return 2; //exit function with error
|
return 2; //exit function with error
|
||||||
}
|
}
|
||||||
|
|
||||||
read_delay += 1; //add 1ms to read_delay
|
read_delay += 1; //add 1ms to read_delay
|
||||||
}
|
}
|
||||||
|
return 0; //no error if here
|
||||||
return 0; //no error if here
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
void PS2X::sendCommandString(byte string[], byte len) {
|
void PS2X::sendCommandString(byte string[], byte len) {
|
||||||
|
#ifdef PS2X_COM_DEBUG
|
||||||
|
|
||||||
#ifdef PS2X_COM_DEBUG
|
|
||||||
byte temp[len];
|
byte temp[len];
|
||||||
ATT_CLR(); // low enable joystick
|
ATT_CLR(); // low enable joystick
|
||||||
delayMicroseconds(CTRL_BYTE_DELAY);
|
delayMicroseconds(CTRL_BYTE_DELAY);
|
||||||
@@ -296,180 +290,175 @@ void PS2X::sendCommandString(byte string[], byte len) {
|
|||||||
temp[y] = _gamepad_shiftinout(string[y]);
|
temp[y] = _gamepad_shiftinout(string[y]);
|
||||||
|
|
||||||
ATT_SET(); //high disable joystick
|
ATT_SET(); //high disable joystick
|
||||||
delay(read_delay); //wait a few
|
delay(read_delay); //wait a few
|
||||||
|
|
||||||
Serial.println("OUT:IN Configure");
|
Serial.println("OUT:IN Configure");
|
||||||
for(int i=0; i<len; i++){
|
for(int i=0; i<len; i++) {
|
||||||
Serial.print(string[i], HEX);
|
Serial.print(string[i], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(temp[i], HEX);
|
Serial.print(temp[i], HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
}
|
}
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
|
#else
|
||||||
#else
|
|
||||||
ATT_CLR(); // low enable joystick
|
ATT_CLR(); // low enable joystick
|
||||||
for (int y=0; y < len; y++)
|
for (int y=0; y < len; y++)
|
||||||
_gamepad_shiftinout(string[y]);
|
_gamepad_shiftinout(string[y]);
|
||||||
|
ATT_SET(); //high disable joystick
|
||||||
ATT_SET(); //high disable joystick
|
delay(read_delay); //wait a few
|
||||||
delay(read_delay); //wait a few
|
#endif
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
byte PS2X::readType() {
|
byte PS2X::readType() {
|
||||||
/*
|
/*
|
||||||
byte temp[sizeof(type_read)];
|
byte temp[sizeof(type_read)];
|
||||||
|
|
||||||
sendCommandString(enter_config, sizeof(enter_config));
|
sendCommandString(enter_config, sizeof(enter_config));
|
||||||
|
|
||||||
delayMicroseconds(CTRL_BYTE_DELAY);
|
delayMicroseconds(CTRL_BYTE_DELAY);
|
||||||
|
|
||||||
CMD_SET();
|
CMD_SET();
|
||||||
CLK_SET();
|
CLK_SET();
|
||||||
ATT_CLR(); // low enable joystick
|
ATT_CLR(); // low enable joystick
|
||||||
|
|
||||||
delayMicroseconds(CTRL_BYTE_DELAY);
|
delayMicroseconds(CTRL_BYTE_DELAY);
|
||||||
|
|
||||||
for (int i = 0; i<9; i++) {
|
for (int i = 0; i<9; i++) {
|
||||||
temp[i] = _gamepad_shiftinout(type_read[i]);
|
temp[i] = _gamepad_shiftinout(type_read[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendCommandString(exit_config, sizeof(exit_config));
|
sendCommandString(exit_config, sizeof(exit_config));
|
||||||
|
|
||||||
if(temp[3] == 0x03)
|
if(temp[3] == 0x03)
|
||||||
return 1;
|
return 1;
|
||||||
else if(temp[3] == 0x01)
|
else if(temp[3] == 0x01)
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if(controller_type == 0x03)
|
if(controller_type == 0x03)
|
||||||
return 1;
|
return 1;
|
||||||
else if(controller_type == 0x01)
|
else if(controller_type == 0x01)
|
||||||
return 2;
|
return 2;
|
||||||
|
else if(controller_type == 0x0C)
|
||||||
return 0;
|
return 3; //2.4G Wireless Dual Shock PS2 Game Controller
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
void PS2X::enableRumble() {
|
void PS2X::enableRumble() {
|
||||||
|
sendCommandString(enter_config, sizeof(enter_config));
|
||||||
sendCommandString(enter_config, sizeof(enter_config));
|
sendCommandString(enable_rumble, sizeof(enable_rumble));
|
||||||
sendCommandString(enable_rumble, sizeof(enable_rumble));
|
sendCommandString(exit_config, sizeof(exit_config));
|
||||||
sendCommandString(exit_config, sizeof(exit_config));
|
en_Rumble = true;
|
||||||
en_Rumble = true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
bool PS2X::enablePressures() {
|
bool PS2X::enablePressures() {
|
||||||
|
sendCommandString(enter_config, sizeof(enter_config));
|
||||||
|
sendCommandString(set_bytes_large, sizeof(set_bytes_large));
|
||||||
|
sendCommandString(exit_config, sizeof(exit_config));
|
||||||
|
|
||||||
sendCommandString(enter_config, sizeof(enter_config));
|
read_gamepad();
|
||||||
sendCommandString(set_bytes_large, sizeof(set_bytes_large));
|
read_gamepad();
|
||||||
sendCommandString(exit_config, sizeof(exit_config));
|
|
||||||
|
|
||||||
read_gamepad();
|
if(PS2data[1] != 0x79)
|
||||||
read_gamepad();
|
return false;
|
||||||
|
|
||||||
if(PS2data[1] != 0x79)
|
en_Pressures = true;
|
||||||
return false;
|
return true;
|
||||||
|
|
||||||
en_Pressures = true;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
void PS2X::reconfig_gamepad(){
|
void PS2X::reconfig_gamepad(){
|
||||||
|
sendCommandString(enter_config, sizeof(enter_config));
|
||||||
sendCommandString(enter_config, sizeof(enter_config));
|
sendCommandString(set_mode, sizeof(set_mode));
|
||||||
sendCommandString(set_mode, sizeof(set_mode));
|
if (en_Rumble)
|
||||||
if (en_Rumble)
|
sendCommandString(enable_rumble, sizeof(enable_rumble));
|
||||||
sendCommandString(enable_rumble, sizeof(enable_rumble));
|
if (en_Pressures)
|
||||||
if (en_Pressures)
|
sendCommandString(set_bytes_large, sizeof(set_bytes_large));
|
||||||
sendCommandString(set_bytes_large, sizeof(set_bytes_large));
|
sendCommandString(exit_config, sizeof(exit_config));
|
||||||
sendCommandString(exit_config, sizeof(exit_config));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
#ifdef __AVR__
|
#ifdef __AVR__
|
||||||
inline void PS2X::CLK_SET(void) {
|
inline void PS2X::CLK_SET(void) {
|
||||||
|
register uint8_t old_sreg = SREG;
|
||||||
register uint8_t old_sreg = SREG;
|
cli();
|
||||||
cli();
|
*_clk_oreg |= _clk_mask;
|
||||||
*_clk_oreg |= _clk_mask;
|
SREG = old_sreg;
|
||||||
SREG = old_sreg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::CLK_CLR(void) {
|
inline void PS2X::CLK_CLR(void) {
|
||||||
register uint8_t old_sreg = SREG;
|
register uint8_t old_sreg = SREG;
|
||||||
cli();
|
cli();
|
||||||
*_clk_oreg &= ~_clk_mask;
|
*_clk_oreg &= ~_clk_mask;
|
||||||
SREG = old_sreg;
|
SREG = old_sreg;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::CMD_SET(void) {
|
inline void PS2X::CMD_SET(void) {
|
||||||
register uint8_t old_sreg = SREG;
|
register uint8_t old_sreg = SREG;
|
||||||
cli();
|
cli();
|
||||||
*_cmd_oreg |= _cmd_mask; // SET(*_cmd_oreg,_cmd_mask);
|
*_cmd_oreg |= _cmd_mask; // SET(*_cmd_oreg,_cmd_mask);
|
||||||
SREG = old_sreg;
|
SREG = old_sreg;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::CMD_CLR(void) {
|
inline void PS2X::CMD_CLR(void) {
|
||||||
register uint8_t old_sreg = SREG;
|
register uint8_t old_sreg = SREG;
|
||||||
cli();
|
cli();
|
||||||
*_cmd_oreg &= ~_cmd_mask; // SET(*_cmd_oreg,_cmd_mask);
|
*_cmd_oreg &= ~_cmd_mask; // SET(*_cmd_oreg,_cmd_mask);
|
||||||
SREG = old_sreg;
|
SREG = old_sreg;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::ATT_SET(void) {
|
inline void PS2X::ATT_SET(void) {
|
||||||
register uint8_t old_sreg = SREG;
|
register uint8_t old_sreg = SREG;
|
||||||
cli();
|
cli();
|
||||||
*_att_oreg |= _att_mask ;
|
*_att_oreg |= _att_mask ;
|
||||||
SREG = old_sreg;
|
SREG = old_sreg;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::ATT_CLR(void) {
|
inline void PS2X::ATT_CLR(void) {
|
||||||
register uint8_t old_sreg = SREG;
|
register uint8_t old_sreg = SREG;
|
||||||
cli();
|
cli();
|
||||||
*_att_oreg &= ~_att_mask;
|
*_att_oreg &= ~_att_mask;
|
||||||
SREG = old_sreg;
|
SREG = old_sreg;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool PS2X::DAT_CHK(void) {
|
inline bool PS2X::DAT_CHK(void) {
|
||||||
return (*_dat_ireg & _dat_mask)? true : false;
|
return (*_dat_ireg & _dat_mask) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
// On pic32, use the set/clr registers to make them atomic...inline void PS2X::CLK_SET(void) {
|
// On pic32, use the set/clr registers to make them atomic...
|
||||||
*_clk_lport_set |= _clk_mask;
|
inline void PS2X::CLK_SET(void) {
|
||||||
|
*_clk_lport_set |= _clk_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::CLK_CLR(void) {
|
inline void PS2X::CLK_CLR(void) {
|
||||||
*_clk_lport_clr |= _clk_mask;
|
*_clk_lport_clr |= _clk_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::CMD_SET(void) {
|
inline void PS2X::CMD_SET(void) {
|
||||||
*_cmd_lport_set |= _cmd_mask;
|
*_cmd_lport_set |= _cmd_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::CMD_CLR(void) {
|
inline void PS2X::CMD_CLR(void) {
|
||||||
*_cmd_lport_clr |= _cmd_mask;
|
*_cmd_lport_clr |= _cmd_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::ATT_SET(void) {
|
inline void PS2X::ATT_SET(void) {
|
||||||
*_att_lport_set |= _att_mask;
|
*_att_lport_set |= _att_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PS2X::ATT_CLR(void) {
|
inline void PS2X::ATT_CLR(void) {
|
||||||
*_att_lport_clr |= _att_mask;
|
*_att_lport_clr |= _att_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool PS2X::DAT_CHK(void) {
|
inline bool PS2X::DAT_CHK(void) {
|
||||||
return (*_dat_lport & _dat_mask)? true : false;
|
return (*_dat_lport & _dat_mask) ? true : false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+80
-84
@@ -72,36 +72,34 @@ GNU General Public License for more details.
|
|||||||
*
|
*
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
|
|
||||||
// $$$$$$$$$$$$ DEBUG ENABLE SECTION $$$$$$$$$$$$$$$$
|
// $$$$$$$$$$$$ DEBUG ENABLE SECTION $$$$$$$$$$$$$$$$
|
||||||
// to debug ps2 controller, uncomment these two lines to print out debug to uart
|
// to debug ps2 controller, uncomment these two lines to print out debug to uart
|
||||||
|
|
||||||
//#define PS2X_DEBUG
|
//#define PS2X_DEBUG
|
||||||
//#define PS2X_COM_DEBUG
|
//#define PS2X_COM_DEBUG
|
||||||
|
|
||||||
|
|
||||||
#ifndef PS2X_lib_h
|
#ifndef PS2X_lib_h
|
||||||
#define PS2X_lib_h
|
#define PS2X_lib_h
|
||||||
|
|
||||||
#if ARDUINO > 22
|
#if ARDUINO > 22
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#else
|
#else
|
||||||
#include "WProgram.h"
|
#include "WProgram.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#ifdef __AVR__
|
#ifdef __AVR__
|
||||||
#include <avr/io.h>
|
// AVR
|
||||||
|
#include <avr/io.h>
|
||||||
#define CTRL_CLK 4
|
#define CTRL_CLK 4
|
||||||
#define CTRL_BYTE_DELAY 3
|
#define CTRL_BYTE_DELAY 3
|
||||||
#else
|
#else
|
||||||
// Pic32...
|
// Pic32...
|
||||||
#include <pins_arduino.h>
|
#include <pins_arduino.h>
|
||||||
#define CTRL_CLK 5
|
#define CTRL_CLK 5
|
||||||
#define CTRL_CLK_HIGH 5
|
#define CTRL_CLK_HIGH 5
|
||||||
#define CTRL_BYTE_DELAY 4
|
#define CTRL_BYTE_DELAY 4
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//These are our button constants
|
//These are our button constants
|
||||||
@@ -127,14 +125,14 @@ GNU General Public License for more details.
|
|||||||
#define PSB_SQUARE 0x8000
|
#define PSB_SQUARE 0x8000
|
||||||
|
|
||||||
//Guitar button constants
|
//Guitar button constants
|
||||||
#define GREEN_FRET 0x0200
|
|
||||||
#define RED_FRET 0x2000
|
|
||||||
#define YELLOW_FRET 0x1000
|
|
||||||
#define BLUE_FRET 0x4000
|
|
||||||
#define ORANGE_FRET 0x8000
|
|
||||||
#define STAR_POWER 0x0100
|
|
||||||
#define UP_STRUM 0x0010
|
#define UP_STRUM 0x0010
|
||||||
#define DOWN_STRUM 0x0040
|
#define DOWN_STRUM 0x0040
|
||||||
|
#define STAR_POWER 0x0100
|
||||||
|
#define GREEN_FRET 0x0200
|
||||||
|
#define YELLOW_FRET 0x1000
|
||||||
|
#define RED_FRET 0x2000
|
||||||
|
#define BLUE_FRET 0x4000
|
||||||
|
#define ORANGE_FRET 0x8000
|
||||||
#define WHAMMY_BAR 8
|
#define WHAMMY_BAR 8
|
||||||
|
|
||||||
//These are stick values
|
//These are stick values
|
||||||
@@ -144,7 +142,7 @@ GNU General Public License for more details.
|
|||||||
#define PSS_LY 8
|
#define PSS_LY 8
|
||||||
|
|
||||||
//These are analog buttons
|
//These are analog buttons
|
||||||
#define PSAB_PAD_RIGHT 9
|
#define PSAB_PAD_RIGHT 9
|
||||||
#define PSAB_PAD_UP 11
|
#define PSAB_PAD_UP 11
|
||||||
#define PSAB_PAD_DOWN 12
|
#define PSAB_PAD_DOWN 12
|
||||||
#define PSAB_PAD_LEFT 10
|
#define PSAB_PAD_LEFT 10
|
||||||
@@ -161,77 +159,75 @@ GNU General Public License for more details.
|
|||||||
#define PSAB_CROSS 15
|
#define PSAB_CROSS 15
|
||||||
#define PSAB_SQUARE 16
|
#define PSAB_SQUARE 16
|
||||||
|
|
||||||
|
|
||||||
#define SET(x,y) (x|=(1<<y))
|
#define SET(x,y) (x|=(1<<y))
|
||||||
#define CLR(x,y) (x&=(~(1<<y)))
|
#define CLR(x,y) (x&=(~(1<<y)))
|
||||||
#define CHK(x,y) (x & (1<<y))
|
#define CHK(x,y) (x & (1<<y))
|
||||||
#define TOG(x,y) (x^=(1<<y))
|
#define TOG(x,y) (x^=(1<<y))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PS2X {
|
class PS2X {
|
||||||
public:
|
public:
|
||||||
boolean Button(uint16_t);
|
boolean Button(uint16_t); //will be TRUE if button is being pressed
|
||||||
unsigned int ButtonDataByte();
|
unsigned int ButtonDataByte();
|
||||||
boolean NewButtonState();
|
boolean NewButtonState();
|
||||||
boolean NewButtonState(unsigned int);
|
boolean NewButtonState(unsigned int); //will be TRUE if button was JUST pressed OR released
|
||||||
boolean ButtonPressed(unsigned int);
|
boolean ButtonPressed(unsigned int); //will be TRUE if button was JUST pressed
|
||||||
boolean ButtonReleased(unsigned int);
|
boolean ButtonReleased(unsigned int); //will be TRUE if button was JUST released
|
||||||
void read_gamepad();
|
void read_gamepad();
|
||||||
boolean read_gamepad(boolean, byte);
|
boolean read_gamepad(boolean, byte);
|
||||||
byte readType();
|
byte readType();
|
||||||
byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t);
|
byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t);
|
||||||
byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t, bool, bool);
|
byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t, bool, bool);
|
||||||
void enableRumble();
|
void enableRumble();
|
||||||
bool enablePressures();
|
bool enablePressures();
|
||||||
byte Analog(byte);
|
byte Analog(byte);
|
||||||
void reconfig_gamepad();
|
void reconfig_gamepad();
|
||||||
private:
|
|
||||||
|
|
||||||
inline void CLK_SET(void);
|
private:
|
||||||
inline void CLK_CLR(void);
|
inline void CLK_SET(void);
|
||||||
inline void CMD_SET(void);
|
inline void CLK_CLR(void);
|
||||||
inline void CMD_CLR(void);
|
inline void CMD_SET(void);
|
||||||
inline void ATT_SET(void);
|
inline void CMD_CLR(void);
|
||||||
inline void ATT_CLR(void);
|
inline void ATT_SET(void);
|
||||||
inline bool DAT_CHK(void);
|
inline void ATT_CLR(void);
|
||||||
|
inline bool DAT_CHK(void);
|
||||||
|
|
||||||
unsigned char _gamepad_shiftinout (char);
|
unsigned char _gamepad_shiftinout (char);
|
||||||
unsigned char PS2data[21];
|
unsigned char PS2data[21];
|
||||||
void sendCommandString(byte*, byte);
|
void sendCommandString(byte*, byte);
|
||||||
unsigned char i;
|
unsigned char i;
|
||||||
unsigned int last_buttons;
|
unsigned int last_buttons;
|
||||||
unsigned int buttons;
|
unsigned int buttons;
|
||||||
#ifdef __AVR__
|
|
||||||
uint8_t maskToBitNum(uint8_t);
|
|
||||||
uint8_t _clk_mask;
|
|
||||||
volatile uint8_t *_clk_oreg;
|
|
||||||
uint8_t _cmd_mask;
|
|
||||||
volatile uint8_t *_cmd_oreg;
|
|
||||||
uint8_t _att_mask;
|
|
||||||
volatile uint8_t *_att_oreg;
|
|
||||||
uint8_t _dat_mask;
|
|
||||||
volatile uint8_t *_dat_ireg;
|
|
||||||
#else
|
|
||||||
uint8_t maskToBitNum(uint8_t);
|
|
||||||
uint16_t _clk_mask;
|
|
||||||
volatile uint32_t * _clk_lport_set;
|
|
||||||
volatile uint32_t * _clk_lport_clr;
|
|
||||||
uint16_t _cmd_mask;
|
|
||||||
volatile uint32_t * _cmd_lport_set;
|
|
||||||
volatile uint32_t * _cmd_lport_clr;
|
|
||||||
uint16_t _att_mask;
|
|
||||||
volatile uint32_t * _att_lport_set;
|
|
||||||
volatile uint32_t * _att_lport_clr;
|
|
||||||
uint16_t _dat_mask;
|
|
||||||
volatile uint32_t * _dat_lport;
|
|
||||||
#endif
|
|
||||||
unsigned long last_read;
|
|
||||||
byte read_delay;
|
|
||||||
byte controller_type;
|
|
||||||
boolean en_Rumble;
|
|
||||||
boolean en_Pressures;
|
|
||||||
|
|
||||||
|
#ifdef __AVR__
|
||||||
|
uint8_t maskToBitNum(uint8_t);
|
||||||
|
uint8_t _clk_mask;
|
||||||
|
volatile uint8_t *_clk_oreg;
|
||||||
|
uint8_t _cmd_mask;
|
||||||
|
volatile uint8_t *_cmd_oreg;
|
||||||
|
uint8_t _att_mask;
|
||||||
|
volatile uint8_t *_att_oreg;
|
||||||
|
uint8_t _dat_mask;
|
||||||
|
volatile uint8_t *_dat_ireg;
|
||||||
|
#else
|
||||||
|
uint8_t maskToBitNum(uint8_t);
|
||||||
|
uint16_t _clk_mask;
|
||||||
|
volatile uint32_t *_clk_lport_set;
|
||||||
|
volatile uint32_t *_clk_lport_clr;
|
||||||
|
uint16_t _cmd_mask;
|
||||||
|
volatile uint32_t *_cmd_lport_set;
|
||||||
|
volatile uint32_t *_cmd_lport_clr;
|
||||||
|
uint16_t _att_mask;
|
||||||
|
volatile uint32_t *_att_lport_set;
|
||||||
|
volatile uint32_t *_att_lport_clr;
|
||||||
|
uint16_t _dat_mask;
|
||||||
|
volatile uint32_t *_dat_lport;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
unsigned long last_read;
|
||||||
|
byte read_delay;
|
||||||
|
byte controller_type;
|
||||||
|
boolean en_Rumble;
|
||||||
|
boolean en_Pressures;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,182 +1,189 @@
|
|||||||
#include <PS2X_lib.h> //for v1.6
|
#include <PS2X_lib.h> //for v1.6
|
||||||
|
|
||||||
|
/******************************************************************
|
||||||
|
* set pins connected to PS2 controller:
|
||||||
|
* - 1e column: original
|
||||||
|
* - 2e colmun: Stef?
|
||||||
|
* replace pin numbers by the ones you use
|
||||||
|
******************************************************************/
|
||||||
|
#define PS2_DAT 13 //14
|
||||||
|
#define PS2_CMD 11 //15
|
||||||
|
#define PS2_SEL 10 //16
|
||||||
|
#define PS2_CLK 12 //17
|
||||||
|
|
||||||
|
/******************************************************************
|
||||||
|
* select modes of PS2 controller:
|
||||||
|
* - pressures = analog reading of push-butttons
|
||||||
|
* - rumble = motor rumbling
|
||||||
|
* uncomment 1 of the lines for each mode selection
|
||||||
|
******************************************************************/
|
||||||
|
//#define pressures true
|
||||||
|
#define pressures false
|
||||||
|
//#define rumble true
|
||||||
|
#define rumble false
|
||||||
|
|
||||||
PS2X ps2x; // create PS2 Controller Class
|
PS2X ps2x; // create PS2 Controller Class
|
||||||
|
|
||||||
//right now, the library does NOT support hot pluggable controllers, meaning
|
//right now, the library does NOT support hot pluggable controllers, meaning
|
||||||
//you must always either restart your Arduino after you conect the controller,
|
//you must always either restart your Arduino after you connect the controller,
|
||||||
//or call config_gamepad(pins) again after connecting the controller.
|
//or call config_gamepad(pins) again after connecting the controller.
|
||||||
|
|
||||||
int error = 0;
|
int error = 0;
|
||||||
byte type = 0;
|
byte type = 0;
|
||||||
byte vibrate = 0;
|
byte vibrate = 0;
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(57600);
|
|
||||||
|
|
||||||
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
|
Serial.begin(57600);
|
||||||
|
|
||||||
error = ps2x.config_gamepad(13,11,10,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
|
delay(300); //added delay to give wireless ps2 module some time to startup, before configuring it
|
||||||
|
|
||||||
if(error == 0){
|
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
|
||||||
Serial.println("Found Controller, configured successful");
|
|
||||||
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
|
|
||||||
Serial.println("holding L1 or R1 will print out the analog stick values.");
|
|
||||||
Serial.println("Go to www.billporter.info for updates and to report bugs.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
|
||||||
|
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
|
||||||
|
|
||||||
|
if(error == 0){
|
||||||
|
Serial.print("Found Controller, configured successful ");
|
||||||
|
Serial.print("pressures = ");
|
||||||
|
if (pressures)
|
||||||
|
Serial.println("true ");
|
||||||
|
else
|
||||||
|
Serial.println("false");
|
||||||
|
Serial.print("rumble = ");
|
||||||
|
if (rumble)
|
||||||
|
Serial.println("true)");
|
||||||
|
else
|
||||||
|
Serial.println("false");
|
||||||
|
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
|
||||||
|
Serial.println("holding L1 or R1 will print out the analog stick values.");
|
||||||
|
Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
|
||||||
|
}
|
||||||
else if(error == 1)
|
else if(error == 1)
|
||||||
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
|
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
|
||||||
|
|
||||||
else if(error == 2)
|
else if(error == 2)
|
||||||
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
|
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
|
||||||
|
|
||||||
else if(error == 3)
|
else if(error == 3)
|
||||||
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
|
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
|
||||||
|
|
||||||
//Serial.print(ps2x.Analog(1), HEX);
|
// Serial.print(ps2x.Analog(1), HEX);
|
||||||
|
|
||||||
type = ps2x.readType();
|
|
||||||
switch(type) {
|
|
||||||
case 0:
|
|
||||||
Serial.println("Unknown Controller type");
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
Serial.println("DualShock Controller Found");
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
Serial.println("GuitarHero Controller Found");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
type = ps2x.readType();
|
||||||
|
switch(type) {
|
||||||
|
case 0:
|
||||||
|
Serial.print("Unknown Controller type found ");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
Serial.print("DualShock Controller found ");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
Serial.print("GuitarHero Controller found ");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
Serial.print("Wireless Sony DualShock Controller found ");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop() {
|
||||||
/* You must Read Gamepad to get new values
|
/* You must Read Gamepad to get new values and set vibration values
|
||||||
Read GamePad and set vibration values
|
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
|
||||||
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
|
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
|
||||||
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
|
You should call this at least once a second
|
||||||
|
|
||||||
you should call this at least once a second
|
|
||||||
*/
|
*/
|
||||||
|
if(error == 1) //skip loop if no controller found
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(type == 2){ //Guitar Hero Controller
|
||||||
|
ps2x.read_gamepad(); //read controller
|
||||||
|
|
||||||
|
if(ps2x.ButtonPressed(GREEN_FRET))
|
||||||
if(error == 1) //skip loop if no controller found
|
Serial.println("Green Fret Pressed");
|
||||||
return;
|
if(ps2x.ButtonPressed(RED_FRET))
|
||||||
|
Serial.println("Red Fret Pressed");
|
||||||
if(type == 2){ //Guitar Hero Controller
|
if(ps2x.ButtonPressed(YELLOW_FRET))
|
||||||
|
Serial.println("Yellow Fret Pressed");
|
||||||
ps2x.read_gamepad(); //read controller
|
if(ps2x.ButtonPressed(BLUE_FRET))
|
||||||
|
Serial.println("Blue Fret Pressed");
|
||||||
if(ps2x.ButtonPressed(GREEN_FRET))
|
if(ps2x.ButtonPressed(ORANGE_FRET))
|
||||||
Serial.println("Green Fret Pressed");
|
Serial.println("Orange Fret Pressed");
|
||||||
if(ps2x.ButtonPressed(RED_FRET))
|
|
||||||
Serial.println("Red Fret Pressed");
|
|
||||||
if(ps2x.ButtonPressed(YELLOW_FRET))
|
|
||||||
Serial.println("Yellow Fret Pressed");
|
|
||||||
if(ps2x.ButtonPressed(BLUE_FRET))
|
|
||||||
Serial.println("Blue Fret Pressed");
|
|
||||||
if(ps2x.ButtonPressed(ORANGE_FRET))
|
|
||||||
Serial.println("Orange Fret Pressed");
|
|
||||||
|
|
||||||
|
|
||||||
if(ps2x.ButtonPressed(STAR_POWER))
|
if(ps2x.ButtonPressed(STAR_POWER))
|
||||||
Serial.println("Star Power Command");
|
Serial.println("Star Power Command");
|
||||||
|
|
||||||
if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed
|
if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed
|
||||||
Serial.println("Up Strum");
|
Serial.println("Up Strum");
|
||||||
if(ps2x.Button(DOWN_STRUM))
|
if(ps2x.Button(DOWN_STRUM))
|
||||||
Serial.println("DOWN Strum");
|
Serial.println("DOWN Strum");
|
||||||
|
|
||||||
|
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
|
||||||
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
|
Serial.println("Start is being held");
|
||||||
Serial.println("Start is being held");
|
|
||||||
if(ps2x.Button(PSB_SELECT))
|
if(ps2x.Button(PSB_SELECT))
|
||||||
Serial.println("Select is being held");
|
Serial.println("Select is being held");
|
||||||
|
|
||||||
|
if(ps2x.Button(ORANGE_FRET)) { // print stick value IF TRUE
|
||||||
if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
|
Serial.print("Wammy Bar Position:");
|
||||||
{
|
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
|
||||||
Serial.print("Wammy Bar Position:");
|
|
||||||
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else { //DualShock Controller
|
||||||
|
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
|
||||||
|
|
||||||
else { //DualShock Controller
|
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
|
||||||
|
Serial.println("Start is being held");
|
||||||
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
|
|
||||||
|
|
||||||
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
|
|
||||||
Serial.println("Start is being held");
|
|
||||||
if(ps2x.Button(PSB_SELECT))
|
if(ps2x.Button(PSB_SELECT))
|
||||||
Serial.println("Select is being held");
|
Serial.println("Select is being held");
|
||||||
|
|
||||||
|
|
||||||
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
|
|
||||||
Serial.print("Up held this hard: ");
|
|
||||||
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
|
|
||||||
}
|
|
||||||
if(ps2x.Button(PSB_PAD_RIGHT)){
|
|
||||||
Serial.print("Right held this hard: ");
|
|
||||||
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
|
|
||||||
}
|
|
||||||
if(ps2x.Button(PSB_PAD_LEFT)){
|
|
||||||
Serial.print("LEFT held this hard: ");
|
|
||||||
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
|
|
||||||
}
|
|
||||||
if(ps2x.Button(PSB_PAD_DOWN)){
|
|
||||||
Serial.print("DOWN held this hard: ");
|
|
||||||
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
vibrate = ps2x.Analog(PSAB_BLUE); //this will set the large motor vibrate speed based on
|
|
||||||
//how hard you press the blue (X) button
|
|
||||||
|
|
||||||
if (ps2x.NewButtonState()) //will be TRUE if any button changes state (on to off, or off to on)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(ps2x.Button(PSB_L3))
|
|
||||||
Serial.println("L3 pressed");
|
|
||||||
if(ps2x.Button(PSB_R3))
|
|
||||||
Serial.println("R3 pressed");
|
|
||||||
if(ps2x.Button(PSB_L2))
|
|
||||||
Serial.println("L2 pressed");
|
|
||||||
if(ps2x.Button(PSB_R2))
|
|
||||||
Serial.println("R2 pressed");
|
|
||||||
if(ps2x.Button(PSB_GREEN))
|
|
||||||
Serial.println("Triangle pressed");
|
|
||||||
|
|
||||||
|
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
|
||||||
|
Serial.print("Up held this hard: ");
|
||||||
|
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
|
||||||
|
}
|
||||||
|
if(ps2x.Button(PSB_PAD_RIGHT)){
|
||||||
|
Serial.print("Right held this hard: ");
|
||||||
|
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
|
||||||
|
}
|
||||||
|
if(ps2x.Button(PSB_PAD_LEFT)){
|
||||||
|
Serial.print("LEFT held this hard: ");
|
||||||
|
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
|
||||||
|
}
|
||||||
|
if(ps2x.Button(PSB_PAD_DOWN)){
|
||||||
|
Serial.print("DOWN held this hard: ");
|
||||||
|
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vibrate = ps2x.Analog(PSAB_CROSS); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
|
||||||
if(ps2x.ButtonPressed(PSB_RED)) //will be TRUE if button was JUST pressed
|
if (ps2x.NewButtonState()) { //will be TRUE if any button changes state (on to off, or off to on)
|
||||||
Serial.println("Circle just pressed");
|
if(ps2x.Button(PSB_L3))
|
||||||
|
Serial.println("L3 pressed");
|
||||||
if(ps2x.ButtonReleased(PSB_PINK)) //will be TRUE if button was JUST released
|
if(ps2x.Button(PSB_R3))
|
||||||
Serial.println("Square just released");
|
Serial.println("R3 pressed");
|
||||||
|
if(ps2x.Button(PSB_L2))
|
||||||
if(ps2x.NewButtonState(PSB_BLUE)) //will be TRUE if button was JUST pressed OR released
|
Serial.println("L2 pressed");
|
||||||
Serial.println("X just changed");
|
if(ps2x.Button(PSB_R2))
|
||||||
|
Serial.println("R2 pressed");
|
||||||
|
if(ps2x.Button(PSB_TRIANGLE))
|
||||||
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
|
Serial.println("Triangle pressed");
|
||||||
{
|
|
||||||
Serial.print("Stick Values:");
|
|
||||||
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
|
|
||||||
Serial.print(",");
|
|
||||||
Serial.print(ps2x.Analog(PSS_LX), DEC);
|
|
||||||
Serial.print(",");
|
|
||||||
Serial.print(ps2x.Analog(PSS_RY), DEC);
|
|
||||||
Serial.print(",");
|
|
||||||
Serial.println(ps2x.Analog(PSS_RX), DEC);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(ps2x.ButtonPressed(PSB_CIRCLE)) //will be TRUE if button was JUST pressed
|
||||||
|
Serial.println("Circle just pressed");
|
||||||
|
if(ps2x.NewButtonState(PSB_CROSS)) //will be TRUE if button was JUST pressed OR released
|
||||||
|
Serial.println("X just changed");
|
||||||
|
if(ps2x.ButtonReleased(PSB_SQUARE)) //will be TRUE if button was JUST released
|
||||||
|
Serial.println("Square just released");
|
||||||
|
|
||||||
}
|
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
|
||||||
|
Serial.print("Stick Values:");
|
||||||
|
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
|
||||||
delay(50);
|
Serial.print(",");
|
||||||
|
Serial.print(ps2x.Analog(PSS_LX), DEC);
|
||||||
|
Serial.print(",");
|
||||||
|
Serial.print(ps2x.Analog(PSS_RY), DEC);
|
||||||
|
Serial.print(",");
|
||||||
|
Serial.println(ps2x.Analog(PSS_RX), DEC);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|||||||
Referência em uma Nova Issue
Bloquear um usuário