Example #1
0
        public static unsafe DualShock4UpdateReport?TryParse(int vendorId, int productId, byte[] rawHid)
        {
            if (vendorId != VendorId.Sony)
            {
                return(null);
            }
            if ((productId != ProductId.Dualshock4) && (productId != ProductId.Dualshock4_v2) && (productId != ProductId.WirelessDongle))
            {
                return(null);
            }

            var report = new DualShock4UpdateReport();
            var size   = Marshal.SizeOf(report);

            if (size > rawHid.Length)
            {
                return(null);
            }
            if (size < rawHid.Length)
            {
                return(null);
            }
            Marshal.Copy(rawHid, 0, new IntPtr(&report), size);
            if (report.ReportType != 0x01)
            {
                return(null);
            }
            return(report);
        }
        void DrawControls(Graphics fx, DualShock4UpdateReport gp)
        {
            // Top strip
            DrawAxises(fx, BatteryArea, new Vector2(gp.Battery / 8f * 2 - 1, 0), false);

            // Left strip
            DrawButton(fx, LeftShoulderArea, gp.LeftShoulder);
            DrawAxises(fx, LeftTriggerArea, new Vector2(0, gp.LeftTrigger / 255f * 2 - 1), gp.LeftTriggerB);

            // Right strip
            DrawButton(fx, RightShoulderArea, gp.RightShoulder);
            DrawAxises(fx, RightTriggerArea, new Vector2(0, gp.RightTrigger / 255f * 2 - 1), gp.RightTriggerB);

            // Top Row

            fx.DrawString("DPad", Font, Brushes.Black, DpadArea);
            DrawBits(fx, DpadArea, new bool[3, 3] {
                { false, gp.DPadUp, false },
                { gp.DPadLeft, false, gp.DPadRight },
                { false, gp.DPadDown, false }
            });

            DrawButton(fx, ShareButtonArea, gp.Share);

            var touches = gp.TouchEvents.ToList();
            var fingers = touches.SelectMany(t => t.Fingers).Where(f => f.Held).Select(f => new Vector2(f.X / 1919f * 2 - 1, f.Y / 942f * 2 - 1)).ToArray();

            fx.DrawString("Touchpad", Font, Brushes.Black, TouchpadArea);
            DrawAxises(fx, TouchpadArea, fingers, gp.TouchpadClick);

            DrawButton(fx, OptionsButtonArea, gp.Options);

            fx.DrawString("Face Buttons", Font, Brushes.Black, FaceButtonsArea);
            DrawBits(fx, FaceButtonsArea, new bool[3, 3] {
                { false, gp.Triangle, false },
                { gp.Square, false, gp.Circle },
                { false, gp.Cross, false }
            });

            // Low row
            fx.DrawString("Left Stick", Font, Brushes.Black, LeftStickArea);
            DrawAxises(fx, LeftStickArea, new Vector2(gp.LeftStickX / 255f * 2 - 1, gp.LeftStickY / 255f * 2 - 1), gp.LeftStick);
            DrawButton(fx, GuideArea, gp.Guide);
            fx.DrawString("Right Stick", Font, Brushes.Black, RightStickArea);
            DrawAxises(fx, RightStickArea, new Vector2(gp.RightStickX / 255f * 2 - 1, gp.RightStickY / 255f * 2 - 1), gp.RightStick);

            // Lowest row
            var exaggerate = 4f;

            fx.DrawString("Accel", Font, Brushes.Black, AccelArea);
            DrawAxises(fx, AccelArea, exaggerate * new Vector3(gp.AccelX * 1f / short.MaxValue, gp.AccelY * 1f / short.MaxValue, gp.AccelZ * 1f / short.MaxValue), false);

            fx.DrawString("Gyro", Font, Brushes.Black, GyroArea);
            DrawAxises(fx, GyroArea, exaggerate * new Vector3(gp.GyroX * 1f / short.MaxValue, gp.GyroY * 1f / short.MaxValue, gp.GyroZ * 1f / short.MaxValue), false);
        }
        void DrawStats(Graphics fx, DualShock4UpdateReport gp)
        {
            int             y         = 10;
            Action <string> writeLine = (line) => { fx.DrawString(line, Font, Brushes.Black, 500, y); y += 12; };

            writeLine(string.Format("Events:                    {0}", Events++));
            writeLine(string.Format("Rate:                     ~{0:N1}hz", RefreshRate));
            writeLine(string.Format("Time:                      {0}", gp.Time));
            writeLine(string.Format("Sensors time:              {0}", gp.SensorsTime.ToString().PadLeft(6, ' ')));
            writeLine(string.Format("                         (+{0})", ((ushort)(gp.SensorsTime - PrevReport.SensorsTime)).ToString().PadLeft(6, ' ')));            // ~ 1500 dT with ~ 125hz packets would imply 187.5 khz clock?  I note this is half the USB rate http://www.psdevwiki.com/ps4/DS4-USB lists (250hz)
            writeLine(string.Format("Temperature:               {0}", ToHex(gp.Temperature)));
            writeLine(string.Format("_Unknown_1:                {0}", ToHex(gp._Unknown_1)));
            writeLine(string.Format("Flags_Unknown_Flags:       {0}", ToHex(gp.Flags_Unknown_Flags)));
            writeLine(string.Format("  Connected:               {0}", gp.Connected));
            writeLine(string.Format("  UsbConnected:            {0}", gp.UsbConnected));
            writeLine(string.Format("  HeadphonesConnected:     {0}", gp.HeadphonesConnected));
            writeLine(string.Format("  Battery:                 {0}", ToHex(gp.Battery)));
            writeLine(string.Format("_Unknown_TouchEventCount:  {0}", ToHex((byte)(gp._Unknown_TouchEventCount & 0x7C))));
            writeLine(string.Format("Final padding:             {0} {1} {2}", ToHex(gp._Unknown_End_0), ToHex(gp._Unknown_End_1), ToHex(gp._Unknown_End_2)));
            writeLine("");
        }
        private void Device_RawInput(object sender, RawInputEventArgs e)
        {
            var device = Device.GetDevices().FirstOrDefault(d => d.Handle == e.Device);

            if (device == null)
            {
                return;                             // XXX: Display state?
            }
            var hidInfo = device as HidInfo;

            if (hidInfo == null)
            {
                return;
            }
            if (hidInfo.ProductId != (int)ProductId.Dualshock4_v2)
            {
                return;
            }
            //if (hidInfo.ProductId != (int)ProductId.WirelessDongle) return;

            var now = DateTime.Now;

            ++Packets;
            if (Packets >= 100)
            {
                RefreshRate  = (float)(Packets / (now - LastRRUpdate).TotalSeconds);
                LastRRUpdate = now;
                Packets      = 0;
            }

            PrevTime   = LastTime;
            PrevReport = LastReport;

            LastTime   = now;
            LastReport = DualShock4UpdateReport.TryParse(hidInfo.VendorId, hidInfo.ProductId, e.RawData) ?? LastReport;
        }