Пример #1
0
 public void getPreviousState(ControllerState state)
 {
     pState.CopyTo(state);
 }
Пример #2
0
        private void performEAll4Input()
        {
            firstActive = DateTime.UtcNow;
            System.Timers.Timer readTimeout = new System.Timers.Timer(); // Await 30 seconds for the initial packet, then 3 seconds thereafter.
            readTimeout.Elapsed += delegate { HidDevice.CancelIO(); };
            List <long> Latency = new List <long>();
            long        oldtime = 0;
            Stopwatch   sw      = new Stopwatch();

            sw.Start();
            while (true)
            {
                string currerror = string.Empty;
                Latency.Add(sw.ElapsedMilliseconds - oldtime);
                oldtime = sw.ElapsedMilliseconds;

                if (Latency.Count > 100)
                {
                    Latency.RemoveAt(0);
                }

                this.Latency = Latency.Average();

                if (this.Latency > 10 && !warn && sw.ElapsedMilliseconds > 4000)
                {
                    warn = true;
                    //System.Diagnostics.Trace.WriteLine(System.DateTime.UtcNow.ToString("o") + "> " + "Controller " + /*this.DeviceNum*/ + 1 + " (" + this.MacAddress + ") is experiencing latency issues. Currently at " + Math.Round(this.Latency, 2).ToString() + "ms of recomended maximum 10ms");
                }
                else if (this.Latency <= 10 && warn)
                {
                    warn = false;
                }

                if (readTimeout.Interval != 3000.0)
                {
                    if (readTimeout.Interval != 30000.0)
                    {
                        readTimeout.Interval = 30000.0;
                    }
                    else
                    {
                        readTimeout.Interval = 3000.0;
                    }
                }
                readTimeout.Enabled = true;
                if (conType != ConnectionType.USB)
                {
                    HidDevice.ReadStatus res = hDevice.ReadFile(btInputReport);
                    readTimeout.Enabled = false;
                    if (res == HidDevice.ReadStatus.Success)
                    {
                        _isAvaliable = true;
                        Array.Copy(btInputReport, 0, inputReport, 0, inputReport.Length);
                    }
                    else
                    {
                        _isAvaliable = false;
                        Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
                        Log.LogToTray(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") +
                                      "> disconnect due to read failure(notUsb): " + Marshal.GetLastWin32Error());
                        Nlog.Debug(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") +
                                   "> disconnect due to read failure(notUsb): " + Marshal.GetLastWin32Error());
                        IsDisconnecting = true;
                        if (Removal != null)
                        {
                            Removal(this, EventArgs.Empty);
                        }

                        break;
                        return;
                    }



                    //else
                    //{
                    //    Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
                    //    sendOutputReport(true); // Kick Windows into noticing the disconnection.
                    //    StopOutputUpdate();
                    //    IsDisconnecting = true;
                    //    if (Removal != null)
                    //        Removal(this, EventArgs.Empty);
                    //    return;

                    //}
                }
                else
                {
                    HidDevice.ReadStatus res = hDevice.ReadFile(inputReport);
                    readTimeout.Enabled = false;
                    if (res != HidDevice.ReadStatus.Success)
                    {
                        Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
                        Log.LogToTray(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") +
                                      "> disconnect due to read failure(USB): " + Marshal.GetLastWin32Error());

                        Nlog.Debug(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") +
                                   "> disconnect due to read failure(USB): " + Marshal.GetLastWin32Error());
                        StopOutputUpdate();
                        IsDisconnecting = true;
                        if (Removal != null)
                        {
                            Removal(this, EventArgs.Empty);
                        }
                        return;
                    }
                }
                //if (ConnectionType == ConnectionType.BT && btInputReport[0] != 0x11)
                //{
                //    //Received incorrect report, skip it
                //    continue;
                //}
                DateTime utcNow = System.DateTime.UtcNow; // timestamp with UTC in case system time zone changes
                //resetHapticState();
                cState.ReportTimeStamp = utcNow;
                cState.LX = inputReport[4]; // left joystick x-axis//左摇杆x轴
                cState.LY = inputReport[5]; // left joystick y-axis//左摇杆Y轴
                cState.RX = inputReport[6]; // right joystick x-axis//右摇杆x轴
                cState.RY = inputReport[7]; //right joystick y-axis//右摇杆y轴


                cState.LT = inputReport[8];
                cState.RT = inputReport[9];
                cState.LB = ((byte)inputReport[1] & Convert.ToByte(64)) != 0;
                cState.RB = ((byte)inputReport[1] & Convert.ToByte(128)) != 0;

                cState.A = ((byte)inputReport[1] & Convert.ToByte(1)) != 0;  // ok
                cState.B = ((byte)inputReport[1] & Convert.ToByte(2)) != 0;  //ok
                cState.X = ((byte)inputReport[1] & Convert.ToByte(8)) != 0;  //ok
                cState.Y = ((byte)inputReport[1] & Convert.ToByte(16)) != 0; //ok
                switch (inputReport[3])
                {
                case 0: cState.DpadUp = true; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = false; break;   //up

                case 1: cState.DpadUp = true; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = true; break;    //up right//fixed on 2016-12-28

                case 2: cState.DpadUp = false; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = true; break;   //right

                case 3: cState.DpadUp = false; cState.DpadDown = true; cState.DpadLeft = false; cState.DpadRight = true; break;    //down right

                case 4: cState.DpadUp = false; cState.DpadDown = true; cState.DpadLeft = false; cState.DpadRight = false; break;   //down

                case 5: cState.DpadUp = false; cState.DpadDown = true; cState.DpadLeft = true; cState.DpadRight = false; break;    //down left

                case 6: cState.DpadUp = false; cState.DpadDown = false; cState.DpadLeft = true; cState.DpadRight = false; break;   // left

                case 7: cState.DpadUp = true; cState.DpadDown = false; cState.DpadLeft = true; cState.DpadRight = false; break;    //up left

                default: cState.DpadUp = false; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = false; break;
                }

                cState.RS = ((byte)inputReport[2] & Convert.ToByte(64)) != 0;
                var leftStick = ((byte)inputReport[2] & Convert.ToByte(32)) != 0;
                cState.LS = leftStick;
                var menu = ((byte)inputReport[2] & Convert.ToByte(8)) != 0;
                var back = ((byte)inputReport[2] & Convert.ToByte(4)) != 0;
                cState.Start = menu;
                cState.Back  = back;


                cState.Guide = menu && leftStick;
                // XXX fix initialization ordering so the null checks all go away

                //battery level, the be-top protocal look like not contain battery data.. can not display battery level for now.
                //var batteryLevel = Convert.ToInt32(inputReport[18]) / 255;
                //battery = batteryLevel;

                //cState.Battery = 60;

                if (Report != null)
                {
                    Report(this, EventArgs.Empty);
                }
                //sendOutputReport(false);

                // the be-top bluetooth report protocal unknow fo now , the rumble function can not supported.
                //sendOutputReport(false);


                if (!string.IsNullOrEmpty(error))
                {
                    error = string.Empty;
                }
                if (!string.IsNullOrEmpty(currerror))
                {
                    error = currerror;
                }
                cState.CopyTo(pState);
            }
        }