/// <summary> /// Constructor for a Windows Form that performs an eye tracker calibration routine with /// either 5, 9, or 16 points. Each target is displayed for a settable duration. /// </summary> /// <seealso cref="T:QuickLink2DotNetHelper.QLHelper.CalibrationForm"/> /// <param name="deviceId"> /// The ID of the device to be calibrated. This is the value found in the /// <see cref="QLHelper.DeviceId"/> field. /// </param> /// <param name="calibrationType"> /// The type of calibration to perform (5, 9, or 16-point). Any member of /// <see cref="QuickLink2DotNet.QLCalibrationType"/> is accepted. /// </param> /// <param name="targetDuration"> /// The duration, in milliseconds, to show each target on the screen during the calibration /// sequence. /// </param> public CalibrationForm(int deviceId, QLCalibrationType calibrationType, int targetDuration) { this._deviceId = deviceId; this._calibrationType = calibrationType; this._targetDuration = targetDuration; this._calibrationId = 0; QLHelper.CalibrationTypeToNumberOfPoints(this._calibrationType, out this._numberOfTargets); this._leftScores = new QLCalibrationScore[this._numberOfTargets]; this._rightScores = new QLCalibrationScore[this._numberOfTargets]; this._targets = new QLCalibrationTarget[this._numberOfTargets]; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Width = Screen.PrimaryScreen.WorkingArea.Width; this.Height = Screen.PrimaryScreen.WorkingArea.Height; this.TopMost = false; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; this.Hide(); this._calibrationPictureBox = new System.Windows.Forms.PictureBox(); this._calibrationPictureBox.BackColor = System.Drawing.SystemColors.ButtonShadow; this._calibrationPictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this._calibrationPictureBox.ClientSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); this.Controls.Add(this._calibrationPictureBox); this._calibrationPictureBox.Paint += new PaintEventHandler(CalibrationPictureBoxPaint); }
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 NumberOfPointsToCalibrationType(int numberOfPoints, out QLCalibrationType calibrationType) { switch (numberOfPoints) { case 5: calibrationType = QLCalibrationType.QL_CALIBRATION_TYPE_5; return(true); case 9: calibrationType = QLCalibrationType.QL_CALIBRATION_TYPE_9; return(true); case 16: calibrationType = QLCalibrationType.QL_CALIBRATION_TYPE_16; return(true); default: calibrationType = DefaultCalibrationType; return(false); } }
private const int DefaultDeviceDistance = 55; // centimeters. private static bool CalibrationTypeToNumberOfPoints(QLCalibrationType calibrationType, out int numberOfPoints) { switch (calibrationType) { case QLCalibrationType.QL_CALIBRATION_TYPE_5: numberOfPoints = 5; return(true); case QLCalibrationType.QL_CALIBRATION_TYPE_9: numberOfPoints = 9; return(true); case QLCalibrationType.QL_CALIBRATION_TYPE_16: numberOfPoints = 16; return(true); default: numberOfPoints = 0; return(false); } }
// Returns false on 'q' for cancellation of configuration; otherwise // returns true. private static bool PromptForCalibrationType(out QLCalibrationType calibrationType) { calibrationType = DefaultCalibrationType; while (true) { // Flush the input buffer. while (Console.KeyAvailable) { Console.ReadKey(true); } Console.Write(" Perform 5, 9, or 16-point calibration ({0}): ", (calibrationType == QLCalibrationType.QL_CALIBRATION_TYPE_5) ? "5" : (calibrationType == QLCalibrationType.QL_CALIBRATION_TYPE_9) ? "9" : "16"); string input = Console.ReadLine(); if (input.Length == 0) { return(true); } else if (input.ToLower().Equals("q")) { return(false); } else { try { int numberOfPoints = int.Parse(input); if (NumberOfPointsToCalibrationType(numberOfPoints, out calibrationType)) { return(true); } } catch (ArgumentNullException) { } catch (FormatException) { } catch (OverflowException) { } } } }
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); }