Compare commits

..

No commits in common. "master" and "event-threads" have entirely different histories.

7 changed files with 148 additions and 228 deletions

2
.gitmodules vendored
View File

@ -1,3 +1,3 @@
[submodule "spacenav-driver"] [submodule "spacenav-driver"]
path = spacenav-driver path = spacenav-driver
url = https://github.com/thib8956/spacenav-driver.git url = https://git.digiserv.ovh/thibaud/spacenav-driver.git

View File

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

View File

@ -1,3 +1,3 @@
# spnav-csharp-wrapper # spnav-csharp-wrapper
Simple C# wrapper for the [HID spacenavigator driver](https://github.com/thib8956/spacenav-driver.git) Simple C# wrapper for the HID spacenavigator [driver](https://git.digiserv.ovh/thibaud/spacenav-driver)

View File

@ -1,165 +1,147 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
namespace SpaceNavWrapper namespace SpaceNavWrapper
{ {
public class SpaceNav : IDisposable public class SpaceNavDriver : 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<ButtonEventArgs> Button; public event EventHandler<EventArgs> Button;
private double _sensitivity = 1.0; private Thread eventThread;
private int _threshold = 5; private bool deviceReady;
private bool _nonblocking; 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 x, y, z;
public int rx, ry, rz;
public uint period;
public IntPtr data;
}
private struct SpNavEventButton
{
public int type;
[MarshalAs(UnmanagedType.Bool)]
public bool press;
public int bnum;
}
[StructLayout(LayoutKind.Explicit)]
private struct SpNavEvent
{
[FieldOffset(0)] public int type;
[FieldOffset(0)] public SpNavEventMotion motion;
[FieldOffset(0)] public SpNavEventButton button;
}
#endregion
#region DLL Imports
[DllImport(DLL_NAME)]
private static extern int spnav_open(ushort vendor_id, ushort product_id);
[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);
[DllImport(DLL_NAME)]
private static extern int spnav_sensitivity(double sens);
[DllImport(DLL_NAME)]
private static extern int spnav_deadzone(int threshold);
#endregion
public void InitDevice()
{
// TODO handle retcode and errors
spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID);
}
public void WaitEvent(int millis = -1)
{
SpNavEvent sev = new SpNavEvent();
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:
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 public int type;
spnav_close(); public int x, y, z;
public int rx, ry, rz;
public uint period;
public IntPtr data;
} }
#region Properties private struct SpNavEventButton
public double Sensitivity
{ {
get public int type;
{ public int press;
return _sensitivity; public int bnum;
}
set
{
_sensitivity = value;
spnav_sensitivity(value);
}
} }
public int Threshold [StructLayout(LayoutKind.Explicit)]
private struct SpNavEvent
{ {
get [FieldOffset(0)] public int type;
{ [FieldOffset(0)] public SpNavEventMotion motion;
return _threshold; [FieldOffset(0)] public SpNavEventButton button;
}
set
{
_threshold = value;
spnav_deadzone(value);
}
}
public bool Nonblocking
{
get
{
return _nonblocking;
}
set
{
_nonblocking = value;
spnav_set_nonblocking(value);
}
} }
#endregion #endregion
public void Dispose() #region DLL Imports
{ [DllImport(DLL_NAME)]
if (!isDisposed) private static extern int spnav_open(ushort vendor_id, ushort product_id);
[DllImport(DLL_NAME)]
private static extern int spnav_close();
[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);
[DllImport(DLL_NAME)]
private static extern int spnav_sensitivity(double sens);
[DllImport(DLL_NAME)]
private static extern int spnav_deadzone(int threshold);
#endregion
public SpaceNavDriver()
{
eventThread = new Thread(HandleEvents)
{ {
CloseDevice(); IsBackground = true,
Button = null; Name = "3Dconnexion-Event-Dispatcher"
Motion = null; };
eventThread.Start();
}
public void InitDevice()
{
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)
{
SpNavEvent sev = new SpNavEvent();
int ev_type = spnav_wait_event(ref sev);
Console.WriteLine("Event type : {0}", sev.type);
switch (ev_type)
{
case SPNAV_EVENT_BUTTON:
//return new SpaceNavButtonEvent(Convert.ToBoolean(sev.button.press), sev.button.bnum);
Button(this, new EventArgs());
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;
}
}
}
public double Sensitivity
{
get => _sensitivity;
set
{
_sensitivity = value;
spnav_sensitivity(value);
} }
isDisposed = true; }
public int Threshold
{
get => _threshold;
set
{
_threshold = value;
spnav_deadzone(value);
}
}
public void Dispose()
{
if (!isDisposed)
{
CloseDevice();
Button = null;
Motion = null;
}
isDisposed = true;
}
private void CloseDevice()
{
// TODO : handle retcode and errors
spnav_close();
} }
} }
} }

View File

@ -1,100 +1,37 @@
using System; using System;
using System.Collections.Generic;
namespace SpaceNavWrapper namespace SpaceNavWrapper
{ {
public enum SpaceNavAxis {
X, Y, Z, Rx, Ry, Rz
}
public class MotionEventArgs : EventArgs public class MotionEventArgs : EventArgs
{ {
public readonly Dictionary<SpaceNavAxis, int> axisValues; public readonly int X, Y, Z;
public readonly int Rx, Ry, Rz;
public MotionEventArgs(int x, int y, int z, int rx, int ry, int rz) public MotionEventArgs(int x, int y, int z, int rx, int ry, int rz)
{ {
axisValues = new Dictionary<SpaceNavAxis, int>(); X = x;
axisValues[SpaceNavAxis.X] = x; Y = y;
axisValues[SpaceNavAxis.Y] = y; Z = z;
axisValues[SpaceNavAxis.Z] = y; Rx = rx;
axisValues[SpaceNavAxis.Rx] = rx; Ry = ry;
axisValues[SpaceNavAxis.Ry] = ry; Rz = rz;
axisValues[SpaceNavAxis.Rz] = rz;
} }
public int X
{
get
{
return axisValues[SpaceNavAxis.X];
}
}
public int Y
{
get
{
return axisValues[SpaceNavAxis.Y];
}
}
public int Z
{
get
{
return axisValues[SpaceNavAxis.Z];
}
}
public int Rx
{
get
{
return axisValues[SpaceNavAxis.Rx];
}
}
public int Ry
{
get
{
return axisValues[SpaceNavAxis.Ry];
}
}
public int Rz
{
get
{
return axisValues[SpaceNavAxis.Rz];
}
}
public override string ToString() public override string ToString()
{
return string.Format("x={0} y={1} z={2} rx={3} ry={4} rz={5}", X, Y, Z, Rx, Ry, Rz);
}
public int GetAxis(SpaceNavAxis axis)
{ {
return axisValues[axis]; return "x=" + X + " y=" + Y + " z=" + Z +
" rx=" + Rz + " ry=" + Ry + " rz=" + Rz;
} }
} }
public class ButtonEventArgs : EventArgs public class ButtonEventArgs : EventArgs
{ {
public readonly bool Pressed; public readonly int button;
public readonly int Button; public readonly bool pressed;
public ButtonEventArgs(bool pressed, int button) public override string ToString()
{ {
Pressed = pressed; return string.Format("[ButtonEventArgs: button={0}, pressed={1}]", button, pressed);
Button = button;
} }
}
public override string ToString()
{
return string.Format("button={0}, pressed={1}", Button, Pressed);
}
}
} }

@ -1 +1 @@
Subproject commit f654a62bdb4dbe14991fa89c2af69034587f816a Subproject commit f37537fa836f19905f43dc5fd49e58384790d38a

View File

@ -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>