Rewrite event system using delegates and threads

This commit is contained in:
Thibaud Gasser 2018-05-30 23:12:00 +02:00
parent b48f5b33ad
commit d98210a403
3 changed files with 171 additions and 191 deletions

View File

@ -1,20 +1,41 @@
using System; using System;
using spnavwrapper;
namespace spnavcsharpwrapper namespace SpaceNavWrapper
{ {
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
SpaceNav.Instance.Threshold = 5; //spnavwrapper.SpaceNav.Instance.Threshold = 5;
SpaceNav.Instance.Sensitivity = 0.1; //spnavwrapper.SpaceNav.Instance.Sensitivity = 0.1;
//for (; ; )
//{
// var ev = spnavwrapper.SpaceNav.Instance.WaitEvent(100);
// Console.WriteLine(ev);
//}
SpaceNavDriver navDriver = new SpaceNavDriver();
navDriver.InitDevice();
navDriver.Button += OnButton;
navDriver.Motion += OnMotion;
Console.CancelKeyPress += delegate {
navDriver.Dispose();
};
for (; ; ) for (; ; )
{ {
var ev = SpaceNav.Instance.WaitEvent(100);
Console.WriteLine(ev);
} }
} }
private static void OnMotion(object sender, MotionEventArgs e)
{
Console.WriteLine(e);
}
private static void OnButton(object sender, EventArgs e)
{
Console.WriteLine("Button pressed");
}
} }
} }

View File

@ -1,176 +1,147 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
namespace spnavwrapper namespace SpaceNavWrapper
{ {
public sealed class SpaceNav : IDisposable public class SpaceNavDriver : IDisposable
{ {
const string DLL_NAME = "spnavhdi"; const string DLL_NAME = "spnavhdi";
#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
#region Constants public event EventHandler<MotionEventArgs> Motion;
private const int SPNAV_EVENT_MOTION = 1; public event EventHandler<EventArgs> Button;
private const int SPNAV_EVENT_BUTTON = 2;
private const ushort SPNAV_VENDOR_ID = 0x046d; private Thread eventThread;
private const ushort SPNAV_PRODUCT_ID = 0x0c627; private bool deviceReady;
#endregion private double _sensitivity = 1.0;
private int _threshold = 5;
private double _sensitivity = 1.0; private bool isDisposed;
private int _threshold = 5;
private static SpaceNav instance;
#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; public int press;
public int bnum; 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
private SpaceNav() public SpaceNavDriver()
{ {
// TODO : handle retcode eventThread = new Thread(HandleEvents)
{
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;
}
#region IDisposable Support private void HandleEvents(object obj)
private bool disposedValue = false; // To detect redundant calls {
// Block while the device isn't ready
private void Dispose(bool disposing) while (!deviceReady) {}
{ Console.WriteLine("Device ready !");
if (!disposedValue) while (!isDisposed)
{ {
// Free unmanaged resources SpNavEvent sev = new SpNavEvent();
spnav_close(); int ev_type = spnav_wait_event(ref sev);
disposedValue = true; Console.WriteLine("Event type : {0}", sev.type);
} switch (ev_type)
} {
case SPNAV_EVENT_BUTTON:
~SpaceNav() //return new SpaceNavButtonEvent(Convert.ToBoolean(sev.button.press), sev.button.bnum);
{ Button(this, new EventArgs());
// Do not change this code. Put cleanup code in Dispose(bool disposing) above. break;
Dispose(false); case SPNAV_EVENT_MOTION:
} var ev = sev.motion;
Motion(this, new MotionEventArgs(ev.x, ev.y, ev.z, ev.rx, ev.ry, ev.rz));
// This code added to correctly implement the disposable pattern. break;
public void Dispose() }
{ }
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region Methods
public static SpaceNav Instance
{
get
{
if (instance == null)
{
instance = new SpaceNav();
}
return instance;
}
}
public void CloseDevice()
{
spnav_close();
instance = null;
}
public SpaceNavEvent WaitEvent()
{
SpNavEvent sev = new SpNavEvent();
spnav_wait_event(ref sev);
switch (sev.type)
{
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;
}
}
public SpaceNavEvent WaitEvent(int milliseconds)
{
SpNavEvent sev = new SpNavEvent();
int ev_type = spnav_wait_event_timeout(ref sev, milliseconds);
switch (ev_type)
{
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;
}
} }
public double Sensitivity public double Sensitivity
{ {
get get => _sensitivity;
{
return _sensitivity;
}
set set
{ {
// TODO : handle retcode
spnav_sensitivity(value);
_sensitivity = value; _sensitivity = value;
spnav_sensitivity(value);
} }
} }
public int Threshold public int Threshold
{ {
get get => _threshold;
{
return _threshold;
}
set set
{ {
// TODO : handle retcode
spnav_deadzone(value);
_threshold = value; _threshold = value;
spnav_deadzone(value);
} }
} }
#endregion
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,49 +1,37 @@
namespace spnavwrapper using System;
namespace SpaceNavWrapper
{ {
public abstract class SpaceNavEvent public class MotionEventArgs : EventArgs
{ {
} public readonly int X, Y, Z;
public readonly int Rx, Ry, Rz;
public class SpaceNavButtonEvent : SpaceNavEvent
{ public MotionEventArgs(int x, int y, int z, int rx, int ry, int rz)
public bool Pressed { get; } {
public int Button { get; } X = x;
Y = y;
public SpaceNavButtonEvent(bool pressed, int button) Z = z;
{ Rx = rx;
Pressed = pressed; Ry = ry;
Button = button; Rz = rz;
} }
public override string ToString()
{
return "pressed=" + Pressed + " button=" + Button;
}
}
public class SpaceNavMotionEvent : SpaceNavEvent
{
public int X { get; }
public int Y { get; }
public int Z { get; }
public int Rx { get; }
public int Ry { get; }
public int Rz { get; }
public SpaceNavMotionEvent(int x, int y, int z, int rx, int ry, int rz)
{
X = x;
Y = y;
Z = z;
Rx = rx;
Ry = ry;
Rz = rz;
}
public override string ToString() public override string ToString()
{ {
return "x=" + X + " y=" + Y + " z=" + Z + return "x=" + X + " y=" + Y + " z=" + Z +
" rx=" + Rz + " ry=" + Ry + " rz=" + Rz; " rx=" + Rz + " ry=" + Ry + " rz=" + Rz;
} }
} }
public class ButtonEventArgs : EventArgs
{
public readonly int button;
public readonly bool pressed;
public override string ToString()
{
return string.Format("[ButtonEventArgs: button={0}, pressed={1}]", button, pressed);
}
}
} }