Use only one thread for the wiimote
This commit is contained in:
1 parent
cbded1c7fb
commit
d6d23809ec
2 files changed
+65
-74
No files matched your search
@@ -31,9 +31,7 @@ namespace Vocaluxe.Lib.Input.WiiMote
|
|||||||
private bool[] _ButtonStates;
|
private bool[] _ButtonStates;
|
||||||
private Point _OldPosition;
|
private Point _OldPosition;
|
||||||
private bool _Connected;
|
private bool _Connected;
|
||||||
|
|
||||||
private Thread _HandlerThread;
|
|
||||||
private AutoResetEvent _EvTerminate;
|
|
||||||
private Object _Sync;
|
private Object _Sync;
|
||||||
private bool _Active;
|
private bool _Active;
|
||||||
private CRumbleTimer _RumbleTimer;
|
private CRumbleTimer _RumbleTimer;
|
||||||
@@ -53,36 +51,34 @@ namespace Vocaluxe.Lib.Input.WiiMote
|
|||||||
_RumbleTimer = new CRumbleTimer();
|
_RumbleTimer = new CRumbleTimer();
|
||||||
_Gesture = new CGesture();
|
_Gesture = new CGesture();
|
||||||
|
|
||||||
_HandlerThread = new Thread(_MainLoop) {Name = "WiiMote", Priority = ThreadPriority.BelowNormal};
|
|
||||||
_EvTerminate = new AutoResetEvent(false);
|
|
||||||
|
|
||||||
_ButtonStates = new bool[11];
|
_ButtonStates = new bool[11];
|
||||||
_OldPosition = new Point();
|
_OldPosition = new Point();
|
||||||
|
|
||||||
|
_WiiMote = new CWiiMoteLib();
|
||||||
|
_WiiMote.WiiMoteChanged += _WmWiiMoteChanged;
|
||||||
|
_WiiMote.WiiMoteConnectionChanged += _WiiMote_WiiMoteConnectionChanged;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Close()
|
public override void Close()
|
||||||
{
|
{
|
||||||
_Active = false;
|
_Active = false;
|
||||||
if (_HandlerThread != null)
|
_WiiMote.SetRumble(false);
|
||||||
{
|
_WiiMote.Disconnect();
|
||||||
//Join before freeing stuff
|
_Connected = false;
|
||||||
//This also ensures, that no other thread is created till the current one is terminated
|
|
||||||
_EvTerminate.Set();
|
|
||||||
_HandlerThread.Join();
|
|
||||||
_HandlerThread = null;
|
|
||||||
_EvTerminate.Dispose();
|
|
||||||
_EvTerminate = null;
|
|
||||||
}
|
|
||||||
base.Close();
|
base.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Connect()
|
public override void Connect()
|
||||||
{
|
{
|
||||||
if (_Active || _HandlerThread == null)
|
if (_Active)
|
||||||
return;
|
return;
|
||||||
_Active = true;
|
_Active = true;
|
||||||
_HandlerThread.Start();
|
|
||||||
|
|
||||||
|
_WiiMote.Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Disconnect()
|
public override void Disconnect()
|
||||||
@@ -104,67 +100,41 @@ namespace Vocaluxe.Lib.Input.WiiMote
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void _MainLoop()
|
private void _WiiMote_WiiMoteConnectionChanged(object sender, CWiiMoteConnectionChangedEventArgs e)
|
||||||
{
|
{
|
||||||
_WiiMote = new CWiiMoteLib();
|
if (!e.Connected)
|
||||||
_WiiMote.WiiMoteChanged += _WmWiiMoteChanged;
|
|
||||||
|
|
||||||
while (_Active)
|
|
||||||
{
|
{
|
||||||
Thread.Sleep(5);
|
_Connected = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_WiiMote.SetReportType(EInputReport.IRAccel, EIRSensitivity.Max, false);
|
||||||
|
_WiiMote.SetLEDs(false, false, true, false);
|
||||||
|
|
||||||
if (!_WiiMote.Connected)
|
_WiiMote.SetRumble(true);
|
||||||
|
Thread.Sleep(250);
|
||||||
|
_WiiMote.SetRumble(false);
|
||||||
|
Thread.Sleep(250);
|
||||||
|
_WiiMote.SetRumble(true);
|
||||||
|
Thread.Sleep(250);
|
||||||
|
_WiiMote.SetRumble(false);
|
||||||
|
|
||||||
|
|
||||||
|
bool startRumble;
|
||||||
|
bool stopRumble;
|
||||||
|
lock (_Sync)
|
||||||
{
|
{
|
||||||
if (!_DoConnect())
|
startRumble = _RumbleTimer.ShouldStart;
|
||||||
_EvTerminate.WaitOne(1000);
|
stopRumble = _RumbleTimer.ShouldStop;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
bool startRumble;
|
|
||||||
bool stopRumble;
|
|
||||||
lock (_Sync)
|
|
||||||
{
|
|
||||||
startRumble = _RumbleTimer.ShouldStart;
|
|
||||||
stopRumble = _RumbleTimer.ShouldStop;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (startRumble)
|
if (startRumble)
|
||||||
_WiiMote.SetRumble(true);
|
_WiiMote.SetRumble(true);
|
||||||
else if (stopRumble)
|
else if (stopRumble)
|
||||||
_WiiMote.SetRumble(false);
|
_WiiMote.SetRumble(false);
|
||||||
}
|
|
||||||
|
_Connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_WiiMote.SetRumble(false);
|
|
||||||
_WiiMote.Disconnect();
|
|
||||||
_Connected = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool _DoConnect()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!_WiiMote.Connect())
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_WiiMote.SetReportType(EInputReport.IRAccel, EIRSensitivity.Max, false);
|
|
||||||
_WiiMote.SetLEDs(false, false, true, false);
|
|
||||||
|
|
||||||
_WiiMote.SetRumble(true);
|
|
||||||
Thread.Sleep(250);
|
|
||||||
_WiiMote.SetRumble(false);
|
|
||||||
Thread.Sleep(250);
|
|
||||||
_WiiMote.SetRumble(true);
|
|
||||||
Thread.Sleep(250);
|
|
||||||
_WiiMote.SetRumble(false);
|
|
||||||
|
|
||||||
_Connected = true;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void _WmWiiMoteChanged(object sender, CWiiMoteChangedEventArgs args)
|
private void _WmWiiMoteChanged(object sender, CWiiMoteChangedEventArgs args)
|
||||||
|
|||||||
@@ -135,6 +135,15 @@ namespace Vocaluxe.Lib.Input.WiiMote
|
|||||||
WiiMoteState = ws;
|
WiiMoteState = ws;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public class CWiiMoteConnectionChangedEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public readonly bool Connected;
|
||||||
|
|
||||||
|
public CWiiMoteConnectionChangedEventArgs(bool connected)
|
||||||
|
{
|
||||||
|
Connected = connected;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion Events
|
#endregion Events
|
||||||
|
|
||||||
public sealed class CWiiMoteLib
|
public sealed class CWiiMoteLib
|
||||||
@@ -181,11 +190,22 @@ namespace Vocaluxe.Lib.Input.WiiMote
|
|||||||
private Thread _Reader;
|
private Thread _Reader;
|
||||||
private readonly bool _Error;
|
private readonly bool _Error;
|
||||||
|
|
||||||
public bool Connected { get; private set; }
|
private bool _Connected;
|
||||||
|
public bool Connected
|
||||||
|
{
|
||||||
|
get { return _Connected; }
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
_Connected = value;
|
||||||
|
|
||||||
|
WiiMoteConnectionChanged?.Invoke(this, new CWiiMoteConnectionChangedEventArgs(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private readonly CWiiMoteStatus _WiiMoteState = new CWiiMoteStatus();
|
private readonly CWiiMoteStatus _WiiMoteState = new CWiiMoteStatus();
|
||||||
private readonly AutoResetEvent _ReadDone = new AutoResetEvent(false);
|
private readonly AutoResetEvent _ReadDone = new AutoResetEvent(false);
|
||||||
public event EventHandler<CWiiMoteChangedEventArgs> WiiMoteChanged;
|
public event EventHandler<CWiiMoteChangedEventArgs> WiiMoteChanged;
|
||||||
|
public event EventHandler<CWiiMoteConnectionChangedEventArgs> WiiMoteConnectionChanged;
|
||||||
|
|
||||||
#region Interface
|
#region Interface
|
||||||
public CWiiMoteLib()
|
public CWiiMoteLib()
|
||||||
@@ -326,6 +346,7 @@ namespace Vocaluxe.Lib.Input.WiiMote
|
|||||||
if (bytesRead == -1)
|
if (bytesRead == -1)
|
||||||
{
|
{
|
||||||
Connected = false; //Disconnected
|
Connected = false; //Disconnected
|
||||||
|
Connect();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -339,7 +360,7 @@ namespace Vocaluxe.Lib.Input.WiiMote
|
|||||||
if (bytesRead > 0 && _ParseInputReport(buff))
|
if (bytesRead > 0 && _ParseInputReport(buff))
|
||||||
{
|
{
|
||||||
if (WiiMoteChanged != null)
|
if (WiiMoteChanged != null)
|
||||||
WiiMoteChanged(this, new CWiiMoteChangedEventArgs(_WiiMoteState));
|
WiiMoteChanged.Invoke(this, new CWiiMoteChangedEventArgs(_WiiMoteState));
|
||||||
}
|
}
|
||||||
Thread.Sleep(5);
|
Thread.Sleep(5);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user