%Holistic Exp
clear all
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Initialize NIDAQ 6503 Digital IO
% dio = digitalio('nidaq',1);
% hwlines = addline(dio,0,'in');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%

TR=3;
epochLength=18;
TrialDuration=0.50; %second
ITI=1;
nRepeat=6; %6
initWait=9; %second
testCond=4;
 
%prepare stimuli
if(testCond==1)
    stiDir1='D:\fMRI_Exp\CharLocalizer\WordSti';
    stiDir2='D:\fMRI_Exp\CharLocalizer\WordScr';  
elseif(testCond==2)
    stiDir1='D:\fMRI_Exp\CharLocalizer\WordSti'; 
    stiDir2='D:\fMRI_Exp\CharLocalizer\NonWordSti'; 
elseif(testCond==3)
    stiDir1='D:\fMRI_Exp\CharLocalizer\FaceSti'; 
    stiDir2='D:\fMRI_Exp\CharLocalizer\FaceScr'; 
elseif(testCond==4)
    stiDir1='D:\fMRI_Exp\CharLocalizer\FamiliarObj';
    stiDir2='D:\fMRI_Exp\CharLocalizer\ObjScr';  
end    
    

cd(stiDir1)
dir1=dir;
fLength1=length(dir1);
for i = 3:fLength1
    tmp=imread([dir1(i).name],'bmp');
    stiSet1(:,:,i-2)=tmp;
end    

cd(stiDir2)
dir2=dir;
fLength2=length(dir2);
for i = 3:fLength2
    tmp=imread([dir2(i).name],'bmp');
    stiSet2(:,:,i-2)=tmp;
end    

fLength1=fLength1-2;
fLength2=fLength2-2;

% Eye Catching
%create Lookup Tables
lut1=[0:1:255;0:1:255;0:1:255]';
lut1(1,:)=[255 0 0];
% eye catching
[win1 rect]=SCREEN(0,'OpenWindow',128);
HideCursor
centerx=rect(3)/2+1;
centery=rect(4)/2+1;

SCREEN(win1,'TextFont','arial');
SCREEN(win1,'TextSize',14);

SCREEN(win1,'DrawText','Ready',centerx-15,centery,255);
screen(win1,'SetClut',lut1);
imgNo=ceil(rand(1,1)*fLength1);
pattern=stiSet1(:,:,imgNo);

% %EPITrigger
% flag=getvalue(dio);while (getvalue(dio)==flag); end;

%t0=getsecs;

t0=kbwait;
% Burning epoch
while(getsecs-t0<initWait)
    t1=getsecs;
    screen(win1,'PutImage',pattern);
    SCREEN(win1,'DrawOval',0,[centerx-2,centery-2,centerx+2,centery+2]);
   
    %prepare next trial
    duplicate=coinflip(1,0.1);
    if(duplicate)
        imgNo=imgNo;
    else
        imgNo=ceil(rand(1,1)*fLength1);
    end
    pattern=stiSet1(:,:,imgNo);
   
    while(getsecs-t1<TrialDuration)
    end 
    %screen(win1,'PutImage',ones(size(pattern)));
    screen(win1,'PutImage',ones(size(pattern))*128);
    while(getsecs-t1<ITI)
    end  

    
end
getsecs-t0
%
t0=getsecs;
for i = 1:nRepeat
    counter=1;
    while(getsecs-t0<(2*i-1)*epochLength)
        t1=getsecs;
        screen(win1,'PutImage',pattern);
        SCREEN(win1,'DrawOval',0,[centerx-2,centery-2,centerx+2,centery+2]);
        % prepare next trial
        duplicate=coinflip(1,0.1);
        if(duplicate)
            imgNo=imgNo;
        else
            imgNo=ceil(rand(1,1)*fLength1);
        end
        if(counter>18)
            pattern=stiSet2(:,:,imgNo);    
        else
            pattern=stiSet1(:,:,imgNo);
        end    
        counter=counter+1;
        while(getsecs-t1<TrialDuration)
        end
        screen(win1,'PutImage',ones(size(pattern))*128);
        while(getsecs-t1<ITI)
        end  

    end
    
    counter=1;
    while(getsecs-t0<2*i*epochLength)
        t1=getsecs;
        screen(win1,'PutImage',pattern);
         SCREEN(win1,'DrawOval',0,[centerx-2,centery-2,centerx+2,centery+2]);
       
%         % prepare next trial
        duplicate=coinflip(1,0.1);
        if(duplicate)
            imgNo=imgNo;
        else
            imgNo=ceil(rand(1,1)*fLength2);
        end
       if(counter>18)
            pattern=stiSet1(:,:,imgNo);    
        else
            pattern=stiSet2(:,:,imgNo);
        end    
        counter=counter+1;
       
        while(getsecs-t1<TrialDuration)
        end    
        screen(win1,'PutImage',ones(size(pattern))*128);
        while(getsecs-t1<ITI)
        end  

    end    
end
 getsecs-t0   
screen(win1,'PutImage',ones(size(pattern))*128);
SCREEN(win1,'DrawText','Done!',centerx-15,centery,255);

screen(win1,'Close');
    

% Clean up DIO object
delete dio; clear flag dio hwlines


