public static bool AutoCalibrate(int deviceId, QLCalibrationType calibrationType, ref int calibrationId) { QLError qlerror = QLError.QL_ERROR_OK; //Initialize the calibration using the inputted data. qlerror = QuickLink2API.QLCalibration_Initialize(deviceId, calibrationId, calibrationType); // If the calibrationId was not valid then create a new calibration container and use it. if (qlerror == QLError.QL_ERROR_INVALID_CALIBRATION_ID) { QuickLink2API.QLCalibration_Create(0, out calibrationId); qlerror = QuickLink2API.QLCalibration_Initialize(deviceId, calibrationId, calibrationType); } // If the initialization failed then print an error and return false. if (qlerror == QLError.QL_ERROR_INVALID_DEVICE_ID) { System.Console.WriteLine("QLCalibration_Initialize() failed with error code {0}.", qlerror); return(false); } //Create a buffer for the targets. This just needs to be large enough to hold the targets. const int bufferSize = 20; int numTargets = bufferSize; QLCalibrationTarget[] targets = new QLCalibrationTarget[bufferSize]; //Get the targets. After the call, numTargets will contain the number of actual targets. qlerror = QuickLink2API.QLCalibration_GetTargets(calibrationId, ref numTargets, targets); // If the buffer was not large enough then print an error and return false. if (qlerror == QLError.QL_ERROR_BUFFER_TOO_SMALL) { System.Console.WriteLine("The target buffer is too small."); return(false); } // Create a window for doing the calibration. CalibrationForm calibrationForm = new CalibrationForm(); calibrationForm.PerformCalibration(calibrationId, numTargets, targets); System.Console.WriteLine("Do you want to improve the calibration? y/n"); while (Console.ReadKey(true).Key == ConsoleKey.Y) { calibrationForm.ImproveCalibration(calibrationId, numTargets, targets); System.Console.WriteLine("Do you want to improve the calibration? y/n"); } QuickLink2API.QLCalibration_Finalize(calibrationId); return(true); }
private static bool Calibrate(int deviceId, string calibrationFilename, int deviceDistance, QLCalibrationType calibrationType, int targetDuration) { // Start the device. QLError error = QuickLink2API.QLDevice_Start(deviceId); if (error != QLError.QL_ERROR_OK) { Console.WriteLine("QLDevice_Start() returned {0}.", error.ToString()); return(false); } bool calibrationSuccessful = false; using (CalibrationForm calibrationForm = new CalibrationForm(deviceId, calibrationType, targetDuration)) { if (calibrationForm.Calibrate()) { // Calculate the total average score. float avg = 0; for (int i = 0; i < calibrationForm.LeftScores.Length; i++) { avg += calibrationForm.LeftScores[i].score; avg += calibrationForm.RightScores[i].score; } avg /= (float)calibrationForm.LeftScores.Length * 2f; Console.WriteLine("Calibration Score: {0}.", avg); while (true) { // Flush the input buffer. while (Console.KeyAvailable) { Console.ReadKey(true); } if (!PromptToApplyCalibration()) { Console.WriteLine("Not applying calibration."); calibrationSuccessful = false; break; } else { error = QuickLink2API.QLCalibration_Finalize(calibrationForm.CalibrationId); if (error != QLError.QL_ERROR_OK) { Console.WriteLine("QLCalibration_Finalize() returned {0}.", error.ToString()); calibrationSuccessful = false; break; } error = QuickLink2API.QLDevice_ApplyCalibration(deviceId, calibrationForm.CalibrationId); if (error != QLError.QL_ERROR_OK) { Console.WriteLine("QLCalibration_ApplyCalibration() returned {0}", error.ToString()); calibrationSuccessful = false; break; } error = QuickLink2API.QLCalibration_Save(calibrationFilename, calibrationForm.CalibrationId); if (error != QLError.QL_ERROR_OK) { Console.WriteLine("QLCalibration_Save() returned {0}", error.ToString()); calibrationSuccessful = false; break; } calibrationSuccessful = true; break; } } } } error = QuickLink2API.QLDevice_Stop(deviceId); if (error != QLError.QL_ERROR_OK) { Console.WriteLine("QLDevice_Stop() returned {0}.", error.ToString()); return(false); } return(calibrationSuccessful); }