function dlxexp6v2

% DLXEXP6  Run dyslexia experiment 6.
%
% Script written by Stephen Wilson <stephenw@ucla.edu>, 2002-03.
% version 2 with colored bar 02/11/05 SW

% variables
soundvolume = 1; % you can change the sound volume here
bbox = 0; % set this to 1 if there's a button box, 0 if there isn't

% specific initialization
numpractice = 4;
numtargets = 12;
numtotal = numpractice + numtargets;
thispath = which('dlxexp6v2');
[pathstr name ext versn] = fileparts(thispath);
cd(pathstr);

% list = textread('dlxexp6-list.txt', '%s', 'delimiter', '\n', 'whitespace', '');
list = mytextread('dlxexp6-list.txt'); % Matlab version 5 does not have textread

targetorder = [1:numpractice randperm(numtargets) + 4];
targetreps = [1 1 1 1 2 2 2 2 3 3 3 3];
targetreps = targetreps(randperm(length(targetreps)));
targetreps = [2 3 1 2 targetreps];
trialnum = 0;

snd('Open');
rand('state', sum(100*clock));

if bbox
    port = psychserial('Open', '.Bin', '.Bout', 19200);	
    a0 = psychserial('Read', port, 0.020); % I don't know what this does
else
    port = 0;
end

% generic initialization
outfid = dlxexpinit('dlxexp6');
if outfid == -1
    return;
end

fprintf(outfid, 'trial\ttarget\tword\trt\tkey\tbbox\tcorr\n');

screen('CloseAll');
grey = 100;
[w, wrect] = screen(0, 'OpenWindow', [grey grey grey], [], 32);
screen_clut = [[0:255]', [0:255]', [0:255]'];
screen(w, 'SetClut', screen_clut);
hidecursor;

imagedim = 300;
gapbetween = 50;
barwidth = 400;
barheight = 100;
imagerect = [wrect(3) / 2 - imagedim / 2, wrect(4) / 2 - (imagedim + gapbetween + barheight) / 2, ...
        wrect(3) / 2 + imagedim / 2, wrect(4) / 2 - (imagedim + gapbetween + barheight) / 2 + imagedim];
outerbarrect = [wrect(3) / 2 - barwidth / 2, wrect(4) / 2 + (imagedim + gapbetween + barheight) / 2 - barheight, ...
        wrect(3) / 2 + barwidth / 2, wrect(4) / 2 + (imagedim + gapbetween + barheight) / 2];
greybarrect = insetrect(outerbarrect, 30, 30);
barrect = insetrect(greybarrect, 2, 2); % for actually filling in

accuratewait(3);

% main loop
for target = 1:numtotal
   
    testwords = {};
    rem = list{targetorder(target)};
    while ~isempty(rem)
    [next, rem] = strtok(rem);
        testwords = [testwords {next}];
    end
    targetword = testwords{1};
    testorder = [ones(1, targetreps(target)) 2:length(testwords)];
    numtestwords = length(testorder);
    testorder = testorder(randperm(length(testorder)));
    
    % display picture
    p = imread(fullfile('dlxexp6-pics', [targetword '.bmp']));
    p = double(p);
    p = 255 * (1 - p);
    screen(w, 'PutImage', p, imagerect);
    
    [wav, samprate, bits] = wavread(fullfile('dlxexp6-wavs', [targetword '.wav']));
    wav = wav';
    snd('Play', wav * soundvolume, samprate, bits);
    
    while snd('IsPlaying'), end
    accuratewait(2); % wait 2 seconds from end of target word
    while kbcheck, end
    
    for test = 1:numtestwords
        trialnum = trialnum + 1;
        [wav, samprate, bits] = wavread(fullfile('dlxexp6-wavs', [testwords{testorder(test)} '.wav']));
        wav = wav'; % this is necessary to run on mac
        
        screen(w, 'FillRect', [255 255 255], outerbarrect); % draw white rectangle
        screen(w, 'FrameRect', grey, greybarrect); % draw grey outline inside
        
        snd('Play', wav * soundvolume, samprate, bits);
        [keydown, keycode, button, rt] = getresponsewithbar(bbox, port, w, barrect);
        if keycode == 'q'
            snd('Close');
			fclose(outfid);
			screen('CloseAll');
            error('Quit key pressed.');
        end
        snd('Quiet');
        
        match = testorder(test) == 1;
        
        if match == 0 & (keycode == 'n' | button == 1)
            corr = 1;
        elseif match == 1 & (keycode == 'y' | button == 4)
            corr = 1;
        else
            corr = 0;
        end
        
        fprintf(outfid, '%d\t%s\t%s\t%.3f\t%s\t%d\t%d\n', trialnum, targetword, ...
            testwords{testorder(test)}, rt, keycode, button, corr);
        while kbcheck
        end
        accuratewait(0.5);
    end
    screen(w, 'FillRect', grey);
    accuratewait(3);
    
    if target == numpractice
        screen(w, 'DrawText', 'You''ve finised the practice items. Press any key when you''re ready to start.', 200, 200, 255);
        accuratewait(1);
        kbwait;
        screen(w, 'FillRect', grey);
        accuratewait(3);
    end
end

% finish up
snd('Close');
fclose(outfid);
screen(w, 'DrawText', 'You''ve finished! Press any key to continue.', 200, 200, 255);
accuratewait(1);
kbwait;
screen('CloseAll');


function [keydown, keycode, button, rt] = getresponsewithbar(bbox, port, w, barrect)

trialstarttime = getsecs;
button = [];
keydown = 0;
barsdone = 0;

while isempty(button) & keydown == 0
    
    if getsecs - trialstarttime > (barsdone + 1) * 0.1 & barsdone < 20
        barsdone = barsdone + 1;
        if barsdone <= 15
            color = [0 160 0];
        elseif barsdone <= 19
            color = [255 150 0];
        elseif barsdone == 20
            color = [255 0 0];
        end
        growingbarrect = [barrect(1) + (barrect(3) - barrect(1)) * (barsdone - 1) / 20, barrect(2), ...
                barrect(1) + (barrect(3) - barrect(1)) * barsdone / 20 - 1, barrect(4)];
        % growingbarrect = [barrect(1), barrect(2), barrect(1) + (barrect(3) - barrect(1)) * barsdone / 20, barrect(4)];
        screen(w, 'FillRect', color, growingbarrect);
    end
    
    if bbox
        button = psychserial('Read', port, 0.001);
        if ~isempty(button)
            button = double(button(1));
            if button ~= 1 & button ~= 2 & button ~=4 & button~=8 & button~=16
                button = [];
            end
        end
    else
        button = [];
    end

    [keydown, secs, keys] = kbcheck;
end

trialendtime = getsecs;
rt = trialendtime - trialstarttime;

if keydown
    keycode = kbname(keys);
else
    keycode = 0;
end

keycode = keycode(1);

if isempty(button)
    button = 0;
end
