Get Gaze Data

clear;

Tobii = EyeTrackingOperations();

eyetracker_address = 'Address of the desired device';
% Example:
% eyetracker_address = 'tet-tcp://10.46.32.51';

try
    eyetracker = Tobii.get_eyetracker(eyetracker_address);
catch ME
    if (strcmp(ME.identifier,'EyeTrackerGet:error204'))
        fprintf('Unable to connect eye tracker.\n');
        return
    end
end

% The first call subscribes to the stream and returns either data
% (might be empty if no data has been received yet) or any error that
% happened during the subscription.

eyetracker.get_gaze_data();
pause(1);
result = eyetracker.get_gaze_data();

if isa(result,'StreamError')
    fprintf('Error: %s\n',string(result.Error.value));
    fprintf('Source: %s\n',string(result.Source.value));
    fprintf('SystemTimeStamp: %d\n',result.SystemTimeStamp);
    fprintf('Message: %s\n',result.Message);

elseif isa(result,'GazeData')

    % Collect data for 1 seconds.
    pause(1);

    % The subsequent calls return the current values in the stream buffer.
    % If a flat structure is prefered just use an extra input 'flat'.
    % i.e. gaze_data = eyetracker.get_gaze_data('flat');
    gaze_data = eyetracker.get_gaze_data();

    eyetracker.stop_gaze_data();

    fprintf('Collected %d data points\n',size(gaze_data,2));

    % To select the most recent data point simply select the last value of the
    % buffer.
    latest_gaze_data = gaze_data(end);

    fprintf('SystemRequestTimeStamp: %d\n',latest_gaze_data.SystemTimeStamp);
    fprintf('DeviceTimeStamp: %d\n',latest_gaze_data.DeviceTimeStamp);

    disp('Left Eye');

    fprintf('GazePoint.OnDisplayArea: %.2f %.2f\n',latest_gaze_data.LeftEye.GazePoint.OnDisplayArea);
    fprintf('GazePoint.InUserCoordinateSystem: %.2f %.2f %.2f\n',latest_gaze_data.LeftEye.GazePoint.InUserCoordinateSystem);
    fprintf('GazePoint.Validity: %d\n',latest_gaze_data.LeftEye.GazePoint.Validity.value);

    fprintf('GazeOrigin.InUserCoordinateSystem: %.2f %.2f %.2f\n',latest_gaze_data.LeftEye.GazeOrigin.InUserCoordinateSystem);
    fprintf('GazeOrigin.InTrackBoxCoordinateSystem: %.2f %.2f %.2f\n',latest_gaze_data.LeftEye.GazeOrigin.InTrackBoxCoordinateSystem);
    fprintf('GazeOrigin.Validity: %d\n',latest_gaze_data.LeftEye.GazeOrigin.Validity.value);

    fprintf('Pupil.Diameter: %.2f\n',latest_gaze_data.LeftEye.Pupil.Diameter);
    fprintf('Pupil.Validity: %d\n',latest_gaze_data.LeftEye.Pupil.Validity.value);

    disp('Right Eye');

    fprintf('GazePoint.OnDisplayArea: %.2f %.2f\n',latest_gaze_data.RightEye.GazePoint.OnDisplayArea);
    fprintf('GazePoint.InUserCoordinateSystem: %.2f %.2f %.2f\n',latest_gaze_data.RightEye.GazePoint.InUserCoordinateSystem);
    fprintf('GazePoint.Validity: %d\n',latest_gaze_data.RightEye.GazePoint.Validity.value);

    fprintf('GazeOrigin.InUserCoordinateSystem: %.2f %.2f %.2f\n',latest_gaze_data.RightEye.GazeOrigin.InUserCoordinateSystem);
    fprintf('GazeOrigin.InTrackBoxCoordinateSystem: %.2f %.2f %.2f\n',latest_gaze_data.RightEye.GazeOrigin.InTrackBoxCoordinateSystem);
    fprintf('GazeOrigin.Validity: %d\n',latest_gaze_data.RightEye.GazeOrigin.Validity.value);

    fprintf('Pupil.Diameter: %.2f\n',latest_gaze_data.RightEye.Pupil.Diameter);
    fprintf('Pupil.Validity: %d\n',latest_gaze_data.RightEye.Pupil.Validity.value);
end