static void DeviceDisconnectedEvent(USBH_Device device) { //Check if device is a PS3 Controller if (device.VENDOR_ID != PS3Controller.PS3_VENDOR_ID || device.PRODUCT_ID != PS3Controller.PS3_PRODUCT_ID) { if (device.TYPE == USBH_DeviceType.Hub) { WriteSerial("Hub Disconnected"); for (byte i = 0; i < PS3Connected; i++)//All controllers has to be stopped { runThread[i] = false; PS3[i].Abort(); BT.Abort(); } } else if (device.PRODUCT_ID == Bluetooth.CSR_PRODUCT_ID && device.VENDOR_ID == Bluetooth.CSR_VENDOR_ID) { WriteSerial("Bluetooth Dongle Disconnected"); PS3RumbleBT = false; BT.Abort(); } else if (device.VENDOR_ID == PS3Move.PS3MOVE_VENDOR_ID && device.PRODUCT_ID == PS3Move.PS3MOVE_PRODUCT_ID) { WriteSerial("PS3 Move Controller Disconnected"); Move.Abort(); } else if (device.VENDOR_ID == PS3Navigation.PS3NAVIGATION_VENDOR_ID && device.PRODUCT_ID == PS3Navigation.PS3NAVIGATION_PRODUCT_ID) { WriteSerial("PS3 Navigation Disconnected"); Navigation.Abort(); } else { WriteSerial("Unknown Device Disconnected"); } return; } PS3Connected--; if (device.ID == PS3Device[PS3Connected].ID) //Check if it is the last one { runThread[PS3Connected] = false; //Stop the loop for the last controller PS3Rumble[PS3Connected] = false; //Also stop the rumble thread if active PS3[PS3Connected].Abort(); //Abort the reading thread WriteSerial("PS3 Controller: " + (PS3Connected + 1) + " - Disconnected"); } else { byte PS3number; for (PS3number = 0; PS3number < PS3Connected; PS3number++)//Check which number that was disconnected { if (device.ID == PS3Device[PS3number].ID) { break; } } runThread[PS3number] = false; PS3Rumble[PS3number] = false;//Also stop the rumble thread if active PS3[PS3number].Abort(); WriteSerial("PS3 Controller: " + (PS3number + 1) + " - Disconnected"); //Move all the controllers from that point one down for (byte i = PS3number; i < PS3Connected; i++)//Does not include the last one, as it is allways moved one down { //Stop that thread including the next one runThread[i] = false; PS3[i].Abort(); runThread[i + 1] = false; PS3[i + 1].Abort(); //Move one down PS3[i] = new PS3Controller(PS3Device[i + 1]); PS3Device[i] = PS3Device[i + 1]; PS3SetLED(i); runThread[i] = true; WriteSerial("PS3 Controller: " + (i + 2) + " - Changed to " + (i + 1)); } } }