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

Commit

Permalink
One thread implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wormyrocks committed Sep 26, 2017
1 parent 62f5d48 commit 5fe23d2
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions Assets/JoyconLib_scripts/Joycon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private float clamp(float x, float min, float max)
}
public byte[] GetData()
{

byte[] rumble_data = new byte[8];
l_f = clamp(l_f, 40.875885f, 626.286133f);
amp = clamp(amp, 0.0f, 1.0f);
Expand All @@ -136,7 +137,6 @@ public byte[] GetData()
rumble_data[0] = (byte)(hf & 0xff);
rumble_data[1] = (byte)((hf >> 8) & 0xff);
rumble_data[2] = lf;

rumble_data[1] += hf_amp;
rumble_data[2] += (byte)((lf_amp >> 8) & 0xff);
rumble_data[3] += (byte)(lf_amp & 0xff);
Expand All @@ -147,9 +147,9 @@ public byte[] GetData()
//Debug.Log(string.Format("Encoded hex freq: {0:X2}", encoded_hex_freq));
//Debug.Log(string.Format("lf_amp: {0:X4}", lf_amp));
//Debug.Log(string.Format("hf_amp: {0:X2}", hf_amp));
Debug.Log(string.Format("l_f: {0:D}", l_f));
Debug.Log(string.Format("hf: {0:X4}", hf));
Debug.Log(string.Format("lf: {0:X2}", lf));
//Debug.Log(string.Format("l_f: {0:F}", l_f));
//Debug.Log(string.Format("hf: {0:X4}", hf));
//Debug.Log(string.Format("lf: {0:X2}", lf));
return rumble_data;
}
}
Expand All @@ -164,7 +164,7 @@ public enum DebugType : int
IMU,
RUMBLE,
};
public DebugType debug_type = DebugType.RUMBLE;
public DebugType debug_type = DebugType.COMMS;
private byte global_count = 0;
private string debug_str;

Expand Down Expand Up @@ -297,10 +297,10 @@ public void Detach()
private byte ts_en;
private byte ts_de;
private System.DateTime ts_prev;
private int ReceiveRaw(bool block = false)
private int ReceiveRaw()
{
if (handle == IntPtr.Zero) return -2;
HIDapi.hid_set_nonblocking(handle, block ? 0 : 1);
HIDapi.hid_set_nonblocking(handle, 0);
byte[] raw_buf = new byte[report_len];
int ret = HIDapi.hid_read(handle, raw_buf, new UIntPtr(report_len));
if (ret > 0)
Expand All @@ -314,19 +314,19 @@ private int ReceiveRaw(bool block = false)
DebugPrint(string.Format("Duplicate timestamp enqueued. TS: {0:X2}", ts_en), DebugType.THREADING);
}
ts_en = raw_buf[1];
DebugPrint(string.Format("Enqueue. Blocking? {0:b}. Bytes read: {1:D}. Timestamp: {2:X2}", block, ret, raw_buf[1]), DebugType.THREADING);
DebugPrint(string.Format("Enqueue. Bytes read: {0:D}. Timestamp: {1:X2}", ret, raw_buf[1]), DebugType.THREADING);
}
return ret;
}
private Thread PollThreadObj;
private Thread RumbleThreadObj;
private void Poll()
{
bool recvd = false;
int attempts = 0;
while (!stop_polling)
{
int a = ReceiveRaw(recvd);
SendRumble(rumble_obj.GetData());
int a = ReceiveRaw();

if (a > 0)
{
Expand Down Expand Up @@ -487,8 +487,6 @@ public void Begin()
{
PollThreadObj = new Thread(new ThreadStart(Poll));
PollThreadObj.Start();
RumbleThreadObj = new Thread(new ThreadStart(RumbleListener));
RumbleThreadObj.Start();
}
public void Recenter()
{
Expand Down Expand Up @@ -520,12 +518,6 @@ public void SetRumble(float low_freq, float high_freq, float amp)
{
rumble_obj = new Rumble(low_freq, high_freq, amp);
}
private void RumbleListener(){
while (!stop_polling) {
SendRumble(rumble_obj.GetData());
Thread.Sleep(20);
}
}
private void SendRumble(byte[] buf)
{
byte[] buf_ = new byte[report_len];
Expand Down

0 comments on commit 5fe23d2

Please sign in to comment.