function InitializeFMRI(simMode)

%	function InitializeFMRI([simMode])
%
%	InitializeFMRI is part of the VVRC Matlab interface to PST's system for
%	functional MRI.  InitializeFMRI sets up the National Instruments 
%	'PCI-DIO-96' card for communication.  Be sure to call InitializeFMRI
%	once before using the GetButtons and WaitScanner functions.  
%
%   You might want to develop scripts for FMRI on a computer not equipped with the National 
%   Instruments card.  If you pass IntiializeFMRI an optional value which evaluates true,
%   then subsequent calls to GetButtons will return the state of the keyboard function keys 
%   instead of the state of the external hand pucks. (See help for GetButtons for
%   correspondence of function keys to hand puck buttons).  Simulation mode also forces 
%   WaitScanner to initiate on a keypress. 
%
%   IntializeFMRI finds the number of the slot holding the DAQCard-DIO-2 card and 
%   stores it in the global variable GLOBAL_NI_DIO_SLOT_NUMBER.  Therefore, if you 
%	clear that variable after calling InitializeFMRI you must call InitializeFMRI
%	once again before using the GetButtons and WaitScanner functions.  
%
%	See Also: GetButtons, WaitScanner.
%
%	9/01/99	awi wrote it.
%	1/27/00 awi added optional simMode argument
%   2/06/00 awi fixed bug when recalling this function.
%   3/31/00 awi now searchs for card in first six slots, warns when card is not found.

global GLOBAL_NI_DIO_SLOT_NUMBER;

io_cardname = 'PCI-DIO-96';
search_slots = 1:6;

GLOBAL_NI_DIO_SLOT_NUMBER = 'x';

if (nargin == 1) 
	if simMode
		GLOBAL_NI_DIO_SLOT_NUMBER = 'sim';
	end %if
end %if

if ~all('sim' == GLOBAL_NI_DIO_SLOT_NUMBER)
	%find the card
	GLOBAL_NI_DIO_SLOT_NUMBER = FindCard(io_cardname,search_slots);
	if isempty(GLOBAL_NI_DIO_SLOT_NUMBER) 
		error([io_cardname ' card not found in specified slots: ' int2str(search_slots) ]);
	end %if
	%configure the ports
	DIG_Prt_Config(GLOBAL_NI_DIO_SLOT_NUMBER, 0, 0,0);	%configure 8255 port A to be input, no handshake
	DIG_Prt_Config(GLOBAL_NI_DIO_SLOT_NUMBER, 1, 0,0);	%configure 8255 port B to be input, no handshake
	DIG_Prt_Config(GLOBAL_NI_DIO_SLOT_NUMBER, 2, 0,0);	%configure 8255 portC to be input, no handshake
end %if





