Minor optimizations

This commit is contained in:
Travis Nickles 2017-05-17 01:59:09 -07:00
parent 1958f082c5
commit 2b741533bb
5 changed files with 35 additions and 21 deletions

View File

@ -657,7 +657,7 @@ namespace DS4Windows
device.firstReport = true; device.firstReport = true;
OnDeviceStatusChanged(this, ind); 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()); OnBatteryStatusChange(this, ind, cState.Battery, device.isCharging());
} }

View File

@ -386,7 +386,7 @@ namespace DS4Windows
return value1 * percent + value2 * (1 - percent); 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) if (value > max)
return max; return max;
@ -395,6 +395,7 @@ namespace DS4Windows
else else
return value; return value;
} }
*/
private static int ClampInt(int min, int value, int max) private static int ClampInt(int min, int value, int max)
{ {
@ -514,8 +515,8 @@ namespace DS4Windows
if (lsSquared > lsDeadzoneSquared) if (lsSquared > lsDeadzoneSquared)
{ {
double currentX = Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue); double currentX = Global.Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue);
double currentY = Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue); double currentY = Global.Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue);
//currentX = (byte)((dState.LX >= 127.5f) ? Math.Min(dState.LX, maxZoneX) : Math.Max(dState.LX, maxZoneX)); //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)); //currentY = (byte)((dState.LY >= 127.5f) ? Math.Min(dState.LY, maxZoneY) : Math.Max(dState.LY, maxZoneY));
tempOutputX = ((currentX - 127.5f - tempLsXDead) / (double)(maxZoneX - tempLsXDead)); tempOutputX = ((currentX - 127.5f - tempLsXDead) / (double)(maxZoneX - tempLsXDead));
@ -524,8 +525,8 @@ namespace DS4Windows
} }
else else
{ {
double currentX = Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue); double currentX = Global.Clamp(maxZoneXNegValue, dState.LX, maxZoneXPosValue);
double currentY = Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue); double currentY = Global.Clamp(maxZoneYNegValue, dState.LY, maxZoneYPosValue);
tempOutputX = (currentX - 127.5f) / (double)(maxZoneX); tempOutputX = (currentX - 127.5f) / (double)(maxZoneX);
tempOutputY = (currentY - 127.5f) / (double)(maxZoneY); tempOutputY = (currentY - 127.5f) / (double)(maxZoneY);
} }
@ -592,8 +593,8 @@ namespace DS4Windows
if (rsSquared > rsDeadzoneSquared) if (rsSquared > rsDeadzoneSquared)
{ {
double currentX = Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue); double currentX = Global.Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue);
double currentY = Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue); double currentY = Global.Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue);
tempOutputX = ((currentX - 127.5f - tempRsXDead) / (double)(maxZoneX - tempRsXDead)); tempOutputX = ((currentX - 127.5f - tempRsXDead) / (double)(maxZoneX - tempRsXDead));
tempOutputY = ((currentY - 127.5f - tempRsYDead) / (double)(maxZoneY - tempRsYDead)); tempOutputY = ((currentY - 127.5f - tempRsYDead) / (double)(maxZoneY - tempRsYDead));
@ -603,8 +604,8 @@ namespace DS4Windows
} }
else else
{ {
double currentX = Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue); double currentX = Global.Clamp(maxZoneXNegValue, dState.RX, maxZoneXPosValue);
double currentY = Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue); double currentY = Global.Clamp(maxZoneYNegValue, dState.RY, maxZoneYPosValue);
tempOutputX = (currentX - 127.5f) / (double)(maxZoneX); tempOutputX = (currentX - 127.5f) / (double)(maxZoneX);
tempOutputY = (currentY - 127.5f) / (double)(maxZoneY); tempOutputY = (currentY - 127.5f) / (double)(maxZoneY);
@ -653,7 +654,7 @@ namespace DS4Windows
{ {
if (cState.L2 > l2Deadzone) 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)); tempL2Output = ((current - l2Deadzone) / (double)(maxValue - l2Deadzone));
} }
else else
@ -691,7 +692,7 @@ namespace DS4Windows
{ {
if (cState.R2 > r2Deadzone) 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)); tempR2Output = ((current - r2Deadzone) / (double)(maxValue - r2Deadzone));
} }
else else
@ -718,24 +719,24 @@ namespace DS4Windows
double lsSens = getLSSens(device); double lsSens = getLSSens(device);
if (lsSens != 1.0) if (lsSens != 1.0)
{ {
dState.LX = (byte)Clamp(0, lsSens * (dState.LX - 127.5f) + 127.5f, 255); dState.LX = (byte)Global.Clamp(0, lsSens * (dState.LX - 127.5f) + 127.5f, 255);
dState.LY = (byte)Clamp(0, lsSens * (dState.LY - 127.5f) + 127.5f, 255); dState.LY = (byte)Global.Clamp(0, lsSens * (dState.LY - 127.5f) + 127.5f, 255);
} }
double rsSens = getRSSens(device); double rsSens = getRSSens(device);
if (rsSens != 1.0) if (rsSens != 1.0)
{ {
dState.RX = (byte)Clamp(0, rsSens * (dState.RX - 127.5f) + 127.5f, 255); dState.RX = (byte)Global.Clamp(0, rsSens * (dState.RX - 127.5f) + 127.5f, 255);
dState.RY = (byte)Clamp(0, rsSens * (dState.RY - 127.5f) + 127.5f, 255); dState.RY = (byte)Global.Clamp(0, rsSens * (dState.RY - 127.5f) + 127.5f, 255);
} }
double l2Sens = getL2Sens(device); double l2Sens = getL2Sens(device);
if (l2Sens != 1.0) 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); double r2Sens = getR2Sens(device);
if (r2Sens != 1.0) 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; return dState;
} }

View File

@ -1019,6 +1019,16 @@ namespace DS4Windows
R *= 255.0f; G *= 255.0f; B *= 255.0f; R *= 255.0f; G *= 255.0f; B *= 255.0f;
return Color.FromArgb((int)R, (int)G, (int)B); 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;
}
} }

View File

@ -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) if (value > max)
return max; return max;
@ -729,6 +729,7 @@ namespace DS4Windows
else else
return value; return value;
} }
*/
void EnableReadings(bool on) void EnableReadings(bool on)
{ {
@ -854,7 +855,7 @@ namespace DS4Windows
x = -Program.rootHub.ExposedState[(int)nUDSixaxis.Value - 1].GyroX + 127; x = -Program.rootHub.ExposedState[(int)nUDSixaxis.Value - 1].GyroX + 127;
y = Program.rootHub.ExposedState[(int)nUDSixaxis.Value - 1].GyroZ + 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; btnSATrackS.Visible = nUDSXS.Value != 1 || nUDSZS.Value != 1;
if (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), btnSATrackS.Location = new Point((int)((float)nUDSXS.Value * (btnSATrack.Location.X - pnlSATrack.Size.Width / 2f) + pnlSATrack.Size.Width / 2f),

View File

@ -573,17 +573,19 @@ namespace DS4Windows
public double Latency = 0; public double Latency = 0;
public string error; public string error;
public bool firstReport = false; public bool firstReport = false;
public bool oldCharging = false;
private void performDs4Input() private void performDs4Input()
{ {
firstActive = DateTime.UtcNow; firstActive = DateTime.UtcNow;
NativeMethods.HidD_SetNumInputBuffers(hDevice.safeReadHandle.DangerousGetHandle(), 2); NativeMethods.HidD_SetNumInputBuffers(hDevice.safeReadHandle.DangerousGetHandle(), 2);
List<long> Latency = new List<long>(100); List<long> Latency = new List<long>(101); // Set capacity at max + 1 to avoid any list resizing
long oldtime = 0; long oldtime = 0;
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
sw.Start(); sw.Start();
while (!exitInputThread) while (!exitInputThread)
{ {
oldCharging = charging;
string currerror = string.Empty; string currerror = string.Empty;
long curtime = sw.ElapsedMilliseconds; long curtime = sw.ElapsedMilliseconds;
this.lastTimeElapsed = curtime - oldtime; this.lastTimeElapsed = curtime - oldtime;