private void calibrateTrigger(string controlName, SetTrigger setTrigger, GetTrigger getTrigger, SetCalibration setCalibration, bool isInverted) { float[] actualValues = new float[byte.MaxValue + 1]; byte[] calibrationTable = new byte[byte.MaxValue + 1]; // Read the GamePad Trigger deflection for each wiper value for (int wiper = 0; wiper <= byte.MaxValue && stopButton.Enabled; wiper++) { float deflection = (float)wiper / (float)byte.MaxValue; setTrigger(deflection); Application.DoEvents(); Thread.Sleep(16); switch (calibrationEnabledComboBox.Text) { case "All High": actualValues[wiper] = 1.0F; break; case "All Centered": actualValues[wiper] = 0.0F; break; case "All Low": actualValues[wiper] = 0.0F; break; default: actualValues[wiper] = getTrigger(); break; } } // Find value array entry whose value is closest to zero float zero = float.PositiveInfinity; Array.ForEach<float>(actualValues, (f) => { zero = (Math.Abs(f) < Math.Abs(zero)) ? f : zero; }); // Find the last entry whose value is closest to zero int lastZero = Array.FindLastIndex<float>(actualValues, (f) => (f == zero)); if (lastZero == -1) lastZero = byte.MinValue; // Find *any* array entry whose value is closest to 1.0F float plusOne = float.PositiveInfinity; Array.ForEach<float>(actualValues, (f) => { plusOne = (Math.Abs(f - 1.0F) < Math.Abs(plusOne - 1.0F)) ? f : plusOne; }); // Find the first array entry whose value is closest to 1.0F int firstPlusOne = Array.FindIndex<float>(actualValues, (f) => (f == plusOne)); if (firstPlusOne == -1) firstPlusOne = byte.MaxValue; for (int wiper = 0; wiper <= byte.MaxValue; wiper++) { float deflection = (float)wiper / (float)byte.MaxValue; int match = firstPlusOne; for (int index = lastZero; index <= firstPlusOne; index++) { if (Math.Abs(actualValues[index] - deflection) < Math.Abs(actualValues[match] - deflection)) match = index; } calibrationTable[wiper] = (byte)match; } setTrigger(0.0F); graphTrigger(controlName + " Calibrated (Simulation)", setTrigger, getTrigger, calibrationTable); writeCalibrationTable(controlName, calibrationTable, isInverted); setCalibration(calibrationTable); }
private void calibrateTrigger(string controlName, SetTrigger setTrigger, GetTrigger getTrigger, SetCalibration setCalibration, bool isInverted) { float[] actualValues = new float[byte.MaxValue + 1]; byte[] calibrationTable = new byte[byte.MaxValue + 1]; // Read the GamePad Trigger deflection for each wiper value for (int wiper = 0; wiper <= byte.MaxValue && stopButton.Enabled; wiper++) { float deflection = (float)wiper / (float)byte.MaxValue; setTrigger(deflection); Application.DoEvents(); Thread.Sleep(16); switch (calibrationEnabledComboBox.Text) { case "All High": actualValues[wiper] = 1.0F; break; case "All Centered": actualValues[wiper] = 0.0F; break; case "All Low": actualValues[wiper] = 0.0F; break; default: actualValues[wiper] = getTrigger(); break; } } // Find value array entry whose value is closest to zero float zero = float.PositiveInfinity; Array.ForEach <float>(actualValues, (f) => { zero = (Math.Abs(f) < Math.Abs(zero)) ? f : zero; }); // Find the last entry whose value is closest to zero int lastZero = Array.FindLastIndex <float>(actualValues, (f) => (f == zero)); if (lastZero == -1) { lastZero = byte.MinValue; } // Find *any* array entry whose value is closest to 1.0F float plusOne = float.PositiveInfinity; Array.ForEach <float>(actualValues, (f) => { plusOne = (Math.Abs(f - 1.0F) < Math.Abs(plusOne - 1.0F)) ? f : plusOne; }); // Find the first array entry whose value is closest to 1.0F int firstPlusOne = Array.FindIndex <float>(actualValues, (f) => (f == plusOne)); if (firstPlusOne == -1) { firstPlusOne = byte.MaxValue; } for (int wiper = 0; wiper <= byte.MaxValue; wiper++) { float deflection = (float)wiper / (float)byte.MaxValue; int match = firstPlusOne; for (int index = lastZero; index <= firstPlusOne; index++) { if (Math.Abs(actualValues[index] - deflection) < Math.Abs(actualValues[match] - deflection)) { match = index; } } calibrationTable[wiper] = (byte)match; } setTrigger(0.0F); graphTrigger(controlName + " Calibrated (Simulation)", setTrigger, getTrigger, calibrationTable); writeCalibrationTable(controlName, calibrationTable, isInverted); setCalibration(calibrationTable); }