function  record = do_PLE_trial(P,orient,diff,backgr,pos)

%DO_PLE_TRIAL -- Project a stimulus and get response from the subject.
%
%  record = do_PLE_trial(P,orient,diff,backgr,pos)
%
%  DO_PLE_TRIAL performs a single trial of the Perceptual Learning Exp1.
%  It assumes all screens and sound channels are already open.
%
%  P is a "master parameter structure" specifying the Gabor patches,
%  movieStructs, sounds, and other parameters.
%  The following fields are used by MAKE_PLE_STIM: 'Gabor','Filter',
%  'noise_params','LUT_params','colors','noise_Cpeak', and 'mask_idx'.
%  In addition, the following fields used by DO_PLE_TRIAL itself:
%  'movieStruct','fix_buffPtr','blank','times', and 'sounds'.
%  ORIENT, DIFF, and BACKGR are passed to MAKE_PLE_STIM.
%  POS is an index in P.position and P.movieStruct specifying the
%  position at which the stimulus will appear on the screen.
%
%  RECORD is a row vector of the following nine numbers:
%   1. time stamp -- GetSecs just before the main stimulus presentation
%   2. background noise orientation -- a copy of BACKGR
%   3. presentation position -- a copy of POS
%   4. difficulty level -- a copy of DIFF
%   5. stimulus orientation -- a copy of ORIENT
%   6. response key pressed by the subject (see P.keys and GET_RESPONSE)
%   7. correct -- 1 iff RESPONSE==ORIENT
%   8. response time -- from the stimulus onset until the keypress, msec
%   9. exit code -- 0 for successful trials, 1 for invalid keys, 2 for
%           invalid presentation duration (due to missed video frames)
%  10. play time -- as returned by PLAYMOVIESTRUCT, in seconds.
%
%  See also PILOT2, MAKE_PLE_STIM.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2002/01/02 17:00 $

%-- Generate an appropriate stimulus
stim = make_PLE_stim(P,orient,diff,backgr) ;

%-- Load it into the appropriate offscreen buffer
mvStruct = P.movieStruct(pos) ;
winPtr = mvStruct.windowPtr ;
LoadMovieStruct({stim,P.blank},mvStruct) ;

%-- Get ready for the stimulus presentation
SCREEN(winPtr,'PutImage',P.fixation) ;   % crosshair
WaitSecs(P.times.fix2alert) ;
SND('Play',P.sounds.alert);
WaitSecs(P.times.alert_onset) ;

%-- Present the main stimulus for a strictly controlled amount of time
[startTime,finishTime] = PlayMovieStruct(mvStruct) ;

%-- Wait for response from the subject and give auditory feedback
[resp,keySecs] = get_response(P.keys) ;
correct = (resp==orient) ;
if (~correct)
   SND('Play',P.sounds.wrong) ;
   WaitSecs(P.times.wrong_delay) ;
end

%-- Get rid of the fixation crosshair (and any other leftovers)
SCREEN(winPtr,'FillRect',P.colors.gray) ;

%-- Verify that the stimulus duration really was as it should
play_time = finishTime - startTime ;
target_play = P.times.target_play ;
if (abs(play_time-target_play)/target_play > 0.005)  % 0.5% tolerance
   exitcode = 2 ;    % missed video frame
elseif (resp<1)
   exitcode = 1 ;    % illegal key
else
   exitcode = 0 ;    % OK
end

%-- Pack results and return RECORD
record = zeros(1,9) ;
record(1) = startTime ;     % absolute time stamp of stimulus onset, sec
record(2) = backgr ;
record(3) = pos ;
record(4) = diff ;
record(5) = orient ;
record(6) = resp ;
record(7) = correct ;
record(8) = (keySecs-startTime) * 1000 ;     % response time, msec
record(9) = exitcode ;
record(10)= play_time ;

%%%  Return RECORD
%%%%% End of file DO_PLE_TRIAL.M
