Working Dualshock output reports on macOS; fix runaway loop

This commit is contained in:
Jack Andersen
2017-05-11 09:12:44 -10:00
parent 7823aecc57
commit 6e1c71aefd
6 changed files with 101 additions and 134 deletions

View File

@@ -17,14 +17,15 @@ static inline uint16_t bswap16(uint16_t val) {return __builtin_byteswap(val);}
namespace boo
{
static const uint8_t defaultReport[35] = {
0x01, 0xff, 0x00, 0xff, 0x00,
0xff, 0x80, 0x00, 0x00, 0x00,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0x00, 0x00, 0x00, 0x00, 0x00
static const uint8_t defaultReport[49] = {
0x01,
0x01, 0xff, 0x00, 0xff, 0x00,
0xff, 0x80, 0x00, 0x00, 0x00,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0xff, 0x27, 0x10, 0x00, 0x32,
0x00, 0x00, 0x00, 0x00, 0x00
};
DualshockPad::DualshockPad(DeviceToken* token)
@@ -33,7 +34,7 @@ DualshockPad::DualshockPad(DeviceToken* token)
m_rumbleRequest(EDualshockMotor::None),
m_rumbleState(EDualshockMotor::None)
{
memcpy(m_report.buf, defaultReport, 35);
memcpy(m_report.buf, defaultReport, 49);
}
DualshockPad::~DualshockPad()
@@ -49,8 +50,9 @@ void DualshockPad::deviceDisconnected()
void DualshockPad::initialCycle()
{
uint8_t setupCommand[4] = {0x42, 0x0c, 0x00, 0x00}; //Tells controller to start sending changes on in pipe
if (!sendHIDReport(setupCommand, sizeof(setupCommand), HIDReportType::Feature, 0xF4))
#if 0
uint8_t setupCommand[5] = {0xF4, 0x42, 0x0c, 0x00, 0x00}; //Tells controller to start sending changes on in pipe
if (!sendHIDReport(setupCommand, 5, HIDReportType::Feature, 0xF4))
{
deviceError("Unable to send complete packet! Request size %x\n", sizeof(setupCommand));
return;
@@ -59,6 +61,7 @@ void DualshockPad::initialCycle()
receiveHIDReport(btAddr, sizeof(btAddr), HIDReportType::Feature, 0xF5);
for (int i = 0; i < 6; i++)
m_btAddress[5 - i] = btAddr[i + 2]; // Copy into buffer reversed, so it is LSB first
#endif
}
void DualshockPad::transferCycle()
@@ -67,7 +70,11 @@ void DualshockPad::transferCycle()
void DualshockPad::finalCycle()
{
m_report.rumble.leftDuration = 0;
m_report.rumble.leftForce = 0;
m_report.rumble.rightDuration = 0;
m_report.rumble.rightOn = false;
sendHIDReport(m_report.buf, sizeof(m_report), HIDReportType::Output, 0x01);
}
void DualshockPad::receivedHIDReport(const uint8_t* data, size_t length, HIDReportType tp, uint32_t message)
@@ -80,6 +87,14 @@ void DualshockPad::receivedHIDReport(const uint8_t* data, size_t length, HIDRepo
state.m_accelerometer[i] = bswap16(state.m_accelerometer[i]);
state.m_gyrometerZ = bswap16(state.m_gyrometerZ);
const double zeroG = 511.5; // 1.65/3.3*1023 (1,65V);
float accXval = -((double)state.m_accelerometer[0] - zeroG);
float accYval = -((double)state.m_accelerometer[1] - zeroG);
float accZval = -((double)state.m_accelerometer[2] - zeroG);
state.accPitch = (atan2(accYval, accZval) + M_PI) * RAD_TO_DEG;
state.accYaw = (atan2(accXval, accZval) + M_PI) * RAD_TO_DEG;
state.gyroZ = (state.m_gyrometerZ / 1023.f);
if (m_callback)
m_callback->controllerUpdate(*this, state);
@@ -98,8 +113,8 @@ void DualshockPad::receivedHIDReport(const uint8_t* data, size_t length, HIDRepo
if ((m_rumbleRequest & EDualshockMotor::Right) != EDualshockMotor::None)
{
m_report.rumble.rightDuration = m_rumbleDuration[0];
m_report.rumble.rightOn = true;
m_report.rumble.rightDuration = m_rumbleDuration[1];
m_report.rumble.rightOn = m_rumbleIntensity[1] > 0;
}
else
{
@@ -116,13 +131,6 @@ void DualshockPad::receivedHIDReport(const uint8_t* data, size_t length, HIDRepo
if (state.m_reserved5[7] & 0x01)
m_rumbleRequest &= ~EDualshockMotor::Left;
m_rumbleState = m_rumbleRequest;
const double zeroG = 511.5; // 1.65/3.3*1023 (1,65V);
float accXval = -((double)state.m_accelerometer[0] - zeroG);
float accYval = -((double)state.m_accelerometer[1] - zeroG);
float accZval = -((double)state.m_accelerometer[2] - zeroG);
state.accPitch = (atan2(accYval, accZval) + M_PI) * RAD_TO_DEG;
state.accYaw = (atan2(accXval, accZval) + M_PI) * RAD_TO_DEG;
state.gyroZ = (state.m_gyrometerZ / 1023.f);
}
}