Add support for asynchronous event via Nonblocking

This commit is contained in:
Thibaud Gasser 2018-05-31 00:38:44 +02:00
parent 0c8028c0a8
commit 6b16cee62b
3 changed files with 45 additions and 31 deletions

View File

@ -16,7 +16,10 @@ namespace SpaceNavWrapper
//} //}
SpaceNav navDriver = new SpaceNav(); 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 {
@ -25,6 +28,7 @@ namespace SpaceNavWrapper
for (; ; ) for (; ; )
{ {
navDriver.WaitEvent(); 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(e);
}
} }
} }

View File

@ -20,6 +20,7 @@ namespace SpaceNavWrapper
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
@ -54,6 +55,8 @@ namespace SpaceNavWrapper
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)]
private static extern int spnav_set_nonblocking(bool nonblock);
[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)]
@ -98,25 +101,43 @@ namespace SpaceNavWrapper
} }
} }
public double Sensitivity private void CloseDevice()
{ {
get => _sensitivity; // TODO : handle retcode and errors
set spnav_close();
{ }
_sensitivity = value;
spnav_sensitivity(value);
}
}
public int Threshold #region Properties
{ public double Sensitivity
get => _threshold; {
set get => _sensitivity;
{ set
_threshold = value; {
spnav_deadzone(value); _sensitivity = value;
} spnav_sensitivity(value);
} }
}
public int Threshold
{
get => _threshold;
set
{
_threshold = value;
spnav_deadzone(value);
}
}
public bool Nonblocking
{
get => _nonblocking;
set
{
_nonblocking = value;
spnav_set_nonblocking(value);
}
}
#endregion
public void Dispose() public void Dispose()
{ {
@ -128,11 +149,5 @@ namespace SpaceNavWrapper
} }
isDisposed = true; isDisposed = true;
} }
private void CloseDevice()
{
// TODO : handle retcode and errors
spnav_close();
}
} }
} }

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