%-------------------------------------------------
%Collect eyetracking data
%
%Samplerate is already 50 Hz
%
% BITSI1 = [1 2 4 8 16 32 64 128];
%    -used for code from Presentation
% BITSI2 = [256 512 1024 2048 4096 8192 16384 32768];
%    -position 1= scantrigger
%    -position 2= Left Index finger
%    -position 3= Left Middle finger
%    -position 4= Right Index finger
%    -position 5= Right Middle finger
%
% LdV2016
%-------------------------------------------------

function [eyetrackdat] = a_geteyetracker(fname,fpath)



%Ssettings
aTR= 1.5; %Set approximate TR (used for scan period definition)
dFs = 500;  %downsampled sample frequency

%if used stand-alone, use GUI to request file
if nargin==0
   [fname,fpath]=uigetfile('*.txt','Get txt file from eyetracker');
   fname=fullfile(fpath,fname);
end
fname = fullfile(fpath,fname);
%load data
[a1,a2,a3,a4,a5,a6]=...
    textread(fname,'%s %s %s %s %s %s');

for i = length(a1):-1:1
    if string(a1(i)) == 'SFIX' || string(a1(i)) == 'EFIX' || string(a1(i)) == 'SBLINK' || string(a1(i)) == 'EBLINK' ||string(a1(i)) == 'SSACC' || string(a1(i)) == 'ESACC' || string(a1(i)) == 'INPUT' || string(a1(i)) == 'END'
        a1(i)=[];
        a2(i)=[];
        a3(i)=[];
        a4(i)=[];
        a5(i)=[];
    end
end

%get pupildialation
diameter=str2num(char(a4(39:end)));%6th collumn is L diameter

%transform codes to 16 bits binary code. !! Not necessary in eyelink data,
%already decimal numbers?
tempdec=str2num(char(a5(39:end)));
tempbin=dec2bin(tempdec,16); 

%collect all scantriggers: 256 in 16 bit binary code will just be a 1 on
%the 8th position
% don't seem to be scantriggers in file so do't need this command?: temptrigs=find(diff((bin2dec(tempbin(:,8))==1))>0)+1;

%put triggers in one variable with 50Hz samplerate
% for i=1:numel(temptrigs)
%     Triggers(round(temptrigs(i)))=1;
% end

%Add zeros at the end
Triggers(numel(Triggers)+1:numel(diameter))=0;

%Define scan periods
% lastTrig = -inf;
% %TRrange = [aTR-aTR/100,aTR+aTR/100];
% TRrange = [aTR-aTR/70,aTR+aTR/70]; %NEW VALUES
% cScanBlock=0;
% nScanBlocks=0;
% BlockStarts=[];
% BlockEnds=[];
% for i=1:numel(temptrigs)
%     
%     cInterval = ((temptrigs(i)-lastTrig)/dFs);
%     if (cInterval>TRrange(1) && cInterval<TRrange(2)) || ...
%             (cInterval>2*TRrange(1) && cInterval<2*TRrange(2))
%         if cScanBlock == 0
%             nScanBlocks=nScanBlocks+1;
%             cScanBlock=nScanBlocks;
%             BlockStarts(cScanBlock) = lastTrig;
%         end
%         
%     else
%         if cScanBlock>0
%             BlockEnds(cScanBlock) = lastTrig;
%             cScanBlock=0;
%         end
%     end
%     lastTrig=temptrigs(i);
% end
% if cScanBlock>0
%     BlockEnds(cScanBlock) = lastTrig;
% end

%collect all presentation codes: Collects all stim codes below 128
%prescodes=bin2dec(tempbin(:,9:16));
prescodes=tempdec;

%make other struct in order of task
% for c_task=1:numel(BlockStarts)
%     eyetrackdat.start=BlockStarts;
%     eyetrackdat.end=BlockEnds;
%     eyetrackdat.trignum{c_task}=sum(Triggers(BlockStarts(c_task)-1:...
%         BlockEnds(c_task)+1));  
%     eyetrackdat.prescodes{c_task}=prescodes(BlockStarts(c_task)-1:BlockEnds(c_task)+1); 
%     eyetrackdat.triggers{c_task}=Triggers(BlockStarts(c_task)-1:BlockEnds(c_task)+1)';
%     eyetrackdat.diameter{c_task}=diameter(BlockStarts(c_task)-1:BlockEnds(c_task)+1);
% end

eyetrackdat.prescodes=prescodes;
eyetrackdat.diameter=diameter;

%save traskdat
save(fullfile(fpath,'eyetrackdat.mat'),'eyetrackdat');