function buttons = GetButtons;

% function buttons = GetButtons
%
%	GetButtons is part of the VVRC Matlab interface to PST's system for
%	functional MRI.  GetButtons returns in one variable the position of subject
%	response buttons on the left and right PST "Hand Pucks."  The returned value
%   is a ten-element logical array.  A set value in that logical array 
%	indicates that the button is depressed.  A zero value in the array indicates 
%	that the button is happy.  Elements in the logical array correspond to 
%   fingers in this way:
%
%	buttons(1)  right-hand thumb    F6
% 	buttons(2)  right-hand pointer  F7	
%	buttons(3)  right-hand middle   F8
%	buttons(4)  right-hand ring     F9
%	buttons(5)  right-hand pinky    F10
%
%	buttons(6)  left-hand thumb     F5
% 	buttons(7)  left-hand pointer   F4	
%	buttons(8)  left-hand middle    F3
%	buttons(9)  left-hand ring      F2
%	buttons(10) left-hand pinky     F1
%
%	Be sure to call the InitializeFMRI function once before using GetButtons.  If you 
%   call InitializeFMRI with the optional simulation parameter set, then GetButtons 
%   ignores the state of any external response buttons, present or not, and returns instead
%   The state of the function keys.  This feature is offered for purposes of testing 
%   stimulus scripts outside of the MRI lab.  The mapping of function keys to return values
%   and fingers is also illustrated in the table above.  This key mapping works for the standard
%   full-size apple ADB keyboard.
% 
%  
% 	Examples:
%	
%	To check whether particular buttons are pressed:
%
%	theReponse = GetButtons;
%	if (theResponse(1) and theResponse(6)) 
%		fprintf(1,'The subject is pushing both thumb buttons');
%	end 
%
%	To find all the buttons wich are being pressed:
%
%	theResponse = GetButtons;
%   theButtons = find(theButtons);
%	fprintf(['subject is pressing button numbers int2str(theButtons) '\n' ]);
%
%	See Also: % See Also: KbName, InitializeFMRI, WaitScanner, GetButtons, ButtonName.
%
%
%	9/01/99	awi wrote it.
%   1/27/00 awi modified for simulation mode
%   1/31/00 awi returned results in a single value
%   4/10/00 awi minor edits to help

global GLOBAL_NI_DIO_SLOT_NUMBER;

gVariables = who('global');
if ~ismember('GLOBAL_NI_DIO_SLOT_NUMBER', gVariables);
	error('call function InitializeFMRI before using GetButtons');
end %if
if isempty(GLOBAL_NI_DIO_SLOT_NUMBER);
	error('call function InitializeFMRI before using GetButtons');
end 

if all('sim' == GLOBAL_NI_DIO_SLOT_NUMBER) %simulate buttons with F keys
	[KeyDown, Secs, keyCode] = KbCheck;
	leftHand = zeros(1,5);
	rightHand = zeros(1,5);
	logical(leftHand);
	logical(rightHand);
	%vectors of function key numbers indexed by corresponding position in return vector.
	lhm = [6 7 8 9 10 5 4 3 2 1];
	for i = 1:10
		buttons(i) = keyCode(KbName(['f' int2str(lhm(i))]));
	end %for i
else %get the real buttons, not the F keys.
	PortA = DIG_In_Port(GLOBAL_NI_DIO_SLOT_NUMBER, 0);
	PortB = DIG_In_Port(GLOBAL_NI_DIO_SLOT_NUMBER, 1);
	leftHand = zeros(1,5);
	logical(leftHand);
	rightHand = zeros(1,5);
	logical(rightHand);
	twoPowers = 2.^[0:7]; 
	for i = 1:5
		leftHand(i) = ~bitand(PortB, twoPowers(i));
		rightHand(i) = ~bitand(PortA, twoPowers(i));
	end %for i
	buttons = [rightHand leftHand]; 
end %else
