Tobii Pro SDK C API
calibration.c
#include <stdio.h>
#if _WIN32 || _WIN64
#include <windows.h>
static void sleep_ms(int time) {
Sleep(time);
}
#else
#include <unistd.h>
static void sleep_ms(int time) {
usleep(time * 1000);
}
#endif
void calibration_example(TobiiResearchEyeTracker* eyetracker) {
/* Enter calibration mode. */
char* serial_number;
tobii_research_get_serial_number(eyetracker, &serial_number);
printf("Entered calibration mode for eye tracker with serial number %s \n.", serial_number);
/* Define the points on screen we should calibrate at. */
/* The coordinates are normalized, i.e. (0.0, 0.0) is the upper left corner and (1.0, 1.0) is the lower right corner. */
{
#define NUM_OF_POINTS 5U
TobiiResearchNormalizedPoint2D points_to_calibrate[NUM_OF_POINTS] = \
{{0.5f, 0.5f}, {0.1f, 0.1f}, {0.1f, 0.9f}, {0.9f, 0.1f}, {0.9f, 0.9f}};
size_t i = 0;
for(; i < NUM_OF_POINTS; i++) {
TobiiResearchNormalizedPoint2D* point = &points_to_calibrate[i];
printf("Show a point on screen at (%f,%f).\n", point->x, point->y);
// Wait a little for user to focus.
sleep_ms(700);
printf("Collecting data at (%f,%f).\n", point->x, point->y);
/* Try again if it didn't go well the first time. */
/* Not all eye tracker models will fail at this point, but instead fail on ComputeAndApply. */
}
}
printf("Computing and applying calibration.\n");
TobiiResearchCalibrationResult* calibration_result = NULL;
status = tobii_research_screen_based_calibration_compute_and_apply(eyetracker, &calibration_result);
if (status == TOBII_RESEARCH_STATUS_OK && calibration_result->status == TOBII_RESEARCH_CALIBRATION_SUCCESS) {
printf("Compute and apply returned %i and collected at %zu points.\n", status, calibration_result->calibration_point_count);
} else {
printf("Calibration failed!\n");
}
/* Free calibration result when done using it */
/* Analyze the data and maybe remove points that weren't good. */
TobiiResearchNormalizedPoint2D* recalibrate_point = &points_to_calibrate[1];
printf("Removing calibration point at (%f,%f).\n", recalibrate_point->x, recalibrate_point->y);
status = tobii_research_screen_based_calibration_discard_data(eyetracker, recalibrate_point->x, recalibrate_point->y);
/* Redo collection at the discarded point */
printf("Show a point on screen at (%f,%f).\n", recalibrate_point->x, recalibrate_point->y);
tobii_research_screen_based_calibration_collect_data(eyetracker, recalibrate_point->x, recalibrate_point->y);
/* Compute and apply again. */
printf("Computing and applying calibration.\n");
status = tobii_research_screen_based_calibration_compute_and_apply(eyetracker, &calibration_result);
if (status == TOBII_RESEARCH_STATUS_OK && calibration_result->status == TOBII_RESEARCH_CALIBRATION_SUCCESS) {
printf("Compute and apply returned %i and collected at %zu points.\n", status, calibration_result->calibration_point_count);
} else {
printf("Calibration failed!\n");
}
/* Free calibration result when done using it */
/* See that you're happy with the result. */
}
/* The calibration is done. Leave calibration mode. */
printf("Left calibration mode.\n");
}