Compare commits

..

5 Commits

7 changed files with 228 additions and 148 deletions

2
.gitmodules vendored
View File

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

View File

@ -14,17 +14,21 @@ 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 += delegate (object sender, ButtonEventArgs e)
{
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");
} }
} }
@ -32,10 +36,5 @@ namespace SpaceNavWrapper
{ {
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://git.digiserv.ovh/thibaud/spacenav-driver) Simple C# wrapper for the [HID spacenavigator driver](https://github.com/thib8956/spacenav-driver.git)

View File

@ -1,10 +1,9 @@
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";
@ -17,12 +16,11 @@ namespace SpaceNavWrapper
#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 bool deviceReady;
private double _sensitivity = 1.0; private double _sensitivity = 1.0;
private int _threshold = 5; private int _threshold = 5;
private bool _nonblocking;
private bool isDisposed; private bool isDisposed;
#region Structures #region Structures
@ -38,7 +36,8 @@ namespace SpaceNavWrapper
private struct SpNavEventButton private struct SpNavEventButton
{ {
public int type; public int type;
public int press; [MarshalAs(UnmanagedType.Bool)]
public bool press;
public int bnum; public int bnum;
} }
@ -57,6 +56,8 @@ namespace SpaceNavWrapper
[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_set_nonblocking(bool nonblock);
[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);
@ -66,50 +67,54 @@ namespace SpaceNavWrapper
private static extern int spnav_deadzone(int threshold); private static extern int spnav_deadzone(int threshold);
#endregion #endregion
public SpaceNavDriver()
{
eventThread = new Thread(HandleEvents)
{
IsBackground = true,
Name = "3Dconnexion-Event-Dispatcher"
};
eventThread.Start();
}
public void InitDevice() public void InitDevice()
{ {
// TODO handle retcode and errors
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
while (!deviceReady) {}
Console.WriteLine("Device ready !");
while (!isDisposed)
{ {
SpNavEvent sev = new SpNavEvent(); SpNavEvent sev = new SpNavEvent();
int ev_type = spnav_wait_event(ref sev); int ev_type;
Console.WriteLine("Event type : {0}", sev.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) switch (ev_type)
{ {
case SPNAV_EVENT_BUTTON: case SPNAV_EVENT_BUTTON:
//return new SpaceNavButtonEvent(Convert.ToBoolean(sev.button.press), sev.button.bnum); var e = sev.button;
Button(this, new EventArgs()); Button(this, new ButtonEventArgs(e.press, e.bnum));
break; break;
case SPNAV_EVENT_MOTION: case SPNAV_EVENT_MOTION:
var ev = sev.motion; var ev = sev.motion;
Motion(this, new MotionEventArgs(ev.x, ev.y, ev.z, ev.rx, ev.ry, ev.rz)); Motion(this, new MotionEventArgs(ev.x, ev.y, ev.z, ev.rx, ev.ry, ev.rz));
break; break;
} default:
break;
} }
} }
private void CloseDevice()
{
// TODO : handle retcode and errors
spnav_close();
}
#region Properties
public double Sensitivity public double Sensitivity
{ {
get => _sensitivity; get
{
return _sensitivity;
}
set set
{ {
_sensitivity = value; _sensitivity = value;
@ -119,7 +124,11 @@ namespace SpaceNavWrapper
public int Threshold public int Threshold
{ {
get => _threshold; get
{
return _threshold;
}
set set
{ {
_threshold = value; _threshold = value;
@ -127,6 +136,21 @@ namespace SpaceNavWrapper
} }
} }
public bool Nonblocking
{
get
{
return _nonblocking;
}
set
{
_nonblocking = value;
spnav_set_nonblocking(value);
}
}
#endregion
public void Dispose() public void Dispose()
{ {
if (!isDisposed) if (!isDisposed)
@ -137,11 +161,5 @@ namespace SpaceNavWrapper
} }
isDisposed = true; isDisposed = true;
} }
private void CloseDevice()
{
// TODO : handle retcode and errors
spnav_close();
}
} }
} }

View File

@ -1,37 +1,100 @@
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 int X, Y, Z; public readonly Dictionary<SpaceNavAxis, int> axisValues;
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)
{ {
X = x; axisValues = new Dictionary<SpaceNavAxis, int>();
Y = y; axisValues[SpaceNavAxis.X] = x;
Z = z; axisValues[SpaceNavAxis.Y] = y;
Rx = rx; axisValues[SpaceNavAxis.Z] = y;
Ry = ry; axisValues[SpaceNavAxis.Rx] = rx;
Rz = rz; axisValues[SpaceNavAxis.Ry] = ry;
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 "x=" + X + " y=" + Y + " z=" + Z + return string.Format("x={0} y={1} z={2} rx={3} ry={4} rz={5}", X, Y, Z, Rx, Ry, Rz);
" rx=" + Rz + " ry=" + Ry + " rz=" + Rz; }
public int GetAxis(SpaceNavAxis axis)
{
return axisValues[axis];
} }
} }
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 ButtonEventArgs(bool pressed, int button)
{
Pressed = pressed;
Button = button;
}
public override string ToString() public override string ToString()
{ {
return string.Format("[ButtonEventArgs: button={0}, pressed={1}]", button, pressed); return string.Format("button={0}, pressed={1}", Button, Pressed);
} }
} }
} }

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

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>