2 Commits

Author SHA1 Message Date
6b16cee62b Add support for asynchronous event via Nonblocking 2018-05-31 00:38:44 +02:00
0c8028c0a8 Mono-threading to be compatible with unity 2018-05-30 23:53:56 +02:00
5 changed files with 143 additions and 133 deletions

View File

@ -14,17 +14,21 @@ namespace SpaceNavWrapper
// var ev = spnavwrapper.SpaceNav.Instance.WaitEvent(100);
// Console.WriteLine(ev);
//}
SpaceNavDriver navDriver = new SpaceNavDriver();
SpaceNav navDriver = new SpaceNav();
navDriver.InitDevice();
navDriver.Button += OnButton;
navDriver.Button += delegate (object sender, ButtonEventArgs e)
{
navDriver.Nonblocking = !navDriver.Nonblocking;
};
navDriver.Motion += OnMotion;
Console.CancelKeyPress += delegate {
navDriver.Dispose();
};
for (; ; )
{
navDriver.WaitEvent();
Console.WriteLine("AA");
}
}
@ -32,10 +36,5 @@ namespace SpaceNavWrapper
{
Console.WriteLine(e);
}
private static void OnButton(object sender, EventArgs e)
{
Console.WriteLine("Button pressed");
}
}
}

View File

@ -1,10 +1,9 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace SpaceNavWrapper
{
public class SpaceNavDriver : IDisposable
public class SpaceNav : IDisposable
{
const string DLL_NAME = "spnavhdi";
@ -17,12 +16,11 @@ namespace SpaceNavWrapper
#endregion
public event EventHandler<MotionEventArgs> Motion;
public event EventHandler<EventArgs> Button;
public event EventHandler<ButtonEventArgs> Button;
private Thread eventThread;
private bool deviceReady;
private double _sensitivity = 1.0;
private int _threshold = 5;
private bool _nonblocking;
private bool isDisposed;
#region Structures
@ -38,7 +36,8 @@ namespace SpaceNavWrapper
private struct SpNavEventButton
{
public int type;
public int press;
[MarshalAs(UnmanagedType.Bool)]
public bool press;
public int bnum;
}
@ -57,6 +56,8 @@ namespace SpaceNavWrapper
[DllImport(DLL_NAME)]
private static extern int spnav_close();
[DllImport(DLL_NAME)]
private static extern int spnav_set_nonblocking(bool nonblock);
[DllImport(DLL_NAME)]
private static extern int spnav_wait_event(ref SpNavEvent ev);
[DllImport(DLL_NAME)]
private static extern int spnav_wait_event_timeout(ref SpNavEvent ev, int timeout);
@ -66,47 +67,47 @@ namespace SpaceNavWrapper
private static extern int spnav_deadzone(int threshold);
#endregion
public SpaceNavDriver()
{
eventThread = new Thread(HandleEvents)
{
IsBackground = true,
Name = "3Dconnexion-Event-Dispatcher"
};
eventThread.Start();
}
public void InitDevice()
{
// TODO handle retcode and errors
spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID);
spnav_deadzone(5);
deviceReady = true;
}
private void HandleEvents(object obj)
{
// Block while the device isn't ready
while (!deviceReady) {}
Console.WriteLine("Device ready !");
while (!isDisposed)
public void WaitEvent(int millis = -1)
{
SpNavEvent sev = new SpNavEvent();
int ev_type = spnav_wait_event(ref sev);
Console.WriteLine("Event type : {0}", sev.type);
int ev_type;
if (millis == -1)
{
ev_type = spnav_wait_event(ref sev);
}
else
{
ev_type = spnav_wait_event_timeout(ref sev, (int)millis);
}
switch (ev_type)
{
case SPNAV_EVENT_BUTTON:
//return new SpaceNavButtonEvent(Convert.ToBoolean(sev.button.press), sev.button.bnum);
Button(this, new EventArgs());
var e = sev.button;
Button(this, new ButtonEventArgs(e.press, e.bnum));
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;
}
}
private void CloseDevice()
{
// TODO : handle retcode and errors
spnav_close();
}
#region Properties
public double Sensitivity
{
get => _sensitivity;
@ -127,6 +128,17 @@ namespace SpaceNavWrapper
}
}
public bool Nonblocking
{
get => _nonblocking;
set
{
_nonblocking = value;
spnav_set_nonblocking(value);
}
}
#endregion
public void Dispose()
{
if (!isDisposed)
@ -137,11 +149,5 @@ namespace SpaceNavWrapper
}
isDisposed = true;
}
private void CloseDevice()
{
// TODO : handle retcode and errors
spnav_close();
}
}
}

View File

@ -26,12 +26,17 @@ namespace SpaceNavWrapper
public class ButtonEventArgs : EventArgs
{
public readonly int button;
public readonly bool pressed;
public readonly bool Pressed;
public readonly int Button;
public ButtonEventArgs(bool pressed, int button)
{
Pressed = pressed;
Button = button;
}
public override string ToString()
{
return string.Format("[ButtonEventArgs: button={0}, pressed={1}]", button, pressed);
return string.Format("[ButtonEventArgs: button={0}, pressed={1}]", Button, Pressed);
}
}
}

View File

@ -27,9 +27,9 @@
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Compile Include="SpaceNav.cs" />
<Compile Include="SpaceNavEvent.cs" />
<Compile Include="Program.cs" />
<Compile Include="SpaceNav.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>