//private static List<UsbEndpointWriter> writers; //TODO: Implement multiple controller writers
 //private static List<UsbEndpointReader> readers; //TODO: Implement multiple controller writers
 public MainWindow()
 {
     System.Windows.Application.Current.MainWindow = this;
     InitializeComponent();
     ValidateRegistrySettings();
     jController = new JoystickController();
     cController = new ChatpadController();
     try
     {
         MyUsbFinder = new UsbDeviceFinder(1118, 1817);
         MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
         if (MyUsbDevice == null) //support other product id (knock-off?)
         {
             ErrorLogging.WriteLogEntry("USB Device (1118,1817) not found: ", ErrorLogging.LogLevel.Fatal);
             MyUsbFinder = new UsbDeviceFinder(1118, 657);
             MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
             if (MyUsbDevice == null)
             {
                 ErrorLogging.WriteLogEntry("USB Device (1118,657) not found: ", ErrorLogging.LogLevel.Fatal);
             }
         }
     }
     catch (Exception e)
     {
         ErrorLogging.WriteLogEntry(String.Format("No USB Device found: {0}", e.InnerException), ErrorLogging.LogLevel.Fatal);
     }
     try
     {
         IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
         if (!ReferenceEquals(wholeUsbDevice, null))
         {
             try
             {
                 wholeUsbDevice.SetConfiguration(1);
                 wholeUsbDevice.ClaimInterface(0);
                 reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
                 writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
                 reader.DataReceived += new EventHandler<EndpointDataEventArgs>(reader_DataReceived);
                 reader.DataReceivedEnabled = true;
             }
             catch (Exception e)
             {
                 ErrorLogging.WriteLogEntry(String.Format("Error opening endpoints: {0}", e.InnerException), ErrorLogging.LogLevel.Fatal);
             }
         }
         else
         {
             ErrorLogging.WriteLogEntry("Whole USB device is not implemented", ErrorLogging.LogLevel.Error);
         }
     }
     catch (Exception e)
     {
         ErrorLogging.WriteLogEntry(String.Format("Failure converting usb device to whole usb device: {0}", e.InnerException), ErrorLogging.LogLevel.Fatal);
     }
     try
     {
         timer.Start();
         InitializeChatpad();
         InitializeController();
     }
     catch (Exception e)
     {
         ErrorLogging.WriteLogEntry(String.Format("Error in init of chatpad or controller: {0}", e.InnerException), ErrorLogging.LogLevel.Fatal);
     }
     timer.Tick += new EventHandler(dispatcherTimer_Tick);
     timer.Interval = new TimeSpan(0, 0, 0, 1, 0);
     ni.Icon = new Icon(@"Images\controller.ico");
     ni.Visible = true;
     ni.Click +=
     delegate(object sender, EventArgs args)
     {
         this.Show();
         this.WindowState = WindowState.Normal;
     };
 }
        public static void OutputMappingForJoyStick(byte[] response, JoystickController jController, uint controllerIndex,uint deadzone)
        {
            if (response[1] == 1 && response[3] == 240 && response[5] == 19)
            {
                if (response[8] == 0)
                {
                    jController.AxisSet(controllerIndex, 0, 8);
                }
                if (response[9] == 0)
                {
                    jController.AxisSet(controllerIndex, 0, 9);
                }
                if (response[11] < deadzone && response[13] < deadzone)
                {
                    jController.AxisSet(controllerIndex, 0, 11);
                    jController.AxisSet(controllerIndex, 0, 13);
                }
                if (response[15] < deadzone && response[17] < deadzone)
                {
                    jController.AxisSet(controllerIndex, 0, 15);
                    jController.AxisSet(controllerIndex, 0, 17);
                }
                //0 1 0 240 0 19 0 XX 0 0 32 12 92 0 44 6 163 247
                //a=16, b=32, x=64, y=128
                if (response[7] > 0)
                {
                    var b = (Bits)response[7];

                    string[] sArray = b.ToString().Replace(" ", string.Empty).Split(',');
                    foreach (string s in sArray)
                    {
                        jController.ButtonToggle(controllerIndex, jController.GetJoystickKeyValue(s, 1), true);
                    }
                }
                else // || response[4] == 0)
                {
                    var b = (Bits)255;
                    string[] sArray = b.ToString().Replace(" ", string.Empty).Split(',');
                    foreach (string s in sArray)
                    {
                        if (s != "NA")
                        {
                            jController.ButtonToggle(controllerIndex, jController.GetJoystickKeyValue(s, 1), false);
                        }
                    }
                }
                if (response[6] > 0)
                {
                    var b = (Bits2)response[6];

                    string[] sArray = b.ToString().Replace(" ", string.Empty).Split(',');
                    foreach (string s in sArray)
                    {
                        if (s.Contains("UP") || s.Contains("DOWN") || s.Contains("LEFT") || s.Contains("RIGHT"))
                        {
                            previousDPad.Enqueue(s);
                            if (previousDPad.Count > 2)
                            {
                                //Store a queue of dpad events as you can only press two directions on the dpad at the same time
                                //But an evant to release the dpad isn't send until all directions are released
                                string dequeudDPad = previousDPad.Dequeue();
                                jController.ButtonToggle(controllerIndex, jController.GetJoystickKeyValue(dequeudDPad, 2), false);
                            }
                        }
                        jController.ButtonToggle(controllerIndex, jController.GetJoystickKeyValue(s, 2), true);
                    }
                }
                else
                {
                    var b2 = (Bits2)255;
                    string[] sArray = b2.ToString().Replace(" ", string.Empty).Split(',');
                    foreach (string s in sArray)
                    {
                        if (s != "NA")
                        {
                            jController.ButtonToggle(controllerIndex, jController.GetJoystickKeyValue(s, 2), false);
                        }
                    }
                    previousDPad.Clear();
                }
                if (response[8] > 0)
                {
                    //Left Trigger
                    int v = response[8];
                    if (v < 0)
                    {
                        v = 0;
                    }
                    jController.AxisSet(controllerIndex, -response[8]/2, 8);
                }
                if (response[9] > 0)
                {
                    int v = response[9];
                    if (v > 0)
                    {
                        v = 0;
                    }
                    jController.AxisSet(controllerIndex, response[9]/2, 9);
                }
                if (response[11] > deadzone || response[13] > deadzone)
                {
                    int x = response[11];
                    if (x >= 128) //What why? Why does MS provide 4 points to position in 2d space? Why do the values jump at a certain point? Why... Why...
                    {
                        x = x - 255;
                    }
                    jController.AxisSet(controllerIndex, x, 11);
                    int y = response[13];
                    if (y >= 128) //What why? Why does MS provide 4 points to position in 2d space? Why do the values jump at a certain point? Why... Why...
                    {
                        y = y - 255; //Why is the y stick inverted?
                    }
                    jController.AxisSet(controllerIndex, -y, 13);
                }
                if (response[15] > deadzone || response[17] > deadzone)
                {
                    int x = response[15];
                    if (x >= 128) //What why? Why does MS provide 4 points to position in 2d space? Why do the values jump at a certain point? Why... Why...
                    {
                        x = x - 255;
                    }
                    jController.AxisSet(controllerIndex, -x, 15);
                    int y = response[17];
                    if (y >= 128) //What why? Why does MS provide 4 points to position in 2d space? Why do the values jump at a certain point? Why... Why...
                    {
                        y = y - 255; //Why is the y stick inverted?
                    }
                    jController.AxisSet(controllerIndex, -y, 17);
                }
            }
        }