% VWF_word.m 12,17,2003
% word pseudoword nonword
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% EPI
%%% TR/TE/@ :3000/35/90
%%% FOV= 240*240
%%% voxel size: 3*3*5mm
%%% whole brain slices=21 slices 
%%% in-plane resolution=3mm*3mm 
%%% repetition= 72+3
%%% 3D high resolution anatomy scan(T1-weighted)
%%% 12blocks*12trials*1.5sec+6*1.5=216+9=225sec
%%% 640*480
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% prepare all stimuli %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computer parameters
pixelSize=0.0353; %degree/pixel
LCDframeRate = 60; %Hz
cd f:\FMRI\VWF_exp % current path

% general present parameters 
ITI=1.50; %second , the duration between stimuli
dur=1.00; %second the duration of one stimuli
nInitWait=9; %second initial waiting time (put some stimuli to get subjects's stable state) 
nepoch=6; % %  the number of 'on' or 'off' conditions
ntrials=12; % each condition involve __ trials 12

% get sequence
[NULL, seti]=sort(rand(6,1)); %seq of initwait sti
seti=mod(seti,6)+1; 

[NULL, seq]=sort(rand(72,1)); % order random all trials of on condition
set=mod(seq,72)+1; % each condition include 15 trials

%posit=mod(seq,15)+1; % mod(x,y)= modulus after division
[NULL,setc]=sort(rand(72,1)); % order random all trials of off condition
setc=mod(setc,72)+1; 
% read data
cd f:\FMRI\VWF_exp\VWFstimuli

% sti of initwait 
for h = 1:6
     fname=['inNW','0',num2str(h),'.bmp'];
     temp=imread(fname,'bmp');
     initSti(:,:,h)=255-double(temp(:,:,1));
   end
   
% sti of 'on' condition
for i = 1:72
      if(i<10)
         fname=['PW1','0',num2str(i),'.bmp'];
      else   
         fname=['PW1',num2str(i),'.bmp'];
      end
      temp=imread(fname,'bmp');
      testSti(:,:,i)=255-double(temp(:,:,1));
    end

% sti of 'off' condition
for j = 1:72
      if(j<10)
         fname=['ScrP1','0',num2str(j),'.bmp'];
      else   
         fname=['ScrP1',num2str(j),'.bmp'];
      end
      temp=imread(fname,'bmp');
      conTest(:,:,j)=255-double(temp(:,:,1));
  end
   
bgImage=zeros(size(temp(:,:,1)));   
clut=kron([1 1 1],[0:255]');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%% Run Experiment  %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Eye Catching 
[win1 rect]=SCREEN(0,'OpenWindow',0); % [windowPtr,rect]=SCREEN(windowPtrOrScreenNumber,'OpenWindow',[color],[rect],[pixelSize]);
HideCursor % hide mouse 
centerx=rect(3)/2+1; % put image on the screen center
centery=rect(4)/2+1; 

SCREEN(win1,'TextFont','arial'); % oldStyle=SCREEN(windowPtr,'TextStyle',[style])
SCREEN(win1,'TextSize',14); % oldFontSize=SCREEN(windowPtr,'TextSize',[fontSize])
SCREEN(win1,'DrawText','Ready',centerx-20,centery,255); % write 'ready' on the center [newX,newY] = SCREEN(windowPtr,'DrawText',text,[x],[y],[color])


%t0=getsecs; % start to get time

t0=kbwait; % demo use Waits until any key is down and returns the time (GetSecs)
screen(win1,'PutImage',bgImage);   
screen(win1,'FillOval',255,...
[centerx-2,centery-2,centerx+2,centery+2]); % draw a fixation 

% initWait
t1=getsecs; % start to get time
for i = 1:6 %  initwait usually 2-3TR (start of each run)
   h=i;
   t2=getsecs;
   testImage=initSti(:,:,seti(h)); 
   screen(win1,'PutImage',testImage); 
   screen(win1,'FillOval',255,[centerx-2,centery-2,centerx+2,centery+2]);
   while(getsecs-t2 < dur)
   end
   screen(win1,'PutImage',bgImage);
   screen(win1,'FillOval',255,[centerx-2,centery-2,centerx+2,centery+2]);
    while(getsecs-t2 < ITI) 
    end
end 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%% start the Experiment  %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%tic   % start to count time
for i = 1:nepoch  % how many 'on' or 'off' conditions 
   % test(on) condition  
   for j =1:ntrials % trilas of each 'on' or 'off' conditions 

      k=(i-1)*ntrials+j;      
      t2=getsecs;
      testImage=testSti(:,:,set(k));
      screen(win1,'PutImage',testImage); 
      screen(win1,'FillOval',255,[centerx-2,centery-2,centerx+2,centery+2]);
      while(getsecs-t2 < dur)
      end  
      screen(win1,'PutImage',bgImage);  %bgimage=background image
      screen(win1,'FillOval',255,[centerx-2,centery-2,centerx+2,centery+2]);
      while(getsecs-t2 < ITI)
      end      
   end

   %control(off) conditions
   for j =1:ntrials

      k=(i-1)*ntrials+j;
            t2=getsecs;
      testImage=conTest(:,:,setc(k));
      screen(win1,'PutImage',testImage); 
      screen(win1,'FillOval',255,[centerx-2,centery-2,centerx+2,centery+2]);
      while(getsecs-t2 < dur)
      end 
      screen(win1,'PutImage',bgImage); 
      screen(win1,'FillOval',255,[centerx-2,centery-2,centerx+2,centery+2]);
      while(getsecs-t2 < ITI)
      end
   end		
%toc   % end to count time
end   

getsecs-t0
getsecs-t1
%Clean up
screen(win1,'PutImage',ones(size(bgImage))*0);
%SCREEN(win1,'DrawText','Ready',centerx-15,centery,255);

screen('closeAll');
clear all

