Get Tine Synchronization 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.
try
    eyetracker.get_time_sync_data();
catch ME
    fprintf(ME.message);
    return
end
pause(1);
result = eyetracker.get_time_sync_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,'TimeSynchronizationReference')
    % 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. time_sync_data = eyetracker.get_time_sync_data('flat');
    time_sync_data = eyetracker.get_time_sync_data();

    eyetracker.stop_time_sync_data();

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

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

    fprintf('SystemRequestTimeStamp: %d\n',latest_time_sync_data.SystemRequestTimeStamp);
    fprintf('DeviceTimeStamp: %d\n',latest_time_sync_data.DeviceTimeStamp);
    fprintf('SystemResponseTimeStamp: %d\n',latest_time_sync_data.SystemResponseTimeStamp);
end