Skip to content
This repository was archived by the owner on Mar 9, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using UnityEngine;
using UnityEngine.SceneManagement;

using System;
public class JoyconManager: MonoBehaviour
{

// Settings accessible via Unity
public bool EnableIMU = true;
public bool EnableLocalize = true;

// Different operating systems either do or don't like the trailing zero
private const ushort vendor_id = 0x57e;
private const ushort vendor_id_ = 0x057e;
private const ushort product_l = 0x2006;
private const ushort product_r = 0x2007;

public List<Joycon> j; // Array of all connected Joy-Cons
static JoyconManager instance;

public static JoyconManager Instance
{
get { return instance; }
}

void Awake()
{
if (instance != null) Destroy(gameObject);
instance = this;
int i = 0;

j = new List<Joycon>();
bool isLeft = false;
HIDapi.hid_init();

IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0);
IntPtr top_ptr = ptr;

if (ptr == IntPtr.Zero)
{
ptr = HIDapi.hid_enumerate(vendor_id_, 0x0);
if (ptr == IntPtr.Zero)
{
HIDapi.hid_free_enumeration(ptr);
Debug.Log ("No Joy-Cons found!");
}
}
hid_device_info enumerate;
while (ptr != IntPtr.Zero) {
enumerate = (hid_device_info)Marshal.PtrToStructure (ptr, typeof(hid_device_info));

Debug.Log (enumerate.product_id);
if (enumerate.product_id == product_l || enumerate.product_id == product_r) {
if (enumerate.product_id == product_l) {
isLeft = true;
Debug.Log ("Left Joy-Con connected.");
} else if (enumerate.product_id == product_r) {
isLeft = false;
Debug.Log ("Right Joy-Con connected.");
} else {
Debug.Log ("Non Joy-Con input device skipped.");
}
IntPtr handle = HIDapi.hid_open_path (enumerate.path);
HIDapi.hid_set_nonblocking (handle, 1);
j.Add (new Joycon (handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft));
++i;
}
ptr = enumerate.next;
}
HIDapi.hid_free_enumeration (top_ptr);
}

void Start()
{
for (int i = 0; i < j.Count; ++i)
{
Debug.Log (i);
Joycon jc = j [i];
byte LEDs = 0x0;
LEDs |= (byte)(0x1 << i);
jc.Attach (leds_: LEDs);
jc.Begin ();
}
}

void Update()
{
for (int i = 0; i < j.Count; ++i)
{
j[i].Update();
}
}

void OnApplicationQuit()
{
for (int i = 0; i < j.Count; ++i)
{
j[i].Detach ();
}
}
}
151 changes: 87 additions & 64 deletions Packages/com.lookingglass.joyconlib/JoyconLib_scripts/JoyconManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
using System.Runtime.InteropServices;

using UnityEngine;
using UnityEngine.SceneManagement;
using System;
public class JoyconManager: MonoBehaviour
public class JoyconManager : MonoBehaviour
{

// Settings accessible via Unity
public bool EnableIMU = true;
public bool EnableLocalize = true;

// Different operating systems either do or don't like the trailing zero
private const ushort vendor_id = 0x57e;
private const ushort vendor_id_ = 0x057e;
private const ushort product_l = 0x2006;
private const ushort product_r = 0x2007;
// Different operating systems either do or don't like the trailing zero
private const ushort vendor_id = 0x57e;
private const ushort vendor_id_ = 0x057e;
private const ushort product_l = 0x2006;
private const ushort product_r = 0x2007;

public List<Joycon> j; // Array of all connected Joy-Cons
static JoyconManager instance;
Expand All @@ -29,75 +30,97 @@ void Awake()
{
if (instance != null) Destroy(gameObject);
instance = this;
int i = 0;

j = new List<Joycon>();
bool isLeft = false;
HIDapi.hid_init();

IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0);
IntPtr top_ptr = ptr;

if (ptr == IntPtr.Zero)
{
ptr = HIDapi.hid_enumerate(vendor_id_, 0x0);
if (ptr == IntPtr.Zero)
{
HIDapi.hid_free_enumeration(ptr);
Debug.Log ("No Joy-Cons found!");
}
}
hid_device_info enumerate;
while (ptr != IntPtr.Zero) {
enumerate = (hid_device_info)Marshal.PtrToStructure (ptr, typeof(hid_device_info));

Debug.Log (enumerate.product_id);
if (enumerate.product_id == product_l || enumerate.product_id == product_r) {
if (enumerate.product_id == product_l) {
isLeft = true;
Debug.Log ("Left Joy-Con connected.");
} else if (enumerate.product_id == product_r) {
isLeft = false;
Debug.Log ("Right Joy-Con connected.");
} else {
Debug.Log ("Non Joy-Con input device skipped.");
}
IntPtr handle = HIDapi.hid_open_path (enumerate.path);
HIDapi.hid_set_nonblocking (handle, 1);
j.Add (new Joycon (handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft));
++i;
}
ptr = enumerate.next;
}
HIDapi.hid_free_enumeration (top_ptr);
int i = 0;

j = new List<Joycon>();
bool isLeft = false;
HIDapi.hid_init();

IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0);
IntPtr top_ptr = ptr;

if (ptr == IntPtr.Zero)
{
ptr = HIDapi.hid_enumerate(vendor_id_, 0x0);
if (ptr == IntPtr.Zero)
{
HIDapi.hid_free_enumeration(ptr);
Debug.Log("No Joy-Cons found!");
}
}
hid_device_info enumerate;
while (ptr != IntPtr.Zero)
{
enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));

Debug.Log(enumerate.product_id);
if (enumerate.product_id == product_l || enumerate.product_id == product_r)
{
if (enumerate.product_id == product_l)
{
isLeft = true;
Debug.Log("Left Joy-Con connected.");
}
else if (enumerate.product_id == product_r)
{
isLeft = false;
Debug.Log("Right Joy-Con connected.");
}
else
{
Debug.Log("Non Joy-Con input device skipped.");
}
IntPtr handle = HIDapi.hid_open_path(enumerate.path);
HIDapi.hid_set_nonblocking(handle, 1);
j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft));
++i;
}
ptr = enumerate.next;
}
HIDapi.hid_free_enumeration(top_ptr);
}

void Start()
{
for (int i = 0; i < j.Count; ++i)
{
Debug.Log (i);
Joycon jc = j [i];
byte LEDs = 0x0;
LEDs |= (byte)(0x1 << i);
jc.Attach (leds_: LEDs);
jc.Begin ();
}
SceneManager.sceneUnloaded += OnSceneUnloaded;
for (int i = 0; i < j.Count; ++i)
{
Debug.Log(i);
Joycon jc = j[i];
byte LEDs = 0x0;
LEDs |= (byte)(0x1 << i);
jc.Attach(leds_: LEDs);
jc.Begin();
}
}

void Update()
{
for (int i = 0; i < j.Count; ++i)
{
j[i].Update();
}
for (int i = 0; i < j.Count; ++i)
{
j[i].Update();
}
}

private void OnDestroy()
{

}

private void OnSceneUnloaded(Scene current)
{
for (int i = 0; i < j.Count; ++i)
{
j[i].Detach();
}
}


void OnApplicationQuit()
{
for (int i = 0; i < j.Count; ++i)
{
j[i].Detach ();
}
for (int i = 0; i < j.Count; ++i)
{
j[i].Detach();
}
}
}