Tobii Pro SDK C API
calibrating_with_gaze.c
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.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
bool user_looking_at_screen = false;
void gaze_callback(TobiiResearchGazeData *gaze_data, void *user_data)
{
(void)(user_data);
/* Check if gaze is in the display area so that we can collect calibration data
once the user is safely looking at the screen. */
if (user_looking_at_screen)
{
return;
}
bool left_eye_on_screen = false;
bool right_eye_on_screen = false;
if (gaze_data->left_eye.gaze_point.position_on_display_area.x >= 0.0f &&
{
left_eye_on_screen = true;
}
if (gaze_data->right_eye.gaze_point.position_on_display_area.x >= 0.0f &&
{
right_eye_on_screen = true;
}
if (left_eye_on_screen && right_eye_on_screen)
{
user_looking_at_screen = true;
}
printf("Left eye gaze point: (%f, %f)\nRight eye gaze point: (%f, %f)\n",
}
void calibrating_with_gaze_example(TobiiResearchEyeTracker *eyetracker)
{
/* Subscribe to gaze before starting the calibration and unsubscribe after calibration is done.
Subscribing and unsubscribing to gaze during the calibration flow will result in undefined
behavior and should be avoided.
*/
TobiiResearchStatus status = tobii_research_subscribe_to_gaze_data(eyetracker, gaze_callback, NULL);
if (status != TOBII_RESEARCH_STATUS_OK)
{
printf("Failed while subscribing to gaze %d", status);
}
/* Enter calibration mode. */
char *serial_number = NULL;
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. */
TobiiResearchNormalizedPoint2D point_to_calibrate = {0.5f, 0.5f};
printf("Show a point on screen at (%f,%f).\n", point_to_calibrate.x, point_to_calibrate.y);
printf("Wait for user to look at the screen until we gather calibration data.\n");
while (!user_looking_at_screen)
{
// Wait a little bit for the user to focus on the dot until we try again.
sleep_ms(100);
}
printf("Collecting data at (%f,%f).\n", point_to_calibrate.x, point_to_calibrate.y);
if (tobii_research_screen_based_calibration_collect_data(eyetracker, point_to_calibrate.x, point_to_calibrate.y) != TOBII_RESEARCH_STATUS_OK)
{
/* 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. */
tobii_research_screen_based_calibration_collect_data(eyetracker, point_to_calibrate.x, point_to_calibrate.y);
}
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 */
/* The calibration is done. Leave calibration mode. */
if (status != TOBII_RESEARCH_STATUS_OK)
{
printf("Failed leaving calibration mode %d.\n", status);
}
else
{
printf("Left calibration mode.\n");
}
/* Unsubscribe to gaze after calibration is done.*/
status = tobii_research_unsubscribe_from_gaze_data(eyetracker, gaze_callback);
if (status != TOBII_RESEARCH_STATUS_OK)
{
printf("Failed unsubscribing to gaze %d.\n", status);
}
else
{
printf("Unsubscribed to gaze.\n");
}
}
Definition: tobii_research_calibration.h:139
size_t calibration_point_count
Definition: tobii_research_calibration.h:147
TobiiResearchCalibrationStatus status
Definition: tobii_research_calibration.h:151
TobiiResearchGazePoint gaze_point
Definition: tobii_research_streams.h:96
Definition: tobii_research_streams.h:112
TobiiResearchEyeData right_eye
Definition: tobii_research_streams.h:121
TobiiResearchEyeData left_eye
Definition: tobii_research_streams.h:116
TobiiResearchNormalizedPoint2D position_on_display_area
Definition: tobii_research_streams.h:76
Definition: tobii_research.h:384
float x
Definition: tobii_research.h:388
float y
Definition: tobii_research.h:392
TOBII_RESEARCH_API void TOBII_RESEARCH_CALL tobii_research_free_string(char *str)
Free memory allocation for a string allocated by the SDK.
struct TobiiResearchEyeTracker TobiiResearchEyeTracker
Definition: tobii_research.h:315
TobiiResearchStatus
Definition: tobii_research.h:45
@ TOBII_RESEARCH_STATUS_OK
Definition: tobii_research.h:49
Functionality for implementing calibration.
TOBII_RESEARCH_API TobiiResearchStatus TOBII_RESEARCH_CALL tobii_research_screen_based_calibration_collect_data(TobiiResearchEyeTracker *eyetracker, float x, float y)
Starts collecting data for a calibration point.
@ TOBII_RESEARCH_CALIBRATION_SUCCESS
Definition: tobii_research_calibration.h:34
TOBII_RESEARCH_API TobiiResearchStatus TOBII_RESEARCH_CALL tobii_research_screen_based_calibration_enter_calibration_mode(TobiiResearchEyeTracker *eyetracker)
Enters the screen based calibration mode and the eye tracker is made ready for collecting data and ca...
TOBII_RESEARCH_API TobiiResearchStatus TOBII_RESEARCH_CALL tobii_research_screen_based_calibration_compute_and_apply(TobiiResearchEyeTracker *eyetracker, TobiiResearchCalibrationResult **result)
Uses the collected data and tries to compute calibration parameters.
TOBII_RESEARCH_API TobiiResearchStatus TOBII_RESEARCH_CALL tobii_research_screen_based_calibration_leave_calibration_mode(TobiiResearchEyeTracker *eyetracker)
Leaves the screen based calibration mode.
TOBII_RESEARCH_API void TOBII_RESEARCH_CALL tobii_research_free_screen_based_calibration_result(TobiiResearchCalibrationResult *result)
Free memory allocation for the calibration result received via tobii_research_screen_based_calibratio...
Functionality for an eye tracker.
TOBII_RESEARCH_API TobiiResearchStatus TOBII_RESEARCH_CALL tobii_research_get_serial_number(TobiiResearchEyeTracker *eyetracker, char **serial_number)
Gets the serial number of the eye tracker. All physical eye trackers have a unique serial number.
Functionality for eye tracker streams.
TOBII_RESEARCH_API TobiiResearchStatus TOBII_RESEARCH_CALL tobii_research_unsubscribe_from_gaze_data(TobiiResearchEyeTracker *eyetracker, tobii_research_gaze_data_callback callback)
Unsubscribes from gaze data for the eye tracker.
TOBII_RESEARCH_API TobiiResearchStatus TOBII_RESEARCH_CALL tobii_research_subscribe_to_gaze_data(TobiiResearchEyeTracker *eyetracker, tobii_research_gaze_data_callback callback, void *user_data)
Subscribes to gaze data for the eye tracker.