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

Commit

Permalink
Added multi support for LibUSB and WinUSB
Browse files Browse the repository at this point in the history
  • Loading branch information
gusmanb committed Nov 3, 2016
1 parent 1dd4cc9 commit 7c0756b
Show file tree
Hide file tree
Showing 188 changed files with 1,021 additions and 26,031 deletions.
4 changes: 0 additions & 4 deletions PSVRFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,17 @@ Global
{168EDAE3-110E-419E-9013-A26FE4F2B6EB}.Release|x86.ActiveCfg = Release|Any CPU
{168EDAE3-110E-419E-9013-A26FE4F2B6EB}.Release|x86.Build.0 = Release|Any CPU
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Debug|Any CPU.ActiveCfg = Debug|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Debug|Any CPU.Build.0 = Debug|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Debug|x64.ActiveCfg = Debug|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Debug|x64.Build.0 = Debug|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Debug|x86.ActiveCfg = Debug|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Debug|x86.Build.0 = Debug|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Release|Any CPU.ActiveCfg = Release|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Release|x64.ActiveCfg = Release|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Release|x64.Build.0 = Release|x64
{16FCBDA3-0C18-4F11-AF62-510B051FDBFB}.Release|x86.ActiveCfg = Release|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Debug|Any CPU.ActiveCfg = Debug|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Debug|Any CPU.Build.0 = Debug|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Debug|x64.ActiveCfg = Debug|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Debug|x64.Build.0 = Debug|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Debug|x86.ActiveCfg = Debug|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Debug|x86.Build.0 = Debug|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Release|Any CPU.ActiveCfg = Release|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Release|x64.ActiveCfg = Release|x64
{9B40E68F-7090-4FFC-AAC7-2E357CEF34C7}.Release|x64.Build.0 = Release|x64
Expand Down
34 changes: 34 additions & 0 deletions PSVRFramework.sln.startup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
This is a configuration file for the SwitchStartupProject Visual Studio Extension
See https://bitbucket.org/thirteen/switchstartupproject/src/tip/Configuration.md
*/
{
/* Configuration File Version */
"Version": 3,

/* Create an item in the dropdown list for each project in the solution? */
"ListAllProjects": true,

/*
Dictionary of named startup project configurations with one or multiple
startup projects and optional command line arguments.
Example:

"MultiProjectConfigurations": {
"A + B": {
"Projects": {
"MyProjectA": {},
"MyProjectB": {
"CommandLineArguments": "1234"
}
}
},
"D": {
"Projects": {
"MyProjectD": {}
}
}
},
*/
"MultiProjectConfigurations": {}
}
242 changes: 143 additions & 99 deletions PSVRFramework/PSVR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,46 @@

namespace PSVRFramework
{
public struct PSVRSensor
public class PSVRSensor
{
//To be analyzed...
public HeadsetButtons Buttons;
public int Volume;
public bool Worn;
public bool DisplayActive;
public bool Muted;
public bool EarphonesConnected;

public int volume;
public bool isWorn;
public bool isDisplayActive;
public bool isMute;
public bool isEarphoneConnected;
public int GroupSequence1;

public int GyroYaw1;
public int GyroPitch1;
public int GyroRoll1;

public int MotionX1;
public int MotionY1;
public int MotionZ1;

public int GroupSequence2;

public int GyroYaw2;
public int GyroPitch2;
public int GyroRoll2;

public int MotionX2;
public int MotionY2;
public int MotionZ2;


public int GyroYaw1;
public int GyroPitch1;
public int GyroRoll1;
public int IRSensor; //1023 = near, 0 = far

public int GyroYaw2;
public int GyroPitch2;
public int GyroRoll2;
public int CalStatus; //Calibration status? 255 = boot, 0-3 calibrating? 4 = calibrated, maybe a bit mask with sensor status? (0 bad, 1 good)?
public int Ready; //0 = not ready, 1 = ready -> uint ushort or byte?

public int PacketSequence;

public int VoltageReference; //Not sure at all, starts in 0 and suddenly jumps to 3
public int VoltageValue; //Not sure at all, starts on 0, ranges very fast to 255, when switched from VR to Cinematic and back varies between 255 and 254

//DEBUG
public int A;
Expand All @@ -45,24 +59,24 @@ public struct PSVRSensor
public int F;
public int G;
public int H;
public int I;
public int J;
public int K;
public int L;
public int M;
public int N;
public int O;

//For future use, will hold a Quaternion with the head orientation
public float[] Orientation;
}

[Flags]
public enum HeadsetButtons
{
VolUp = 2,
VolDown = 4,
Mute = 8
}

public struct PSVRState
{
public PSVRSensor sensor;
}




public class PSVR : IDisposable
{
UsbEndpointWriter writer;
Expand All @@ -74,40 +88,81 @@ public class PSVR : IDisposable
public event EventHandler Removed;

Timer aliveTimer;

public PSVR()
public PSVR(bool UseLibUSB)
{
var ndev = UsbDevice.AllWinUsbDevices.Where(d => d.Vid == 0x54C && d.Name == "PS VR Control (Interface 5)").FirstOrDefault();

if (ndev == null)
throw new InvalidOperationException("No Control device found");
if (!UseLibUSB)
{

if (!ndev.Open(out controlDevice))
throw new InvalidOperationException("Device in use");
var ndev = UsbDevice.AllWinUsbDevices.Where(d => d.Vid == 0x54C && d.Name == "PS VR Control (Interface 5)").FirstOrDefault();

if (ndev == null)
throw new InvalidOperationException("No Control device found");

if (!ndev.Open(out controlDevice))
throw new InvalidOperationException("Device in use");

ndev = UsbDevice.AllWinUsbDevices.Where(d => d.Vid == 0x54C && d.Name == "PS VR Sensor (Interface 4)").FirstOrDefault();

if (ndev == null)
{
controlDevice.Close();
throw new InvalidOperationException("No Sensor device found");
}
if (!ndev.Open(out sensorDevice))
{
controlDevice.Close();
throw new InvalidOperationException("Device in use");
}

writer = controlDevice.OpenEndpointWriter(LibUsbDotNet.Main.WriteEndpointID.Ep04);

reader = sensorDevice.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep03, 64);
reader.DataReceived += Reader_DataReceived;
reader.DataReceivedEnabled = true;

aliveTimer = new Timer(is_alive);
aliveTimer.Change(2000, 2000);

ndev = UsbDevice.AllWinUsbDevices.Where(d => d.Vid == 0x54C && d.Name == "PS VR Sensor (Interface 4)").FirstOrDefault();

if (ndev == null)
{
controlDevice.Close();
throw new InvalidOperationException("No Sensor device found");
}
if (!ndev.Open(out sensorDevice))
else
{
controlDevice.Close();
throw new InvalidOperationException("Device in use");
}
LibUsbDotNet.Main.UsbDeviceFinder find = new LibUsbDotNet.Main.UsbDeviceFinder(0x054C, 0x09AF);
controlDevice = LibUsbDotNet.UsbDevice.OpenUsbDevice(find);

if (controlDevice == null)
throw new InvalidOperationException("No device found");

var dev = (IUsbDevice)controlDevice;

for (int ifa = 0; ifa < controlDevice.Configs[0].InterfaceInfoList.Count; ifa++)
{
var iface = controlDevice.Configs[0].InterfaceInfoList[ifa];

writer = controlDevice.OpenEndpointWriter(LibUsbDotNet.Main.WriteEndpointID.Ep04);
if (iface.Descriptor.InterfaceID == 5)
{
dev.SetConfiguration(1);

reader = sensorDevice.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep03, 64);
reader.DataReceived += Reader_DataReceived;
reader.DataReceivedEnabled = true;
var res = dev.ClaimInterface(5);
res = dev.ClaimInterface(4);

aliveTimer = new Timer(is_alive);
aliveTimer.Change(2000, 2000);
writer = dev.OpenEndpointWriter(LibUsbDotNet.Main.WriteEndpointID.Ep04);

reader = dev.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep03, 64);
reader.DataReceived += Reader_DataReceived;
reader.DataReceivedEnabled = true;

aliveTimer = new Timer(is_alive);
aliveTimer.Change(2000, 2000);

return;
}
}

throw new InvalidOperationException("Device does not match descriptor");
}
}

void is_alive(object state)
Expand Down Expand Up @@ -156,77 +211,54 @@ public static PSVRSensor parseSensor(byte[] data)
return sensor;
}

sensor.Buttons = (HeadsetButtons)data[0];
sensor.Volume = data[2];

/*
* sample data
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
00-00-00-19-00-0A-04-20-00-06-1D-01-FF-7F-00-00-42-AB-D7-69-00-FE-FF-F4-FF-16-00-E1-CF-1F-E0-E1-E9-9F-D9-69-00-FC-FF-EF-FF-24-00-C1-CF-1F-E0-B1-E9-1D-01-00-00-00-03-FF-82-00-00-00-00-00-BC-01-6D
00-00-00-19-00-0A-04-20-00-06-1D-01-FF-7F-00-00-42-93-DB-69-00-FF-FF-FA-FF-13-00-D1-CF-2F-E0-81-E9-88-DD-69-00-FC-FF-0A-00-FC-FF-11-D0-FF-DF-D1-E9-1D-01-00-00-00-03-FF-82-00-00-00-00-00-7D-01-6E
00-00-00-19-00-0A-04-20-00-06-1D-01-FF-7F-00-00-42-7B-DF-69-00-08-00-0E-00-EE-FF-F1-CF-EF-DF-01-EA-70-E1-69-00-04-00-09-00-F9-FF-E1-CF-1F-E0-B1-E9-1D-01-00-00-00-03-FF-82-00-00-00-00-00-84-01-6F
[byte 3]
Volume (0~50)
[byte 9]
0000 0001 Worn (Mounted on Player)
0000 0010 Display is On
0000 0100 ??
0000 1000 Mute
0001 0000 Earphone
0010 0000
0100 0000
1000 0000
reference:
https://github.com/hrl7/node-psvr/blob/master/lib/psvr.js
*/


sensor.volume = data[2];

sensor.isWorn = (data[8] & 0x1) == 0x1 ? true : false;//confirmed
sensor.isDisplayActive = (data[8] & 0x2) == 0x2 ? false : true;
sensor.isMute = (data[8] & 0x8) == 0x8 ? true : false;//confirmed

sensor.isEarphoneConnected = (data[8] & 0x10) == 0x10 ? true : false;//confirmed

sensor.Worn = (data[8] & 0x1) == 0x1 ? true : false;//confirmed
sensor.DisplayActive = (data[8] & 0x2) == 0x2 ? false : true;
sensor.Muted = (data[8] & 0x8) == 0x8 ? true : false;//confirmed

sensor.MotionX1 = getIntFromInt16(data, 26);
sensor.MotionY1 = getIntFromInt16(data, 28);
sensor.MotionZ1 = getIntFromInt16(data, 30);
sensor.EarphonesConnected = (data[8] & 0x10) == 0x10 ? true : false;//confirmed

sensor.MotionX2 = getIntFromInt16(data, 42);
sensor.MotionY2 = getIntFromInt16(data, 44);
sensor.MotionZ2 = getIntFromInt16(data, 46);
sensor.GroupSequence1 = getIntFromInt16(data, 18);

sensor.GyroYaw1 = getIntFromInt16(data, 20);
sensor.GyroPitch1 = getIntFromInt16(data, 22);
sensor.GyroRoll1 = getIntFromInt16(data, 24);

sensor.MotionX1 = getAccelShort(data, 26) ;
sensor.MotionY1 = getAccelShort(data, 28);
sensor.MotionZ1 = getAccelShort(data, 30);

sensor.GroupSequence2 = getIntFromInt16(data, 34);

sensor.GyroYaw2 = getIntFromInt16(data, 36);
sensor.GyroPitch2 = getIntFromInt16(data, 38);
sensor.GyroRoll2 = getIntFromInt16(data, 40);

sensor.MotionX2 = getAccelShort(data, 42);
sensor.MotionY2 = getAccelShort(data, 44);
sensor.MotionZ2 = getAccelShort(data, 46);

sensor.CalStatus = data[48];
sensor.Ready = data[49];

sensor.A = convert(data[18], data[19]);
sensor.B = convert(data[20], data[21]);
sensor.C = convert(data[22], data[23]);

sensor.D = convert(data[24], data[25]);
sensor.E = convert(data[26], data[27]);//下方向加速度
sensor.F = convert(data[28], data[29]);//左方向加速度

sensor.G = convert(data[30], data[31]);//後ろ方向加速度
sensor.H = convert(data[32], data[33]);
sensor.I = convert(data[34], data[37]);

sensor.J = convert(data[36], data[37]);
sensor.K = convert(data[38], data[39]);
sensor.L = convert(data[40], data[41]);
sensor.A = data[50];
sensor.B = data[51];
sensor.C = data[52];

sensor.M = convert(data[42], data[43]);//下方向加速度
sensor.N = convert(data[44], data[45]);//左方向加速度
sensor.O = convert(data[46], data[47]);//後ろ方向加速度
sensor.VoltageValue = data[53];
sensor.VoltageReference = data[54];
sensor.IRSensor = getIntFromInt16(data, 55);

sensor.D = data[58];
sensor.E = data[59];
sensor.F = data[60];
sensor.G = data[61];
sensor.H = data[62];

sensor.PacketSequence = data[63];

return sensor;

}
Expand All @@ -242,6 +274,18 @@ private static int getIntFromInt16(byte[] data, byte offset)
return (short)data[offset] | (short)(data[offset + 1] << 8);
}

private static short getAccelShort(byte[] data, byte offset)
{

return (short)(((short)data[offset] | (short)(data[offset + 1] << 8)) >> 4);
}

private static int getIntFromUInt16(byte[] data, byte offset)
{

return (ushort)data[offset] | (ushort)(data[offset + 1] << 8);
}

public void Dispose()
{
aliveTimer.Change(Timeout.Infinite, Timeout.Infinite);
Expand Down
Loading

0 comments on commit 7c0756b

Please sign in to comment.