Mono-threading to be compatible with unity
This commit is contained in:
parent
d98210a403
commit
0c8028c0a8
12
Program.cs
12
Program.cs
@ -14,7 +14,7 @@ namespace SpaceNavWrapper
|
|||||||
// var ev = spnavwrapper.SpaceNav.Instance.WaitEvent(100);
|
// var ev = spnavwrapper.SpaceNav.Instance.WaitEvent(100);
|
||||||
// Console.WriteLine(ev);
|
// Console.WriteLine(ev);
|
||||||
//}
|
//}
|
||||||
SpaceNavDriver navDriver = new SpaceNavDriver();
|
SpaceNav navDriver = new SpaceNav();
|
||||||
navDriver.InitDevice();
|
navDriver.InitDevice();
|
||||||
navDriver.Button += OnButton;
|
navDriver.Button += OnButton;
|
||||||
navDriver.Motion += OnMotion;
|
navDriver.Motion += OnMotion;
|
||||||
@ -22,10 +22,10 @@ namespace SpaceNavWrapper
|
|||||||
Console.CancelKeyPress += delegate {
|
Console.CancelKeyPress += delegate {
|
||||||
navDriver.Dispose();
|
navDriver.Dispose();
|
||||||
};
|
};
|
||||||
|
for (; ; )
|
||||||
for (; ; )
|
{
|
||||||
{
|
navDriver.WaitEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OnMotion(object sender, MotionEventArgs e)
|
private static void OnMotion(object sender, MotionEventArgs e)
|
||||||
@ -35,7 +35,7 @@ namespace SpaceNavWrapper
|
|||||||
|
|
||||||
private static void OnButton(object sender, EventArgs e)
|
private static void OnButton(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Button pressed");
|
Console.WriteLine(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
193
SpaceNav.cs
193
SpaceNav.cs
@ -1,110 +1,101 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace SpaceNavWrapper
|
namespace SpaceNavWrapper
|
||||||
{
|
{
|
||||||
public class SpaceNavDriver : IDisposable
|
public class SpaceNav : IDisposable
|
||||||
{
|
{
|
||||||
|
|
||||||
const string DLL_NAME = "spnavhdi";
|
const string DLL_NAME = "spnavhdi";
|
||||||
|
|
||||||
#region Constants
|
#region Constants
|
||||||
private const int SPNAV_EVENT_MOTION = 1;
|
private const int SPNAV_EVENT_MOTION = 1;
|
||||||
private const int SPNAV_EVENT_BUTTON = 2;
|
private const int SPNAV_EVENT_BUTTON = 2;
|
||||||
private const ushort SPNAV_VENDOR_ID = 0x046d;
|
private const ushort SPNAV_VENDOR_ID = 0x046d;
|
||||||
private const ushort SPNAV_PRODUCT_ID = 0x0c627;
|
private const ushort SPNAV_PRODUCT_ID = 0x0c627;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public event EventHandler<MotionEventArgs> Motion;
|
public event EventHandler<MotionEventArgs> Motion;
|
||||||
public event EventHandler<EventArgs> Button;
|
public event EventHandler<ButtonEventArgs> Button;
|
||||||
|
|
||||||
private Thread eventThread;
|
private double _sensitivity = 1.0;
|
||||||
private bool deviceReady;
|
private int _threshold = 5;
|
||||||
private double _sensitivity = 1.0;
|
|
||||||
private int _threshold = 5;
|
|
||||||
private bool isDisposed;
|
private bool isDisposed;
|
||||||
|
|
||||||
#region Structures
|
#region Structures
|
||||||
private struct SpNavEventMotion
|
private struct SpNavEventMotion
|
||||||
{
|
{
|
||||||
public int type;
|
public int type;
|
||||||
public int x, y, z;
|
public int x, y, z;
|
||||||
public int rx, ry, rz;
|
public int rx, ry, rz;
|
||||||
public uint period;
|
public uint period;
|
||||||
public IntPtr data;
|
public IntPtr data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct SpNavEventButton
|
private struct SpNavEventButton
|
||||||
{
|
{
|
||||||
public int type;
|
public int type;
|
||||||
public int press;
|
[MarshalAs(UnmanagedType.Bool)]
|
||||||
public int bnum;
|
public bool press;
|
||||||
}
|
public int bnum;
|
||||||
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Explicit)]
|
[StructLayout(LayoutKind.Explicit)]
|
||||||
private struct SpNavEvent
|
private struct SpNavEvent
|
||||||
{
|
{
|
||||||
[FieldOffset(0)] public int type;
|
[FieldOffset(0)] public int type;
|
||||||
[FieldOffset(0)] public SpNavEventMotion motion;
|
[FieldOffset(0)] public SpNavEventMotion motion;
|
||||||
[FieldOffset(0)] public SpNavEventButton button;
|
[FieldOffset(0)] public SpNavEventButton button;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region DLL Imports
|
#region DLL Imports
|
||||||
[DllImport(DLL_NAME)]
|
[DllImport(DLL_NAME)]
|
||||||
private static extern int spnav_open(ushort vendor_id, ushort product_id);
|
private static extern int spnav_open(ushort vendor_id, ushort product_id);
|
||||||
[DllImport(DLL_NAME)]
|
[DllImport(DLL_NAME)]
|
||||||
private static extern int spnav_close();
|
private static extern int spnav_close();
|
||||||
[DllImport(DLL_NAME)]
|
[DllImport(DLL_NAME)]
|
||||||
private static extern int spnav_wait_event(ref SpNavEvent ev);
|
private static extern int spnav_wait_event(ref SpNavEvent ev);
|
||||||
[DllImport(DLL_NAME)]
|
[DllImport(DLL_NAME)]
|
||||||
private static extern int spnav_wait_event_timeout(ref SpNavEvent ev, int timeout);
|
private static extern int spnav_wait_event_timeout(ref SpNavEvent ev, int timeout);
|
||||||
[DllImport(DLL_NAME)]
|
[DllImport(DLL_NAME)]
|
||||||
private static extern int spnav_sensitivity(double sens);
|
private static extern int spnav_sensitivity(double sens);
|
||||||
[DllImport(DLL_NAME)]
|
[DllImport(DLL_NAME)]
|
||||||
private static extern int spnav_deadzone(int threshold);
|
private static extern int spnav_deadzone(int threshold);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public SpaceNavDriver()
|
public void InitDevice()
|
||||||
{
|
{
|
||||||
eventThread = new Thread(HandleEvents)
|
// TODO handle retcode and errors
|
||||||
{
|
|
||||||
IsBackground = true,
|
|
||||||
Name = "3Dconnexion-Event-Dispatcher"
|
|
||||||
};
|
|
||||||
eventThread.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void InitDevice()
|
|
||||||
{
|
|
||||||
spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID);
|
spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID);
|
||||||
spnav_deadzone(5);
|
}
|
||||||
deviceReady = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleEvents(object obj)
|
public void WaitEvent(int millis = -1)
|
||||||
{
|
{
|
||||||
// Block while the device isn't ready
|
SpNavEvent sev = new SpNavEvent();
|
||||||
while (!deviceReady) {}
|
int ev_type;
|
||||||
Console.WriteLine("Device ready !");
|
if (millis == -1)
|
||||||
while (!isDisposed)
|
{
|
||||||
{
|
ev_type = spnav_wait_event(ref sev);
|
||||||
SpNavEvent sev = new SpNavEvent();
|
}
|
||||||
int ev_type = spnav_wait_event(ref sev);
|
else
|
||||||
Console.WriteLine("Event type : {0}", sev.type);
|
{
|
||||||
switch (ev_type)
|
ev_type = spnav_wait_event_timeout(ref sev, (int)millis);
|
||||||
{
|
}
|
||||||
case SPNAV_EVENT_BUTTON:
|
|
||||||
//return new SpaceNavButtonEvent(Convert.ToBoolean(sev.button.press), sev.button.bnum);
|
switch (ev_type)
|
||||||
Button(this, new EventArgs());
|
{
|
||||||
break;
|
case SPNAV_EVENT_BUTTON:
|
||||||
case SPNAV_EVENT_MOTION:
|
var e = sev.button;
|
||||||
var ev = sev.motion;
|
Button(this, new ButtonEventArgs(e.press, e.bnum));
|
||||||
Motion(this, new MotionEventArgs(ev.x, ev.y, ev.z, ev.rx, ev.ry, ev.rz));
|
break;
|
||||||
break;
|
case SPNAV_EVENT_MOTION:
|
||||||
}
|
var ev = sev.motion;
|
||||||
}
|
Motion(this, new MotionEventArgs(ev.x, ev.y, ev.z, ev.rx, ev.ry, ev.rz));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double Sensitivity
|
public double Sensitivity
|
||||||
@ -126,17 +117,17 @@ namespace SpaceNavWrapper
|
|||||||
spnav_deadzone(value);
|
spnav_deadzone(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (!isDisposed)
|
if (!isDisposed)
|
||||||
{
|
{
|
||||||
CloseDevice();
|
CloseDevice();
|
||||||
Button = null;
|
Button = null;
|
||||||
Motion = null;
|
Motion = null;
|
||||||
}
|
}
|
||||||
isDisposed = true;
|
isDisposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CloseDevice()
|
private void CloseDevice()
|
||||||
{
|
{
|
||||||
|
@ -25,13 +25,18 @@ namespace SpaceNavWrapper
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class ButtonEventArgs : EventArgs
|
public class ButtonEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public readonly int button;
|
public readonly bool Pressed;
|
||||||
public readonly bool pressed;
|
public readonly int Button;
|
||||||
|
|
||||||
public override string ToString()
|
public ButtonEventArgs(bool pressed, int button)
|
||||||
{
|
{
|
||||||
return string.Format("[ButtonEventArgs: button={0}, pressed={1}]", button, pressed);
|
Pressed = pressed;
|
||||||
|
Button = button;
|
||||||
}
|
}
|
||||||
}
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return string.Format("[ButtonEventArgs: button={0}, pressed={1}]", Button, Pressed);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,9 +27,9 @@
|
|||||||
<PlatformTarget>x86</PlatformTarget>
|
<PlatformTarget>x86</PlatformTarget>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="SpaceNav.cs" />
|
|
||||||
<Compile Include="SpaceNavEvent.cs" />
|
<Compile Include="SpaceNavEvent.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="SpaceNav.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user