spnav-csharp-wrapper/SpaceNav.cs

177 lines
3.7 KiB
C#
Raw Normal View History

2018-05-30 17:15:37 +00:00
using System;
using System.Runtime.InteropServices;
namespace spnavwrapper
{
2018-05-30 17:36:43 +00:00
public sealed class SpaceNav : IDisposable
2018-05-30 17:15:37 +00:00
{
2018-05-30 17:46:52 +00:00
const string DLL_NAME = "spnavhdi";
2018-05-30 17:15:37 +00:00
2018-05-30 17:46:52 +00:00
#region Constants
private const int SPNAV_EVENT_MOTION = 1;
private const int SPNAV_EVENT_BUTTON = 2;
private const ushort SPNAV_VENDOR_ID = 0x046d;
private const ushort SPNAV_PRODUCT_ID = 0x0c627;
#endregion
2018-05-30 17:15:37 +00:00
2018-05-30 17:46:52 +00:00
private double _sensitivity = 1.0;
private int _threshold = 5;
private static SpaceNav instance;
2018-05-30 17:15:37 +00:00
2018-05-30 17:46:52 +00:00
#region Structures
private struct SpNavEventMotion
2018-05-30 17:15:37 +00:00
{
public int type;
public int x, y, z;
public int rx, ry, rz;
public uint period;
public IntPtr data;
}
2018-05-30 17:46:52 +00:00
private struct SpNavEventButton
2018-05-30 17:15:37 +00:00
{
public int type;
public int press;
public int bnum;
}
[StructLayout(LayoutKind.Explicit)]
2018-05-30 17:46:52 +00:00
private struct SpNavEvent
2018-05-30 17:15:37 +00:00
{
[FieldOffset(0)] public int type;
[FieldOffset(0)] public SpNavEventMotion motion;
[FieldOffset(0)] public SpNavEventButton button;
}
2018-05-30 17:46:52 +00:00
#endregion
2018-05-30 17:15:37 +00:00
2018-05-30 17:46:52 +00:00
#region DLL Imports
2018-05-30 17:15:37 +00:00
[DllImport(DLL_NAME)]
2018-05-30 17:46:52 +00:00
private static extern int spnav_open(ushort vendor_id, ushort product_id);
2018-05-30 17:15:37 +00:00
[DllImport(DLL_NAME)]
2018-05-30 17:46:52 +00:00
private static extern int spnav_close();
2018-05-30 17:15:37 +00:00
[DllImport(DLL_NAME)]
2018-05-30 17:46:52 +00:00
private static extern int spnav_wait_event(ref SpNavEvent ev);
2018-05-30 17:15:37 +00:00
[DllImport(DLL_NAME)]
2018-05-30 17:46:52 +00:00
private static extern int spnav_wait_event_timeout(ref SpNavEvent ev, int timeout);
2018-05-30 17:15:37 +00:00
[DllImport(DLL_NAME)]
2018-05-30 17:46:52 +00:00
private static extern int spnav_sensitivity(double sens);
2018-05-30 17:15:37 +00:00
[DllImport(DLL_NAME)]
2018-05-30 17:46:52 +00:00
private static extern int spnav_deadzone(int threshold);
#endregion
2018-05-30 17:15:37 +00:00
2018-05-30 17:46:52 +00:00
private SpaceNav()
2018-05-30 17:15:37 +00:00
{
// TODO : handle retcode
spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID);
}
2018-05-30 17:46:52 +00:00
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
private void Dispose(bool disposing)
{
if (!disposedValue)
{
// Free unmanaged resources
spnav_close();
disposedValue = true;
}
}
~SpaceNav()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region Methods
2018-05-30 17:15:37 +00:00
public static SpaceNav Instance
{
get
{
2018-05-30 17:36:43 +00:00
if (instance == null)
2018-05-30 17:15:37 +00:00
{
2018-05-30 17:36:43 +00:00
instance = new SpaceNav();
2018-05-30 17:15:37 +00:00
}
2018-05-30 17:36:43 +00:00
return instance;
2018-05-30 17:15:37 +00:00
}
}
public void CloseDevice()
{
2018-05-30 17:36:43 +00:00
spnav_close();
instance = null;
2018-05-30 17:15:37 +00:00
}
public SpaceNavEvent WaitEvent()
{
2018-05-30 17:36:43 +00:00
SpNavEvent sev = new SpNavEvent();
spnav_wait_event(ref sev);
switch (sev.type)
2018-05-30 17:15:37 +00:00
{
2018-05-30 17:36:43 +00:00
case SPNAV_EVENT_BUTTON:
return new SpaceNavButtonEvent(Convert.ToBoolean(sev.button.press), sev.button.bnum);
case SPNAV_EVENT_MOTION:
var ev = sev.motion;
return new SpaceNavMotionEvent(ev.x, ev.y, ev.z, ev.rx, ev.ry, ev.rz);
default:
return null;
2018-05-30 17:15:37 +00:00
}
}
public SpaceNavEvent WaitEvent(int milliseconds)
{
2018-05-30 17:36:43 +00:00
SpNavEvent sev = new SpNavEvent();
int ev_type = spnav_wait_event_timeout(ref sev, milliseconds);
switch (ev_type)
2018-05-30 17:15:37 +00:00
{
2018-05-30 17:36:43 +00:00
case SPNAV_EVENT_BUTTON:
return new SpaceNavButtonEvent(Convert.ToBoolean(sev.button.press), sev.button.bnum);
case SPNAV_EVENT_MOTION:
var ev = sev.motion;
return new SpaceNavMotionEvent(ev.x, ev.y, ev.z, ev.rx, ev.ry, ev.rz);
default:
return null;
2018-05-30 17:15:37 +00:00
}
}
public double Sensitivity
2018-05-30 17:36:43 +00:00
{
get
2018-05-30 17:15:37 +00:00
{
return _sensitivity;
}
set
{
2018-05-30 17:36:43 +00:00
// TODO : handle retcode
spnav_sensitivity(value);
_sensitivity = value;
2018-05-30 17:15:37 +00:00
}
}
public int Threshold
2018-05-30 17:36:43 +00:00
{
2018-05-30 17:15:37 +00:00
get
{
return _threshold;
}
2018-05-30 17:36:43 +00:00
set
2018-05-30 17:15:37 +00:00
{
2018-05-30 17:36:43 +00:00
// TODO : handle retcode
spnav_deadzone(value);
_threshold = value;
}
}
#endregion
2018-05-30 17:15:37 +00:00
}
}