From e44a21631cef2a3f5d526f63c431b91389f8920c Mon Sep 17 00:00:00 2001 From: mcgurk Date: Fri, 7 Oct 2016 11:54:18 +0300 Subject: [PATCH] Update RetroJoystickAdapter_Playstation_beta.ino --- RetroJoystickAdapter_Playstation_beta.ino | 122 ++++++++++++++-------- 1 file changed, 79 insertions(+), 43 deletions(-) diff --git a/RetroJoystickAdapter_Playstation_beta.ino b/RetroJoystickAdapter_Playstation_beta.ino index 74d98f8..4bcc935 100644 --- a/RetroJoystickAdapter_Playstation_beta.ino +++ b/RetroJoystickAdapter_Playstation_beta.ino @@ -8,11 +8,17 @@ #error The Joystick2 library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.). #endif +#if !defined(_USING_HID) +#error "Using legacy HID core (non pluggable)" +#endif + #define JOYSTICK_REPORT_ID 0x03 #define JOYSTICK2_REPORT_ID 0x04 #define JOYSTICK3_REPORT_ID 0x05 #define JOYSTICK4_REPORT_ID 0x06 +//#define DEBUG + //================================================================================ //================================================================================ // Joystick (Gamepad) @@ -30,6 +36,8 @@ public: int8_t xAxis; int8_t yAxis; + int8_t xAxis2; + int8_t yAxis2; uint16_t buttons; //void begin(bool initAutoSendState = true); @@ -45,34 +53,42 @@ public: }; -#define JOYSTICK_STATE_SIZE 4 +#define JOYSTICK_STATE_SIZE 6 #define HIDDESC_MACRO(REPORT_ID) \ /* Joystick # */ \ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ \ - 0x09, 0x05, /* USAGE (Joystick) */ \ - 0xA1, 0x02, /* COLLECTION (Logical) */ \ - 0xA1, 0x01, /* COLLECTION (Application) */ \ - 0x85, REPORT_ID, /* REPORT_ID */ \ + 0x09, 0x04, /* USAGE (Joystick) */ \ + 0xa1, 0x01, /* COLLECTION (Application) */ \ + 0x85, REPORT_ID, /* REPORT_ID */ \ + /* 16 Buttons */ \ + 0x05, 0x09, /* USAGE_PAGE (Button) */ \ + 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ \ + 0x29, 0x10, /* USAGE_MAXIMUM (Button 16) */ \ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \ + 0x75, 0x01, /* REPORT_SIZE (1) */ \ + 0x95, 0x10, /* REPORT_COUNT (16) */ \ + 0x55, 0x00, /* UNIT_EXPONENT (0) */ \ + 0x65, 0x00, /* UNIT (None) */ \ + 0x81, 0x02, /* INPUT (Data,Var,Abs) */ \ /* X and Y Axis */ \ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ \ + 0x09, 0x01, /* USAGE (Pointer) */ \ + 0xA1, 0x00, /* COLLECTION (Physical) */ \ + 0x09, 0x32, /* USAGE (Z) */ \ + 0x09, 0x35, /* USAGE (Rz) */ \ 0x09, 0x30, /* USAGE (x) */ \ 0x09, 0x31, /* USAGE (y) */ \ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \ - 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */ \ + 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */ \ 0x75, 0x08, /* REPORT_SIZE (8) */ \ - 0x95, 0x02, /* REPORT_COUNT (2) */ \ - 0x81, 0x02, /* INPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */ \ - /* 16 Buttons */ \ - 0x05, 0x09, /* USAGE_PAGE (Button) */ \ - 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ \ - 0x29, 0x10, /* USAGE_MAXIMUM (Button 16) */ \ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \ - 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \ - 0x75, 0x01, /* REPORT_SIZE (1) */ \ - 0x95, 0x10, /* REPORT_COUNT (16) */ \ - 0x81, 0x02, /* INPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) */ \ - 0xC0, /* END_COLLECTION */ \ - 0xC0 /* END_COLLECTION */ + 0x95, 0x04, /* REPORT_COUNT (4) */ \ + 0x81, 0x02, /* INPUT (Data,Var,Abs) */ \ + 0xc0, /* END_COLLECTION */ \ + 0xc0 /* END_COLLECTION */ + + static const uint8_t hidReportDescriptor[] PROGMEM = { @@ -98,8 +114,10 @@ Joystick_::Joystick_(uint8_t initJoystickId, uint8_t initReportId) // Initalize State joystickId = initJoystickId; reportId = initReportId; - xAxis = 0; - yAxis = 0; + xAxis = 127; + yAxis = 127; + xAxis2 = 127; + yAxis2 = 127; buttons = 0; sendState(); @@ -111,18 +129,24 @@ void Joystick_::sendState() uint16_t buttonTmp = buttons; // Split 16 bit button-state into 2 bytes - data[2] = buttonTmp & 0xFF; + data[0] = buttonTmp & 0xFF; buttonTmp >>= 8; - data[3] = buttonTmp & 0xFF; + data[1] = buttonTmp & 0xFF; - data[0] = xAxis; - data[1] = yAxis; + data[2] = xAxis; + data[3] = yAxis; + data[4] = xAxis2; + data[5] = yAxis2; + //data[2] = 127;//xAxis; + //data[3] = 127;//yAxis; // HID().SendReport(Report number, array of values in same order as HID descriptor, length) HID().SendReport(reportId, data, JOYSTICK_STATE_SIZE); } Joystick_ Joystick[4] = +//Joystick_ Joystick[3] = +//Joystick_ Joystick[2] = { Joystick_(0, JOYSTICK_REPORT_ID), Joystick_(1, JOYSTICK2_REPORT_ID), @@ -180,7 +204,10 @@ void setup() { pinMode(CMD2, OUTPUT); pinMode(ATT2, OUTPUT); pinMode(CLK2, OUTPUT);*/ + + #ifdef DEBUG Serial.begin(115200); + #endif } @@ -208,11 +235,13 @@ void loop() { //delayMicroseconds(160); //delay(16); + #ifdef DEBUG /*Serial.print("head: 0x"); Serial.print(head, HEX); Serial.print(" type: 0x"); Serial.print(type, HEX); Serial.print(" padding: 0x"); Serial.print(padding, HEX); Serial.print(" data1: 0x"); Serial.print(data1, HEX); Serial.print(" "), Serial.print(data1, BIN); Serial.print(" data2: 0x"); Serial.print(data2, HEX); Serial.print(" "), Serial.print(data2, BIN); Serial.print(" rest: "); Serial.print(data3, DEC); Serial.print(" "); Serial.print(data4, DEC); Serial.print(" "); Serial.print(data5, DEC); Serial.print(" "); Serial.print(data6, DEC); Serial.println();*/ + #endif // check and read multitap delayMicroseconds(20); @@ -225,28 +254,34 @@ void loop() { //padding = shift(0x01); if (multitap == 0x80) { for (uint8_t i = 0; i < 4; i++) { + //for (uint8_t i = 0; i < 2; i++) { type = shift(0x00); //controller ID padding = shift(0x00); //controller ID //type = shift(0x00); //controller ID //padding = shift(0x00); //controller ID data1 = shift(0x00); //buttons data2 = shift(0x00); //buttons - data3 = shift(0x00); //left analog - data4 = shift(0x00); //left analog - data5 = shift(0x00); //right analog - data6 = shift(0x00); //right analog - if (type == 0x73) { - Joystick[i].xAxis = data5; - Joystick[i].yAxis = data6; - } else { - Joystick[i].xAxis = 128; - Joystick[i].yAxis = 128; - if (!bitRead(data1,4)) Joystick[i].yAxis = 0; - if (!bitRead(data1,5)) Joystick[i].xAxis = 255; - if (!bitRead(data1,6)) Joystick[i].yAxis = 255; - if (!bitRead(data1,7)) Joystick[i].xAxis = 0; + data3 = shift(0x00); //right analog + data4 = shift(0x00); //right analog + data5 = shift(0x00); //left analog + data6 = shift(0x00); //left analog + if (type != 0x41) { + Joystick[i].xAxis = data3; + Joystick[i].yAxis = data4; + Joystick[i].xAxis2 = data5; + Joystick[i].yAxis2 = data6; + } else { + Joystick[i].xAxis = 127;//0; + Joystick[i].yAxis = 127;//0; + Joystick[i].xAxis2 = 127;//0; + Joystick[i].yAxis2 = 127;//0; + } /* + if (!bitRead(data1,4)) Joystick[i].yAxis2 = 0; + if (!bitRead(data1,5)) Joystick[i].xAxis2 = 255; + if (!bitRead(data1,6)) Joystick[i].yAxis2 = 255; + if (!bitRead(data1,7)) Joystick[i].xAxis2 = 0; data1 = data1 | B11110000; - } + }*/ Joystick[i].buttons = ~( (data1) | (data2 << 8) ); } } @@ -255,6 +290,7 @@ void loop() { }*/ digitalWrite(ATT1, HIGH); + #ifdef DEBUG /*Serial.print(" head/type/padding: 0x"); Serial.print(head, HEX); Serial.print(" 0x"); Serial.print(type, HEX); Serial.print(" 0x"); Serial.print(padding, HEX); @@ -276,14 +312,14 @@ void loop() { Serial.print(" rest: "); Serial.print(data3, DEC); Serial.print(" "); Serial.print(data4, DEC); Serial.print(" "); Serial.print(data5, DEC); Serial.print(" "); Serial.print(data6, DEC);*/ Serial.println(); Serial.flush(); - //Serial.print(" type2: "); Serial.print(type, HEX); //Serial.println(); + #endif //delayMicroseconds(50); Joystick[0].sendState(); Joystick[1].sendState(); Joystick[2].sendState(); Joystick[3].sendState(); - delay(50); -} + delay(50);} +