% FMRIdemo.m
% Allen Ingling  Allen.Ingling@vanderbilt.edu
%
% This script demonstrates the use of the FMRI interface functions by doing a short experiment.  
% The subject is asked to perform a simple vernier task while inside the scanner.  Tje results, the 
% differences in alignment between bars for each trial, are displayed at the end of the experiment.
%
% If run in simulation mode the subject responds with function keys.  If not in simulation mode 
% the script uses IFIS hand pucks on an external interface.   
%
% Key mappings:
% 
%   left thumb     F5       : moves bar leftward.
%   right thumb    F6      : moves bar rightward.
%   index fingers  F4, F7  : accept alignment, proceed to next trial.
%
%
% See Also: InitializeFMRI, WaitScanner, GetButtons, ButtonName.
%
% history:
% 1/31/00 awi wrote it
% 2/06/00 awi added semicolon


%___________________________________________________________________________________________________
% set experiment parameters
%

stimulus_monitor = 1;  %the device number of monitor on which to display the stimulus.  0 is the 
                       % monitor with the menu bar, 1 is the second monitor, etc.

num_tasks = 3;	       %the number of vernier tasks the subject is asked to perform

right_key = ButtonName('right thumb');  %specify the button which advances bar rightward.
left_key = ButtonName('left thumb');    %specify the button which advance the bar leftward.
accept_keys = [ButtonName('left index'), ButtonName('right index')];

%ask the user if he wants to run in simulation mode
simStr = input('Run in simulation mode[y] ? ','s');
simStr = upper(simStr);
if (strcmp(simStr, 'Y') | strcmp(simStr, 'YES') | isempty(simStr) )
	initializeFMRI(1);
else 
	initializeFMRI;
end %else

%___________________________________________________________________________________________________
% setup displays: open window, set clut, get display measurements
%
screen('CloseAll');
[w, wRect] = screen(stimulus_monitor,'OpenWindow',0,[],8);
wClut = [ [0:255]' [0:255]' [0:255]' ];
screen(w,'SetClut',wClut);
wHeight = RectHeight(wRect);
wWidth = RectWidth(wRect);
wCenterY = round(wHeight/2);
wCenterX = round(wWidth/2);


%___________________________________________________________________________________________________
% create a bit image of vernier target on an offscreen window
%
vernLine = [ 0 0 1 1 0 0];
vernHeight = 20;
vernLine = vernLine * 255;
vernWidth = length(vernLine);
vernMat = repmat(vernLine, vernHeight, 1);
vernRect = [0 0 vernWidth vernHeight];
vernWin = screen(w,'OpenOffscreenWindow', 0, vernRect);
screen(vernWin, 'PutImage', vernMat);
verticalRefOffset = round(vernHeight/2) + 4;


%___________________________________________________________________________________________________
% run experiment
%


%draw the reference vernier
screen('CopyWindow', vernWin, w, vernRect, CenterRectOnPoint(vernRect, wCenterX, wCenterY + verticalRefOffset));
yPos = wCenterY - verticalRefOffset; 
for i = 1:num_tasks
	accept = 0;
	xPos = (round(rand(1) * 50) - 25) + wCenterX;  
	while ~accept
		press = GetButtons;
		if press(right_key) 
			xPos = xPos + 1;
		elseif press(left_key)
			xPos = xPos - 1;
		end %else if
		if (xPos > (wCenterX + 25)), xPos = (wCenterX + 25);, end;
		if (xPos < (wCenterX - 25)), xPos = (wCenterX - 25);, end;
		screen(w,'WaitVBL');
		screen('CopyWindow', vernWin, w, vernRect, CenterRectOnPoint(vernRect, xPos, yPos));
		if any(press(accept_keys))
			accept = 1;
		end %if
	end %while
	results(i) = wCenterX - xPos;
	pause(1);
	screen(w,'FillRect');
	screen('CopyWindow', vernWin, w, vernRect, CenterRectOnPoint(vernRect, wCenterX, wCenterY + verticalRefOffset));
	
end %for 


%___________________________________________________________________________________________________
% clean up
%

flushevents('keyDown');
screen('CloseAll');

fprintf(1,'errors on trials were\n');
fprintf(1,[int2str(results) '\n']);


		
		
	
	
	
