Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Refined report translation process
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Oct 2, 2015
1 parent e2c4b30 commit 64bd780
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 82 deletions.
92 changes: 91 additions & 1 deletion ScpControl/Usb/UsbDs3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public static void SetDpadLeftDigital(this ReportEventArgs args, bool input)
args.Report[10] |= (byte)(input ? 0x80 : 0x00);
}

public static void SetDpadUpDgital(this ReportEventArgs args, bool input)
public static void SetDpadUpDigital(this ReportEventArgs args, bool input)
{
args.Report[10] |= (byte)(input ? 0x10 : 0x00);
}
Expand All @@ -358,6 +358,11 @@ public static void ZeroSelectStartButtonsState(this ReportEventArgs args)
args.Report[10] = 0x00;
}

public static void ZeroPsButtonsState(this ReportEventArgs args)
{
args.Report[12] = 0x00;
}

public static void SetSelect(this ReportEventArgs args, int input)
{
args.Report[10] |= (byte) (input & 1);
Expand All @@ -368,6 +373,11 @@ public static void SetStart(this ReportEventArgs args, int input)
args.Report[10] |= (byte)((input & 1) << 3);
}

public static void SetPs(this ReportEventArgs args, int input)
{
args.Report[12] |= (byte) (input & 1);
}

public static void SetLeftShoulderDigital(this ReportEventArgs args, int input)
{
args.Report[11] |= (byte) ((input & 1) << 2);
Expand All @@ -377,5 +387,85 @@ public static void SetRightShoulderDigital(this ReportEventArgs args, int input)
{
args.Report[11] |= (byte)((input & 1) << 3);
}

public static void SetLeftTriggerDigital(this ReportEventArgs args, int input)
{
args.Report[11] |= (byte)((input & 1) << 0);
}

public static void SetRightTriggerDigital(this ReportEventArgs args, int input)
{
args.Report[11] |= (byte)((input & 1) << 1);
}

public static void SetLeftShoulderAnalog(this ReportEventArgs args, byte input)
{
args.Report[28] = input;
}

public static void SetRightShoulderAnalog(this ReportEventArgs args, byte input)
{
args.Report[29] = input;
}

public static void SetLeftTriggerAnalog(this ReportEventArgs args, byte input)
{
args.Report[26] = input;
}

public static void SetRightTriggerAnalog(this ReportEventArgs args, byte input)
{
args.Report[27] = input;
}

public static void SetTriangleAnalog(this ReportEventArgs args, byte input)
{
args.Report[30] = input;
}

public static void SetCircleAnalog(this ReportEventArgs args, byte input)
{
args.Report[31] = input;
}

public static void SetCrossAnalog(this ReportEventArgs args, byte input)
{
args.Report[32] = input;
}

public static void SetSquareAnalog(this ReportEventArgs args, byte input)
{
args.Report[33] = input;
}

public static void SetLeftThumb(this ReportEventArgs args, int input)
{
args.Report[10] |= (byte)((input & 1) << 1);
}

public static void SetRightThumb(this ReportEventArgs args, int input)
{
args.Report[10] |= (byte)((input & 1) << 2);
}

public static void SetLeftAxisY(this ReportEventArgs args, byte input)
{
args.Report[15] = input;
}

public static void SetLeftAxisX(this ReportEventArgs args, byte input)
{
args.Report[14] = input;
}

public static void SetRightAxisY(this ReportEventArgs args, byte input)
{
args.Report[17] = input;
}

public static void SetRightAxisX(this ReportEventArgs args, byte input)
{
args.Report[16] = input;
}
}
}
153 changes: 93 additions & 60 deletions ScpControl/Usb/UsbDs3Afterglow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,121 @@ public class UsbDs3Afterglow : UsbDs3
{
protected override void Parse(byte[] report)
{
ConvertAfterglowToValidBytes(ref report);
//ConvertAfterglowToValidBytes(ref report);

base.Parse(report);
}

/// <summary>
/// Prototype helper method to convert input byte stream of Afterglow Wireless Controllers to valid PS3 packet.
/// </summary>
/// <param name="b">The input byte stream captured from the controller.</param>
private static void ConvertAfterglowToValidBytes(ref byte[] b)
{
// identify the fake controller
// TODO: add more checks
if (b[26] != 0x02) return;
if (report[26] != 0x02) return;

// prepare temporary array for input values
var input = new byte[28];

// copy source array and zero out bytes
for (int i = 0; i < 27; i++)
if (m_Packet++ + 1 < m_Packet)
{
input[i] = b[i];
b[i] = 0x00;
Log.WarnFormat("Packet counter rolled over ({0}), resetting to 0", m_Packet);
m_Packet = 0;
}

b[4] |= (byte)((input[1] >> 4) & 1); // PS
b[2] |= (byte)((input[1] >> 0) & 1); // Select
b[2] |= (byte)(((input[1] >> 1) & 1) << 3); // Start
#region HID Report translation

b[3] |= (byte)(((input[0] >> 4) & 1) << 2); // L1 (button)
b[20] = input[15]; // L1 (analog)
// overwrite Report ID
m_ReportArgs.Report[0] = 0x01;

b[3] |= (byte)(((input[0] >> 5) & 1) << 3); // R1 (button)
b[21] = input[16]; // R1 (analog)
// battery
m_BatteryStatus = m_ReportArgs.SetBatteryStatus(report);

b[3] |= (byte)(((input[0] >> 6) & 1) << 0); // L2 (button)
b[18] = input[17]; // L2 (analog)
// packet counter
m_ReportArgs.SetPacketCounter(m_Packet);

b[3] |= (byte)(((input[0] >> 7) & 1) << 1); // R2 (button)
b[19] = input[18]; // R2 (analog)
// null button states
m_ReportArgs.ZeroPsButtonsState();
m_ReportArgs.ZeroSelectStartButtonsState();
m_ReportArgs.ZeroShoulderButtonsState();

b[3] |= (byte)(((input[0] >> 3) & 1) << 4); // Triangle (button)
b[22] = input[11]; // Triangle (analog)
// control buttons
m_ReportArgs.SetPs(report[1] >> 4);
m_ReportArgs.SetSelect(report[1] >> 0);
m_ReportArgs.SetStart(report[1] >> 1);

b[3] |= (byte)(((input[0] >> 2) & 1) << 5); // Circle (button)
b[23] = input[12]; // Circle (analog)
// Left shoulder
m_ReportArgs.SetLeftShoulderDigital(report[0] >> 4);
m_ReportArgs.SetLeftShoulderAnalog(report[15]);

b[3] |= (byte)(((input[0] >> 1) & 1) << 6); // Cross (button)
b[24] = input[13]; // Cross (analog)
// Right shoulder
m_ReportArgs.SetRightShoulderDigital(report[0] >> 5);
m_ReportArgs.SetRightShoulderAnalog(report[16]);

b[3] |= (byte)(((input[0] >> 0) & 1) << 7); // Square (button)
b[25] = input[14]; // Square (analog)
// Left trigger
m_ReportArgs.SetLeftTriggerDigital(report[0] >> 6);
m_ReportArgs.SetLeftTriggerAnalog(report[17]);

if (input[2] != 0x0F)
{
b[2] |= (byte)((input[2] == 0x02) ? 0x20 : 0x00); // D-Pad right
b[15] = input[7]; // D-Pad right
// Right trigger
m_ReportArgs.SetRightTriggerDigital(report[0] >> 7);
m_ReportArgs.SetRightTriggerAnalog(report[18]);

b[2] |= (byte)((input[2] == 0x06) ? 0x80 : 0x00); // D-Pad left
b[17] = input[8]; // D-Pad left
// Triangle
m_ReportArgs.SetTriangleDigital(report[0] >> 3);
m_ReportArgs.SetTriangleAnalog(report[11]);

b[2] |= (byte)((input[2] == 0x00) ? 0x10 : 0x00); // D-Pad up
b[14] = input[9]; // D-Pad up
// Circle
m_ReportArgs.SetCircleDigital(report[0] >> 2);
m_ReportArgs.SetCircleAnalog(report[12]);

b[2] |= (byte)((input[2] == 0x04) ? 0x40 : 0x00); // D-Pad down
b[16] = input[10]; // D-Pad down
// Cross
m_ReportArgs.SetCrossDigital(report[0] >> 1);
m_ReportArgs.SetCrossAnalog(report[13]);

// Square
m_ReportArgs.SetSquareDigital(report[0] >> 0);
m_ReportArgs.SetSquareAnalog(report[14]);

// Left thumb
m_ReportArgs.SetLeftThumb(report[1] >> 2);
// Right thumb
m_ReportArgs.SetRightThumb(report[1] >> 3);

// D-Pad
if (report[2] != 0x0F)
{
switch (report[2])
{
case 0:
m_ReportArgs.SetDpadUpDigital(true);
break;
case 1:
m_ReportArgs.SetDpadUpDigital(true);
m_ReportArgs.SetDpadRightDigital(true);
break;
case 2:
m_ReportArgs.SetDpadRightDigital(true);
break;
case 3:
m_ReportArgs.SetDpadRightDigital(true);
m_ReportArgs.SetDpadDownDigital(true);
break;
case 4:
m_ReportArgs.SetDpadDownDigital(true);
break;
case 5:
m_ReportArgs.SetDpadDownDigital(true);
m_ReportArgs.SetDpadLeftDigital(true);
break;
case 6:
m_ReportArgs.SetDpadLeftDigital(true);
break;
case 7:
m_ReportArgs.SetDpadLeftDigital(true);
m_ReportArgs.SetDpadUpDigital(true);
break;
}
}

b[7] = input[4]; // Left Axis Y+
b[7] = input[4]; // Left Axis Y-
b[6] = input[3]; // Left Axis X-
b[6] = input[3]; // Left Axis X+
// Left thumb stick
m_ReportArgs.SetLeftAxisY(report[4]);
m_ReportArgs.SetLeftAxisX(report[3]);

b[9] = input[6]; // Right Axis Y+
b[9] = input[6]; // Right Axis Y-
b[8] = input[5]; // Right Axis X-
b[8] = input[5]; // Right Axis X+
// Right thumb stick
m_ReportArgs.SetRightAxisY(report[6]);
m_ReportArgs.SetRightAxisX(report[5]);

b[2] |= (byte)(((input[1] >> 2) & 1) << 1); // Left Thumb
b[2] |= (byte)(((input[1] >> 3) & 1) << 2); // Right Thumb
#endregion

b[0] = 0x01;
OnHidReportReceived();
}

public override bool Pair(byte[] master)
Expand Down
31 changes: 10 additions & 21 deletions ScpControl/Usb/UsbDs3SnesGamepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,31 @@ protected override void Parse(byte[] report)
// packet counter
m_ReportArgs.SetPacketCounter(m_Packet);

// reset buttons
m_ReportArgs.ZeroSelectStartButtonsState();
m_ReportArgs.ZeroShoulderButtonsState();

m_ReportArgs.SetSelect(report[6] >> 4); // Select
m_ReportArgs.SetStart(report[6] >> 5); // Start

m_ReportArgs.ZeroShoulderButtonsState();

m_ReportArgs.SetLeftShoulderDigital(report[6] >> 0); // L1 (button)
//m_ReportArgs.Report[20] = input[15]; // L1 (analog)

m_ReportArgs.SetRightShoulderDigital(report[6] >> 2); // R1 (button)
//m_ReportArgs.Report[21] = input[16]; // R1 (analog)

m_ReportArgs.SetTriangleDigital(report[5] >> 4); // Triangle (button)
//m_ReportArgs.Report[22] = report[11]; // Triangle (analog)

m_ReportArgs.SetCircleDigital(report[5] >> 5); // Circle (button)
//m_ReportArgs.Report[23] = report[12]; // Circle (analog)

m_ReportArgs.SetCrossDigital(report[5] >> 6); // Cross (button)
//m_ReportArgs.Report[24] = report[13]; // Cross (analog)

m_ReportArgs.SetSquareDigital(report[5] >> 7); // Square (button)
//m_ReportArgs.Report[25] = report[14]; // Square (analog)

m_ReportArgs.SetDpadRightDigital(report[3] == 0xFF); // D-Pad right
m_ReportArgs.Report[23] = (byte)((report[3] == 0xFF) ? 0xFF : 0x00); // D-Pad right

m_ReportArgs.SetDpadLeftDigital(report[3] == 0x00); // D-Pad left
m_ReportArgs.Report[25] = (byte)((report[3] == 0x00) ? 0xFF : 0x00); // D-Pad left

m_ReportArgs.SetDpadUpDgital(report[4] == 0x00); // D-Pad up
m_ReportArgs.Report[22] = (byte)((report[4] == 0x00) ? 0xFF : 0x00); // D-Pad up

m_ReportArgs.SetDpadUpDigital(report[4] == 0x00); // D-Pad up
m_ReportArgs.SetDpadDownDigital(report[4] == 0xFF); // D-Pad down
m_ReportArgs.Report[24] = (byte)((report[4] == 0xFF) ? 0xFF : 0x00); // D-Pad down

//m_ReportArgs.Report[12] ^= 0x1;
// This device has no thumb sticks, center axes
m_ReportArgs.SetLeftAxisY(0x7F);
m_ReportArgs.SetLeftAxisX(0x7F);
m_ReportArgs.SetRightAxisY(0x7F);
m_ReportArgs.SetRightAxisX(0x7F);

OnHidReportReceived();
}
Expand Down

0 comments on commit 64bd780

Please sign in to comment.