Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
fixed multiple JoyCon lag issue (thanks gb2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
wormyrocks committed Feb 2, 2018
1 parent d720b27 commit 22843ed
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 13 deletions.
56 changes: 56 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,56 @@
{
"files.exclude":
{
"**/.DS_Store":true,
"**/.git":true,
"**/.gitignore":true,
"**/.gitmodules":true,
"**/*.booproj":true,
"**/*.pidb":true,
"**/*.suo":true,
"**/*.user":true,
"**/*.userprefs":true,
"**/*.unityproj":true,
"**/*.dll":true,
"**/*.exe":true,
"**/*.pdf":true,
"**/*.mid":true,
"**/*.midi":true,
"**/*.wav":true,
"**/*.gif":true,
"**/*.ico":true,
"**/*.jpg":true,
"**/*.jpeg":true,
"**/*.png":true,
"**/*.psd":true,
"**/*.tga":true,
"**/*.tif":true,
"**/*.tiff":true,
"**/*.3ds":true,
"**/*.3DS":true,
"**/*.fbx":true,
"**/*.FBX":true,
"**/*.lxo":true,
"**/*.LXO":true,
"**/*.ma":true,
"**/*.MA":true,
"**/*.obj":true,
"**/*.OBJ":true,
"**/*.asset":true,
"**/*.cubemap":true,
"**/*.flare":true,
"**/*.mat":true,
"**/*.meta":true,
"**/*.prefab":true,
"**/*.unity":true,
"build/":true,
"Build/":true,
"Library/":true,
"library/":true,
"obj/":true,
"Obj/":true,
"ProjectSettings/":true,
"temp/":true,
"Temp/":true
}
}
5 changes: 5 additions & 0 deletions Assets/JoyconDemo.cs
Expand Up @@ -70,6 +70,11 @@ void Start ()
accel = j.GetAccel();

orientation = j.GetVector();
if (j.GetButton(Joycon.Button.DPAD_UP)){
gameObject.GetComponent<Renderer>().material.color = Color.red;
} else{
gameObject.GetComponent<Renderer>().material.color = Color.blue;
}
gameObject.transform.rotation = orientation;
}
}
Expand Down
19 changes: 12 additions & 7 deletions Assets/JoyconLib_scripts/Joycon.cs
Expand Up @@ -19,7 +19,7 @@ public enum DebugType : int
IMU,
RUMBLE,
};
public DebugType debug_type = DebugType.IMU;
public DebugType debug_type = DebugType.NONE;
public bool isLeft;
public enum state_ : uint
{
Expand Down Expand Up @@ -72,9 +72,6 @@ public enum Button : int
private Vector3 acc_g;

private Int16[] gyr_r = { 0, 0, 0 };
private Int16[] gyr_neutral = { 0, 0, 0 };
private Vector3 gyr_g;
private Int16[] gyr_r = { 0, 0, 0 };
private Int16[] gyr_neutral = { 0, 0, 0 };
private Vector3 gyr_g;
private bool do_localize;
Expand Down Expand Up @@ -233,7 +230,13 @@ public Vector3 GetAccel()
}
public Quaternion GetVector()
{
return Quaternion.LookRotation(new Vector3(j_b.x, i_b.x, k_b.x), -(new Vector3(j_b.z, i_b.z, k_b.z)));
Vector3 v1 = new Vector3(j_b.x, i_b.x, k_b.x);
Vector3 v2 = -(new Vector3(j_b.z, i_b.z, k_b.z));
if (v2 != Vector3.zero){
return Quaternion.LookRotation(v1, v2);
}else{
return Quaternion.identity;
}
}
public int Attach(byte leds_ = 0x0)
{
Expand Down Expand Up @@ -312,7 +315,7 @@ private void Poll()
{
SendRumble(rumble_obj.GetData());
int a = ReceiveRaw();

a = ReceiveRaw();
if (a > 0)
{
state = state_.IMU_DATA_OK;
Expand Down Expand Up @@ -367,6 +370,9 @@ public void Update()
DebugPrint(string.Format("Dequeue. Queue length: {0:d}. Packet ID: {1:X2}. Timestamp: {2:X2}. Lag to dequeue: {3:s}. Lag between packets (expect 15ms): {4:s}",
reports.Count, report_buf[0], report_buf[1], System.DateTime.Now.Subtract(rep.GetTime()), rep.GetTime().Subtract(ts_prev)), DebugType.THREADING);
ts_prev = rep.GetTime();
if (reports.Count > 1){
Debug.Log(reports.Count);
}
}
ProcessButtonsAndStick(report_buf);
if (rumble_obj.timed_rumble) {
Expand Down Expand Up @@ -503,7 +509,6 @@ private int ProcessIMU(byte[] report_buf)
i_b = i_b_;
k_b = Vector3.Cross(i_b, j_b);
}

dt = 1;
}
timestamp = report_buf[1] + 2;
Expand Down
8 changes: 3 additions & 5 deletions Assets/JoyconLib_scripts/JoyconManager.cs
Expand Up @@ -64,7 +64,7 @@ void Awake()
}
IntPtr handle = HIDapi.hid_open_path (enumerate.path);
HIDapi.hid_set_nonblocking (handle, 1);
j.Add (new Joycon (handle, EnableIMU, EnableLocalize & EnableIMU, 0.04f, isLeft));
j.Add (new Joycon (handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft));
++i;
}
ptr = enumerate.next;
Expand All @@ -89,17 +89,15 @@ void Update()
{
for (int i = 0; i < j.Count; ++i)
{
Joycon jc = j [i];
jc.Update ();
j[i].Update();
}
}

void OnApplicationQuit()
{
for (int i = 0; i < j.Count; ++i)
{
Joycon jc = j [i];
jc.Detach ();
j[i].Detach ();
}
}
}
2 changes: 2 additions & 0 deletions Assets/Scenes/Scene1.unity
Expand Up @@ -88,6 +88,7 @@ LightmapSettings:
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_ShowResolutionOverlay: 1
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
--- !u!196 &4
Expand Down Expand Up @@ -492,6 +493,7 @@ Camera:
m_TargetEye: 3
m_HDR: 1
m_AllowMSAA: 1
m_AllowDynamicResolution: 0
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectVersion.txt
@@ -1 +1 @@
m_EditorVersion: 2017.2.0f3
m_EditorVersion: 2017.3.0f3

0 comments on commit 22843ed

Please sign in to comment.