Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unplug devices from ScpBus when they are removed #2

Merged
merged 3 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions mi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ private static void ManageControllers(ScpBus scpBus)
{
var compatibleDevices = HidDevices.Enumerate(0x2717, 0x3144).ToList();
var existingDevices = Gamepads.Select(g => g.Device).ToList();
var newDevices = compatibleDevices.Where(d => !existingDevices.Contains(d));
var newDevices = compatibleDevices.Where(d => !existingDevices.Select(e => e.DevicePath).Contains(d.DevicePath));
foreach (var gamepad in Gamepads.ToList())
{
if (!gamepad.check_connected())
{
gamepad.unplug();
Gamepads.Remove(gamepad);
}
}
foreach (var deviceInstance in newDevices)
{
var device = deviceInstance;
Expand Down Expand Up @@ -157,19 +165,19 @@ private static void ManageControllers(ScpBus scpBus)
device.ReadProduct(out product);


Gamepads.Add(new Xiaomi_gamepad(device, scpBus, Gamepads.Count + 1));
var usedIndexes = Gamepads.Select(g => g.Index);
var index = 1;
while (usedIndexes.Contains(index))
{
index++;
}
Gamepads.Add(new Xiaomi_gamepad(device, scpBus, index));
}
if (Gamepads.Count != nrConnected)
{
InformUser($"{Gamepads.Count} controllers connected");
}
nrConnected = Gamepads.Count;
if (nrConnected == 4)
{
Thread.Sleep(10000);
continue;
}
Thread.Sleep(5000);
Thread.Sleep(1000);
}
}

Expand Down
28 changes: 24 additions & 4 deletions mi/Xiaomi_gamepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,48 @@ namespace mi
public class Xiaomi_gamepad
{
public HidDevice Device { get; set; }
public int Index;
private Thread rThread, iThread;
private ScpBus ScpBus;
private byte[] Vibration = { 0x20, 0x00, 0x00 };
private Mutex rumble_mutex = new Mutex();
private bool Running = true;
//private byte[] enableAccelerometer = { 0x31, 0x01, 0x08 };

public Xiaomi_gamepad(HidDevice device, ScpBus scpBus, int index)
{
Index = index;
ScpBus = scpBus;
Device = device;
Device.WriteFeatureData(Vibration);

Thread rThread = new Thread(() => rumble_thread(Device));
rThread = new Thread(() => rumble_thread(Device));
// rThread.Priority = ThreadPriority.BelowNormal;
rThread.Start();

Thread iThread = new Thread(() => input_thread(Device, scpBus, index));
iThread = new Thread(() => input_thread(Device, scpBus, index));
iThread.Priority = ThreadPriority.Highest;
iThread.Start();
}

public bool check_connected()
{
return Device.WriteFeatureData(Vibration);
}

public void unplug()
{
Running = false;
rThread.Join();
iThread.Join();
ScpBus.Unplug(Index);
Device.CloseDevice();
}

private void rumble_thread(HidDevice Device)
{
byte[] local_vibration = { 0x20, 0x00, 0x00 };
while (true)
while (Running)
{
rumble_mutex.WaitOne();
if (local_vibration[2] != Vibration[2] || Vibration[1] != local_vibration[1])
Expand All @@ -55,7 +75,7 @@ private void input_thread(HidDevice Device, ScpBus scpBus, int index)
int timeout = 30;
long last_changed = 0;
long last_mi_button = 0;
while (true)
while (Running)
{
HidDeviceData data = Device.Read(timeout);
var currentState = data.Data;
Expand Down