From 2b741533bb7a27c68dd5ad249f108d3660e6b5b3 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Wed, 17 May 2017 01:59:09 -0700 Subject: [PATCH] Minor optimizations --- DS4Windows/DS4Control/ControlService.cs | 2 +- DS4Windows/DS4Control/Mapping.cs | 35 +++++++++++++------------ DS4Windows/DS4Control/ScpUtil.cs | 10 +++++++ DS4Windows/DS4Forms/Options.cs | 5 ++-- DS4Windows/DS4Library/DS4Device.cs | 4 ++- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index b9d6156..7497462 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -657,7 +657,7 @@ namespace DS4Windows device.firstReport = true; OnDeviceStatusChanged(this, ind); } - else if (pState.Battery != cState.Battery) + else if (pState.Battery != cState.Battery || device.oldCharging != device.isCharging()) { OnBatteryStatusChange(this, ind, cState.Battery, device.isCharging()); } diff --git a/DS4Windows/DS4Control/Mapping.cs b/DS4Windows/DS4Control/Mapping.cs index 71a14aa..169b63b 100644 --- a/DS4Windows/DS4Control/Mapping.cs +++ b/DS4Windows/DS4Control/Mapping.cs @@ -386,7 +386,7 @@ namespace DS4Windows return value1 * percent + value2 * (1 - percent); } - static double Clamp(double min, double value, double max) + /*static double Clamp(double min, double value, double max) { if (value > max) return max; @@ -395,6 +395,7 @@ namespace DS4Windows else return value; } + */ private static int ClampInt(int min, int value, int max) { @@ -514,8 +515,8 @@ namespace DS4Windows if (lsSquared > lsDeadzoneSquared) { - double currentX = Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue); - double currentY = Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue); + double currentX = Global.Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue); + double currentY = Global.Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue); //currentX = (byte)((dState.LX >= 127.5f) ? Math.Min(dState.LX, maxZoneX) : Math.Max(dState.LX, maxZoneX)); //currentY = (byte)((dState.LY >= 127.5f) ? Math.Min(dState.LY, maxZoneY) : Math.Max(dState.LY, maxZoneY)); tempOutputX = ((currentX - 127.5f - tempLsXDead) / (double)(maxZoneX - tempLsXDead)); @@ -524,8 +525,8 @@ namespace DS4Windows } else { - double currentX = Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue); - double currentY = Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue); + double currentX = Global.Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue); + double currentY = Global.Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue); tempOutputX = (currentX - 127.5f) / (double)(maxZoneX); tempOutputY = (currentY - 127.5f) / (double)(maxZoneY); } @@ -592,8 +593,8 @@ namespace DS4Windows if (rsSquared > rsDeadzoneSquared) { - double currentX = Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue); - double currentY = Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue); + double currentX = Global.Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue); + double currentY = Global.Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue); tempOutputX = ((currentX - 127.5f - tempRsXDead) / (double)(maxZoneX - tempRsXDead)); tempOutputY = ((currentY - 127.5f - tempRsYDead) / (double)(maxZoneY - tempRsYDead)); @@ -603,8 +604,8 @@ namespace DS4Windows } else { - double currentX = Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue); - double currentY = Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue); + double currentX = Global.Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue); + double currentY = Global.Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue); tempOutputX = (currentX - 127.5f) / (double)(maxZoneX); tempOutputY = (currentY - 127.5f) / (double)(maxZoneY); @@ -653,7 +654,7 @@ namespace DS4Windows { if (cState.L2 > l2Deadzone) { - double current = Clamp(0, dState.L2, maxValue); + double current = Global.Clamp(0, dState.L2, maxValue); tempL2Output = ((current - l2Deadzone) / (double)(maxValue - l2Deadzone)); } else @@ -691,7 +692,7 @@ namespace DS4Windows { if (cState.R2 > r2Deadzone) { - double current = Clamp(0, dState.R2, maxValue); + double current = Global.Clamp(0, dState.R2, maxValue); tempR2Output = ((current - r2Deadzone) / (double)(maxValue - r2Deadzone)); } else @@ -718,24 +719,24 @@ namespace DS4Windows double lsSens = getLSSens(device); if (lsSens != 1.0) { - dState.LX = (byte)Clamp(0, lsSens * (dState.LX - 127.5f) + 127.5f, 255); - dState.LY = (byte)Clamp(0, lsSens * (dState.LY - 127.5f) + 127.5f, 255); + dState.LX = (byte)Global.Clamp(0, lsSens * (dState.LX - 127.5f) + 127.5f, 255); + dState.LY = (byte)Global.Clamp(0, lsSens * (dState.LY - 127.5f) + 127.5f, 255); } double rsSens = getRSSens(device); if (rsSens != 1.0) { - dState.RX = (byte)Clamp(0, rsSens * (dState.RX - 127.5f) + 127.5f, 255); - dState.RY = (byte)Clamp(0, rsSens * (dState.RY - 127.5f) + 127.5f, 255); + dState.RX = (byte)Global.Clamp(0, rsSens * (dState.RX - 127.5f) + 127.5f, 255); + dState.RY = (byte)Global.Clamp(0, rsSens * (dState.RY - 127.5f) + 127.5f, 255); } double l2Sens = getL2Sens(device); if (l2Sens != 1.0) - dState.L2 = (byte)Clamp(0, l2Sens * dState.L2, 255); + dState.L2 = (byte)Global.Clamp(0, l2Sens * dState.L2, 255); double r2Sens = getR2Sens(device); if (r2Sens != 1.0) - dState.R2 = (byte)Clamp(0, r2Sens * dState.R2, 255); + dState.R2 = (byte)Global.Clamp(0, r2Sens * dState.R2, 255); return dState; } diff --git a/DS4Windows/DS4Control/ScpUtil.cs b/DS4Windows/DS4Control/ScpUtil.cs index 7ea8537..d24eabc 100644 --- a/DS4Windows/DS4Control/ScpUtil.cs +++ b/DS4Windows/DS4Control/ScpUtil.cs @@ -1019,6 +1019,16 @@ namespace DS4Windows R *= 255.0f; G *= 255.0f; B *= 255.0f; return Color.FromArgb((int)R, (int)G, (int)B); } + + public static double Clamp(double min, double value, double max) + { + return (value < min) ? min : (value > max) ? max : value; + } + + private static int ClampInt(int min, int value, int max) + { + return (value < min) ? min : (value > max) ? max : value; + } } diff --git a/DS4Windows/DS4Forms/Options.cs b/DS4Windows/DS4Forms/Options.cs index d6fc561..9c209b2 100644 --- a/DS4Windows/DS4Forms/Options.cs +++ b/DS4Windows/DS4Forms/Options.cs @@ -720,7 +720,7 @@ namespace DS4Windows } } - public double Clamp(double min, double value, double max) + /*public double Clamp(double min, double value, double max) { if (value > max) return max; @@ -729,6 +729,7 @@ namespace DS4Windows else return value; } + */ void EnableReadings(bool on) { @@ -854,7 +855,7 @@ namespace DS4Windows x = -Program.rootHub.ExposedState[(int)nUDSixaxis.Value - 1].GyroX + 127; y = Program.rootHub.ExposedState[(int)nUDSixaxis.Value - 1].GyroZ + 127; - btnSATrack.Location = new Point((int)(dpix * Clamp(0, x / 2.09, pnlSATrack.Size.Width)), (int)(dpiy * Clamp(0, y / 2.09, pnlSATrack.Size.Height))); + btnSATrack.Location = new Point((int)(dpix * Global.Clamp(0, x / 2.09, pnlSATrack.Size.Width)), (int)(dpiy * Global.Clamp(0, y / 2.09, pnlSATrack.Size.Height))); btnSATrackS.Visible = nUDSXS.Value != 1 || nUDSZS.Value != 1; if (nUDSXS.Value != 1 || nUDSZS.Value != 1) btnSATrackS.Location = new Point((int)((float)nUDSXS.Value * (btnSATrack.Location.X - pnlSATrack.Size.Width / 2f) + pnlSATrack.Size.Width / 2f), diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index 077ed85..3b0c143 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -573,17 +573,19 @@ namespace DS4Windows public double Latency = 0; public string error; public bool firstReport = false; + public bool oldCharging = false; private void performDs4Input() { firstActive = DateTime.UtcNow; NativeMethods.HidD_SetNumInputBuffers(hDevice.safeReadHandle.DangerousGetHandle(), 2); - List Latency = new List(100); + List Latency = new List(101); // Set capacity at max + 1 to avoid any list resizing long oldtime = 0; Stopwatch sw = new Stopwatch(); sw.Start(); while (!exitInputThread) { + oldCharging = charging; string currerror = string.Empty; long curtime = sw.ElapsedMilliseconds; this.lastTimeElapsed = curtime - oldtime;