Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mschrimpf committed Jul 29, 2016
0 parents commit 6d78fe8
Show file tree
Hide file tree
Showing 15 changed files with 1,021 additions and 0 deletions.
86 changes: 86 additions & 0 deletions AwaitGoodFixation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function [exp_params, hadToRecalibrate] = AwaitGoodFixation(exp_params, ...
window, windowRect, fixationScreen, getKeyInput)
%AWAITGOODFIXATION wait for good fixation and recalibrate if necessary
goodFixation = false;
while(~goodFixation)
goodFixation = fixationPoint(window, fixationScreen, ...
windowRect(3) / 2, windowRect(4) / 2, ...
exp_params.fixation_threshold, exp_params.fixation_time, ...
exp_params.fixation_timeout);

if ~goodFixation
hadToRecalibrate = true;
DrawCenteredText(window, ...
{'Unable to detect fixation. Recalibrate [y/n]?'}, 0);
Screen('flip', window);
keyCode = getKeyInput();
localEyelinkFile = getEyelinkFilepath(...
'eyemovements_OcclusionQT_', exp_params.subject_name);
c = clock;
expEyelinkFile = [...
exp_params.subject_name(end-2:end) ...
num2str(mod(c(3), 10), '%0.1d') num2str(c(4), '%0.2d') ...
num2str(c(5), '%0.2d') '.edf'];
if(find(keyCode) == KbName('y')) % recalibrate if 'y' is entered
status = Eyelink('closefile');
if status ~= 0
fprintf('closefile error, status: %d', status);
end
Eyelink('ReceiveFile', exp_params.eyelink_file, localEyelinkFile);
exp_params.eyelink_file = expEyelinkFile;
calibrateEyelink(window, windowRect, exp_params.eyelink_file);
elseif find(keyCode) == KbName('x')
% save existing eyelink data
status = Eyelink('closefile');
if status ~= 0
fprintf('closefile error, status: %d', status);
end
Eyelink('ReceiveFile', exp_params.eyelink_file, ...
localEyelinkFile);
exp_params.eyelink_file = expEyelinkFile;

% turn eyelink OFF
exp_params.eyelink = 0;
goodFixation = true;
end
end
end
end

function resultcode = fixationPoint(window, fixationScreen, ...
fixX, fixY, fixAccuracy, fixTime, timeout)
%should probably add in some keyboard awareness here, in case we need to
%cancel or pause or something during fixation.
Screen('DrawTexture',window,fixationScreen);
Screen('flip',window);
fixOnTime = GetSecs;

resultcode = 0;
firstGoodTime = -1;
lastGoodTime = -1;
while (GetSecs - fixOnTime < timeout) && (resultcode < 1)
fsample = eyelink('NewestFloatSample');
if isstruct(fsample)
gx = max(fsample.gx); %gaze position in pixels - I think whichever eye is not tracked is -32768, so this should pull out the useful position
gy = max(fsample.gy);
rx = fsample.rx; %pixels per degree
ry = fsample.ry;
fixError = sqrt(((fixX - gx)/rx)^2 + ((fixY - gy)/ry)^2);
if fixError <= fixAccuracy
lastGoodTime = GetSecs;
if firstGoodTime == -1
firstGoodTime = lastGoodTime;
end
else
lastGoodTime = -1;
firstGoodTime = -1;
end
if lastGoodTime - firstGoodTime > fixTime
resultcode = 1;
end
elseif GetSecs - lastGoodTime > .05
firstGoodTime = -1;
lastGoodTime = -1;
end
end
end
23 changes: 23 additions & 0 deletions DrawCenteredText.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function DrawCenteredText(window, text, color, shiftX)
if ~exist('shiftX', 'var')
shiftX = 0;
end

% text has to be a cell array of lines

if nargin < 3
color = 0;
end
win_rect=Screen('Rect', window);
total_height=0;
for ln=1:length(text);
bbox{ln} = Screen('TextBounds',window,text{ln});
total_height=total_height+bbox{ln}(RectBottom);
bbox{ln} = CenterRect(bbox{ln},win_rect);
end
y=win_rect(RectTop)+(win_rect(RectBottom)-win_rect(RectTop)-total_height)/2;
for ln=1:length(text);
x=bbox{ln}(RectLeft) + shiftX;
Screen('DrawText',window,text{ln},x,y,color);
y=y+bbox{ln}(RectBottom)-bbox{ln}(RectTop);
end
28 changes: 28 additions & 0 deletions GKlab_ScreenGeometry.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function [Geo] = GKlab_ScreenGeometry (ScrWdPix, ScrWdCm, DistToScrCm)

% General screen considerations:

% Scanner Projection System Measurements:
HalfScrWdCm = ScrWdCm/2;

% Screen Size (Scanner Projector) in Pixels:
% ScrWdPix = 1920; %1024
HalfScrWdPix = ScrWdPix/2;

CmPerPix = ScrWdCm/ScrWdPix; % = .035
DistToScrPix = ceil(DistToScrCm/CmPerPix); % = 2429

% For the Pixs nearest to the Fov:
PixFromFov = 1;

Geo.FovDegPerPix = atand(PixFromFov/DistToScrPix);
Geo.FovPixPerDeg = Geo.FovDegPerPix^(-1);

% For the Pixs nearest to the Scr's edge:
PixNearPeriph1 = HalfScrWdPix-1;
PixNearPeriph2 = HalfScrWdPix;

Geo.PeriphDegPerPix = atand(PixNearPeriph2/DistToScrPix) - atand(PixNearPeriph1/DistToScrPix);
Geo.PeriphPixPerDeg = Geo.PeriphDegPerPix^(-1);

Geo.GenPixPerDeg = pi * ScrWdPix / atan(ScrWdCm/DistToScrCm/2) / 360; % slightly different.
82 changes: 82 additions & 0 deletions GenScreenSetup.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function [win,ScrVars] = GenScreenSetup(BG,Res)

% Based on mlScreenSetup
%
% Function to set up all the usual screen values that Mark Lescroart uses:
% Inputs:
% BackGround - default [128 128 128]
% Text size = 18
% Text font = Arial
% Background = [128 128 128] (gray)
%

% Inputs:
if ~exist('BG','var')
ScrVars.BG = [128 128 128];
else
ScrVars.BG = BG;
end
AssertOpenGL;

WarnStr = '\n\n\nmlScreenSetup currently has anti-aliasing turned on. This may decrease performance! \nPlease check code timing before proceeding with experiments!\n\n\n';
warning([mfilename ':Anti-Alias Warning'],WarnStr)

%%% Get screen number
ScrVars.ScreenNumber = max(Screen('Screens'));
try
if exist('Res','var')
Screen('Resolution', ScrVars.ScreenNumber, Res(1), Res(2));
end

ExtraParams = Screen('Resolution', ScrVars.ScreenNumber);
if (ExtraParams.width~=Res(1) || ExtraParams.height~=Res(2))
ScrVars.Xdimensions(1) = ExtraParams.height;
ScrVars.Xdimensions(2) = ExtraParams.width;
ScrVars.XframeRate = ExtraParams.hz;
beep;
warning('DisplayError:DisplaySizeChangeFail','Failed to set indicated resolution.');
display('Saving current resolution to display structure (ScrVars)...');
pause(.5);
else
display('Resolution and refresh rate successfully set.');
end
catch
ExtraParams = Screen('Resolution',ScrVars.ScreenNumber);
ScrVars.Xdimensions(1) = ExtraParams.height;
ScrVars.Xdimensions(2) = ExtraParams.width;
ScrVars.XframeRate = ExtraParams.hz;
beep;
pause(.5);
warning('DisplayError:DisplaySizeChangeFail','Error occurred in Screen(''Resolution'') call.\n\tMay have failed to set indicated resolution and refresh rate.');
display('Saving current resolution and refresh rate to display structure...');
end
% Open full screen for experiment:
% Usage: [Window,Rect] = Screen('OpenWindow',windowPtrOrScreenNumber [,color] [,rect][,pixelSize][,numberOfBuffers][,stereomode][,multisample][,imagingmode]);
[win, ScrVars.winRect] = Screen('OpenWindow',ScrVars.ScreenNumber, ScrVars.BG,[],32,2,[],6);

%%% Getting inter-flip interval (ifi):
Priority(MaxPriority(win));
ScrVars.ifi = Screen('GetFlipInterval', win, 20);
Priority(0);

Screen('TextFont', win, 'Arial');
Screen('TextSize', win, 18);

%%% Establishing generally useful screen value variables in a struct array:
ScrVars.x_min = ScrVars.winRect(1);
ScrVars.x_max = ScrVars.winRect(3);
ScrVars.y_min = ScrVars.winRect(2);
ScrVars.y_max = ScrVars.winRect(4);

ScrVars.AspectRatio = ScrVars.x_max/ScrVars.y_max;
ScrVars.winWidth = ScrVars.x_max-ScrVars.x_min;
ScrVars.winHeight = ScrVars.y_max-ScrVars.y_min;
ScrVars.x_center = ScrVars.winWidth/2;
ScrVars.y_center = ScrVars.winHeight/2;

ScrVars.FixRect = [0 0 10 10];
ScrVars.FixRectBig = [0 0 12 12];
ScrVars.Fixation = CenterRectOnPoint (ScrVars.FixRect, ScrVars.x_center, ScrVars.y_center);
ScrVars.FixationBig = CenterRectOnPoint (ScrVars.FixRectBig, ScrVars.x_center, ScrVars.y_center);

%[ScrVars.ScreenGeo] = SkyraScreenGeometry;
57 changes: 57 additions & 0 deletions calibrateEyelink.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
function result = calibrateEyelink(window, screenRect, edfFile)

try
Eyelink('Shutdown')
end

el=EyelinkInitDefaults(window);
% Disable key output to Matlab window:
%ListenChar(2);

% STEP 3
% Initialization of the connection with the Eyelink Gazetracker.
% exit program if this fails.
if ~EyelinkInit(0, 1)
fprintf('Eyelink Init aborted.\n');
cleanup; % cleanup function
return;
end

[v vs]=Eyelink('GetTrackerVersion');
fprintf('Running experiment on a ''%s'' tracker.\n', vs );

Eyelink('command', 'calibration_type = HV9');

% set the eyetracker commands
Eyelink('Command', 'screen_pixel_coords = %d %d %d %d', screenRect(1),screenRect(2),screenRect(3)-1,screenRect(4)-1);

Eyelink('Message', 'DISPLAY_COORDS %d %d %d %d', screenRect(1),screenRect(2),screenRect(3)-1,screenRect(4)-1);

Eyelink('Command', 'file_event_filter = LEFT,RIGHT,FIXATION,SACCADE,BLINK,MESSAGE,BUTTON,INPUT');

Eyelink('Command', 'link_event_filter = LEFT,RIGHT,FIXATION,BUTTON');

% make sure that we get gaze data from the Eyelink
Eyelink('Command', 'link_sample_data = LEFT,RIGHT,GAZE,AREA');

% open file to record data to
Eyelink('Openfile', edfFile);

% STEP 4
% Calibrate the eye tracker
result = EyelinkDoTrackerSetup(el);

% do a final check of calibration using driftcorrection
EyelinkDoDriftCorrection(el);

% STEP 5
% start recording eye position
Eyelink('StartRecording');
% record a few samples before we actually start displaying
WaitSecs(0.1);
% mark zero-plot time in data file
Eyelink('Message', 'DISPLAY_ON'); % message for RT recording in analysis
Eyelink('Message', 'SYNCTIME');

fprintf('Eyelink set up complete...\n');
end
9 changes: 9 additions & 0 deletions createFixationMatrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function fixationMatrix = createFixationMatrix(...
fixationSize, fixationWidth, backgroundColor, fixationColor)
fixationMatrix = backgroundColor * ...
ones(fixationSize + fixationWidth);
fixationMatrix(ceil(fixationSize / 2 - fixationWidth / 2) + ...
1:floor(fixationSize / 2 + fixationWidth / 2 + 1), :) = fixationColor;
fixationMatrix(:, ceil(fixationSize / 2 - fixationWidth / 2) + ...
1:floor(fixationSize / 2 + fixationWidth / 2 + 1)) = fixationColor;
end
10 changes: 10 additions & 0 deletions createGratingImage.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function gratingImage = createGratingImage(targetDegrees, degreesPerPixel, ...
background, foreground)
pixels = round(targetDegrees / degreesPerPixel);
gratingImage = background * ones(pixels, pixels);
numBars = 5;
gratingWidth = (targetDegrees / numBars) / degreesPerPixel;
for x = 1:gratingWidth * 2:pixels
gratingImage(:, x:x + gratingWidth) = foreground;
end
end
20 changes: 20 additions & 0 deletions drawScoreScreen.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function drawScoreScreen(window, blockIndex, ...
performance, performanceMessages, color)
if performance > 0.9
markstr = performanceMessages{5};
elseif performance > 0.8
markstr = performanceMessages{4};
elseif performance > 0.7
markstr = performanceMessages{3};
elseif performance > 0.6
markstr = performanceMessages{2};
else
markstr = performanceMessages{1};
end
fprintf('%s\n', markstr);
DrawCenteredText(window, ...
{['Block ', num2str(blockIndex) ': ', markstr]}, color);
Screen('flip',window);
WaitSecs(1);
end

9 changes: 9 additions & 0 deletions getEyelinkFilepath.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function filepath = getEyelinkFilepath(prefix, subjectName)
c = clock;
filepath = fullfile('subjects', subjectName, ...
[prefix subjectName ...
'-' num2str(c(1)) '_' num2str(c(2),'%0.2d') '_' ...
num2str(c(3), '%0.2d') '-' num2str(c(4),'%0.2d') '_' ...
num2str(c(5), '%0.2d') '_' num2str(round(c(6)), '%0.2d') ...
'.edf']);
end
26 changes: 26 additions & 0 deletions initPMD1208FS.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function d=initPMD1208FS();

% initPMD1208FS
% usage:
% d=initPMD1208FS
%
% last modified: Itamar Khan / Hesehng Liu

daq=DaqDeviceIndex;
switch length(daq)
case 0,
fprintf('Sorry. Couldn''t find a PMD-1208FS box connected to your computer.\n');
d = -1;
return;
case 1,
fprintf('Yay. You have a PMD-1208FS daq: \n');
case 2,
fprintf('Yay. You have two PMD-1208FS daqs: \n');
otherwise,
fprintf('Yay. You have %d PMD-1208FS daqs: \n',length(daq));
end
devices=PsychHID('Devices');
for i=1:length(daq)
d=devices(daq(i));
fprintf('device %d, serialNumber %s\n',d.index,d.serialNumber);
end
17 changes: 17 additions & 0 deletions parseArgs.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function exp_params = parseArgs(exp_params, varargin)
for a = 1:2:length(varargin)
argument = lower(cell2mat(varargin(a)));
value = lower(cell2mat(varargin(a + 1)));
if strcmp(value, 'yes') || strcmp(value, 'on')
value = true;
elseif strcmp(value, 'no') || strcmp(value, 'off')
value = false;
end
% assign the value to a variable with the same name as the argument
if isfield(exp_params, argument)
exp_params = setfield(exp_params, argument, value);
else
error('unknown parameter: %s', argument);
end
end
end
Loading

0 comments on commit 6d78fe8

Please sign in to comment.