-
Notifications
You must be signed in to change notification settings - Fork 2
/
runEquiFlicker.m
391 lines (351 loc) · 12.2 KB
/
runEquiFlicker.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
function runEquiFlicker(ana)
fprintf('\n\n--->>> runEquiFlicker Started: UUID = %s!\n',ana.uuid);
%============================use reward system?======================================
global rM
if ana.sendReward
if ~exist('rM','var') || isempty(rM)
rM = arduinoManager;
end
open(rM) %open our reward manager
end
%=================================general bits=======================================
%if ispc; PsychJavaTrouble(); end
KbName('UnifyKeyNames');
PsychDefaultSetup(2);
%==========================Initiate out metadata=====================================
ana.date = datestr(datetime);
ana.version = Screen('Version');
ana.computer = Screen('Computer');
ana.gpu = opengl('data');
%==========================experiment parameters=====================================
if ana.debug
ana.screenID = 0;
ana.windowed = [0 0 1000 1000];
ana.bitDepth = '8bit';
else
ana.screenID = max(Screen('Screens'));%-1;
end
%=======================Make a name for this run=====================================
pf='EquiFlicker_';
if ~isempty(ana.subject)
nameExp = [pf ana.subject];
c = sprintf(' %i',fix(clock()));
nameExp = [nameExp c];
ana.nameExp = regexprep(nameExp,' ','_');
else
ana.nameExp = 'debug';
end
cla(ana.plotAxis1);
cla(ana.plotAxis2);
cla(ana.plotAxis3);
drawnow;
try
%=======================open our screen==========================================
sM = screenManager();
if ana.debug || ismac || ispc || ~isempty(regexpi(ana.gpu.Vendor,'NVIDIA','ONCE'))
sM.disableSyncTests = true;
end
sM.screen = ana.screenID;
sM.debug = ana.debug;
sM.windowed = ana.windowed;
sM.pixelsPerCm = ana.pixelsPerCm;
sM.distance = ana.distance;
sM.blend = true;
sM.bitDepth = ana.bitDepth;
sM.verbosityLevel = 4;
if exist(ana.gammaTable, 'file')
load(ana.gammaTable);
if isa(c,'calibrateLuminance')
sM.gammaTable = c;
end
clear c;
if ana.debug
sM.gammaTable.plot
end
end
sM.backgroundColour = ana.backgroundColor;
screenVals = sM.open; % OPEN THE SCREEN
ana.gpuInfo = Screen('GetWindowInfo',sM.win);
ana.screenVals = screenVals;
fprintf('\n--->>> runEquiFlicker Opened Screen %i : %s\n', sM.win, sM.fullName);
disp(screenVals);
if IsLinux
Screen('Preference', 'TextRenderer', 1);
Screen('Preference', 'DefaultFontName', 'Liberation Sans');
end
%===========================SETUP STIMULI========================================
grating = colourGratingStimulus();
grating.size = ana.size;
grating.colour = ana.colorFixed;
grating.colour2 = ana.colorStart;
grating.contrast = 1;
grating.type = ana.type;
grating.mask = ana.mask;
grating.tf = 0;
grating.sf = ana.sf;
setup(grating,sM);
%============================SETUP VARIABLES=====================================
varC = find(ana.colorEnd == max(ana.colorEnd));
varC = varC(1);
varColour = grating.colour2;
%==============================SETUP EYELINK=====================================
ana.strictFixation = false;
eL = eyelinkManager();
fprintf('--->>> runEquiFlicker eL setup starting: %s\n', eL.fullName);
eL.isDummy = ana.isDummy; %use dummy or real eyelink?
eL.name = ana.nameExp;
eL.saveFile = [ana.nameExp '.edf'];
eL.recordData = true; %save EDF file
eL.sampleRate = ana.sampleRate;
eL.calibration.manual = false; % manual calibration?
eL.calibration.proportion = [0.5 0.5];
eL.calibration.style = ana.calibrationStyle; % calibration style
eL.calibration.calibrationtargetcolour = [1 1 1];
eL.calibration.calibrationtargetsize = 1.75;
eL.calibration.calibrationtargetwidth = 0.1;
eL.calibration.waitformodereadytime = 500;
% X, Y, FixInitTime, FixTime, Radius, StrictFix
updateFixationValues(eL, ana.fixX, ana.fixY, ana.firstFixInit,...
ana.firstFixTime, ana.firstFixDiameter, ana.strictFixation);
%sM.verbose = true; eL.verbose = true; sM.verbosityLevel = 10; eL.verbosityLevel = 4; %force lots of log output
initialise(eL, sM); %use sM to pass screen values to eyelink
ListenChar(-1); trackerSetup(eL); ListenChar(0); % do setup and calibration
fprintf('--->>> runEquiFlicker eL setup complete: %s\n', eL.fullName);
WaitSecs('YieldSecs',0.5);
getSample(eL); %make sure everything is in memory etc.
%================================================================================
%-------------prepare variables needed for task loop-----------------------------
NO = 1; YES = 2; UNSURE = 3; REDO = -10; BREAKFIX = -1;
tL = timeLogger();
tL.screenLog.beforeDisplay = GetSecs();
tL.screenLog.stimTime(1) = 1;
breakLoop = false;
ana.trial = struct();
ana.onFrames = round((ana.screenVals.fps/ana.frequency));
thisTrial = 0;
tick = 1;
fInc = 6;
response = BREAKFIX;
nresponse = 0;
halfisi = sM.screenVals.halfisi;
commandwindow
if ~ana.debug; ListenChar(-1); end
Priority(MaxPriority(sM.win));
colours = {};
%================================================================================
%-------------------------------------TASK LOOP----------------------------------
while breakLoop == false
thisTrial = thisTrial + 1;
%=================Define stimulus colours for this run=======================
if mod(thisTrial, 2) == 0
grating.colour2Out = [ana.colorStart(1:3) 1];
else
grating.colour2Out = [ana.colorEnd(1:3) 1];
end
varColour = grating.colour2Out;
update(grating);
fprintf('===>>> FIX=%s | MOD=%s\n',num2str(grating.colourOut(1:3)),num2str(grating.colour2Out(1:3)));
%======================prepare eyelink for this trial ==============
resetFixation(eL);
trackerClearScreen(eL);
trackerDrawFixation(eL); %draw fixation window on eyelink computer
trackerMessage(eL,'V_RT MESSAGE END_FIX END_RT'); %this 3 lines set the trial info for the eyelink
trackerMessage(eL,['TRIALID ' thisTrial]); %obj.getTaskIndex gives us which trial we're at
startRecording(eL);
statusMessage(eL,'INITIATE FIXATION...');
fixated = '';
%=======================Prepare for the stimulus loop========================
vbl = Screen('Flip',sM.win);
tFix = vbl;
%================================initiate fixation===========================
while ~strcmpi(fixated,'fix') && ~strcmpi(fixated,'breakfix')
drawCross(sM, 0.75, [1 1 1 1], ana.fixX, ana.fixY, 0.1, true, 0.5);
finishDrawing(sM);
getSample(eL);
fixated=testSearchHoldFixation(eL,'fix','breakfix');
[tL.vbl(tick), tL.show(tick),tL.flip(tick),tL.miss(tick)] = Screen('Flip',sM.win, vbl + halfisi);
tL.stimTime(tick) = 0;
tick = tick + 1;
[keyIsDown, ~, keyCode] = KbCheck(-1);
if keyIsDown
rchar = KbName(keyCode);
switch lower(rchar)
case {'c'}
fprintf('===>>> runEquiFlicker recalibrate pressed!\n');
fixated = 'breakfix';
stopRecording(eL);
setOffline(eL);
trackerSetup(eL);
WaitSecs('YieldSecs', 1);
case {'d'}
fprintf('===>>> runEquiFlicker drift correct pressed!\n');
fixated = 'breakfix';
stopRecording(eL);
driftCorrection(eL);
WaitSecs('YieldSecs', 1);
case {'q'}
fprintf('===>>> runEquiFlicker Q pressed!!!\n');
fixated = 'breakfix';
breakLoop = true;
end
end
end
if strcmpi(fixated,'breakfix')
fprintf('===>>> BROKE INITIATE FIXATION Trial = %i\n', thisTrial);
statusMessage(eL,'Subject Broke Initial Fixation!');
trackerMessage(eL,'MSG:BreakInitialFix');
response = BREAKFIX;
resetFixation(eL);
stopRecording(eL);
setOffline(eL);
Screen('Flip',sM.win); %flip the buffer
WaitSecs('YieldSecs',0.2);
continue
end
statusMessage(eL,'Show Stimulus...');
%=======================Our actual stimulus drawing loop=====================
startTick = tick; keepRunning = true; stroke = 1; ii = 1; keyTicks = 0; keyHold = 0;
tStart = GetSecs; vbl = tStart;if isempty(tL.vbl);tL.vbl(1) = tStart;tL.startTime = tStart; end
while keepRunning
switch stroke
case 1
grating.driftPhase = 0;
draw(grating)
case 2
grating.driftPhase = 180;
draw(grating)
end
if mod(tick,ana.onFrames) == 0
stroke = stroke + 1;
if stroke > 2; stroke = 1; end
end
drawCross(sM, 0.75, [0.5 0.5 0.5], ana.fixX, ana.fixY, 0.1, true, 0.1);
finishDrawing(sM);
[vbl, tL.show(tick),tL.flip(tick),tL.miss(tick)] = Screen('Flip',sM.win, vbl + halfisi);
if tick == startTick; trackerMessage(eL,'END_FIX'); end
tL.vbl(tick) = vbl; tL.stimTime(tick) = 1 + (stroke/10);
tick = tick + 1;
ii = ii + 1;
keyTicks = keyTicks + 1;
[keyIsDown, ~, keyCode] = KbCheck(-1);
rchar = KbName(keyCode); if iscell(rchar);rchar=rchar{1};end
if keyIsDown
switch lower(rchar)
case {'downarrow','down'}
if keyTicks > keyHold
varColour(varC) = varColour(varC) - 0.005;
if varColour(varC) < 0; varColour(varC) = 0; end
grating.colour2Out = varColour;
update(grating);
fprintf('Variable Colour is: %s\n',num2str(grating.colour2Out,'%.3f '));
keyHold = keyTicks + fInc;
end
case {'uparrow','up'}
if keyTicks > keyHold
varColour(varC) = varColour(varC) + 0.005;
if varColour(varC) > 1; varColour(varC) = 1; end
grating.colour2Out = varColour;
update(grating);
fprintf('Variable Colour is: %s\n',num2str(grating.colour2Out,'%.3f '));
keyHold = keyTicks + fInc;
end
case {'space'}
keepRunning = false;
response = YES;
case {'c'}
fprintf('===>>> runEquiFlicker recalibrate pressed!\n');
stopRecording(eL);
setOffline(eL);
trackerSetup(eL);
keepRunning = false;
WaitSecs('YieldSecs',2);
case {'d'}
fprintf('===>>> runEquiFlicker drift correct pressed!\n');
stopRecording(eL);
driftCorrection(eL);
keepRunning = false;
WaitSecs('YieldSecs',2);
case {'q'}
fprintf('===>>> runEquiFlicker Q pressed!!!\n');
fixated = 'breakfix';
breakLoop = true;
keepRunning = false;
end
end
end
%============================================================================
tEnd=Screen('Flip',sM.win);
if strcmp(fixated,'breakfix')
fprintf('===>>> BROKE FIXATION Trial = %i (%i secs)\n\n', thisTrial, tEnd-tStart);
response = BREAKFIX;
end
if response == YES
nresponse = nresponse + 1;
colours{end+1,1} = grating.colour2Out;
disp(cell2mat(colours));
disp(['Isoluminant flicker point = ' num2str(mean(cell2mat(colours)))]);
end
trackerMessage(eL,['TRIAL_RESULT ' response]);
trackerMessage(eL,'MSG:BreakFix');
resetFixation(eL);
stopRecording(eL);
setOffline(eL);
tL.tick = tick;
if nresponse >= ana.trialNumber
breakLoop = true;
else
WaitSecs('YieldSecs',ana.trialInterval);
end
end % while ~breakLoop
%===============================Clean up============================
Screen('DrawText', sM.win, '===>>> FINISHED!!!',50,50);
Screen('Flip',sM.win);
WaitSecs('YieldSecs', 2);
reset(grating);
close(sM); breakLoop = true;
ListenChar(0);ShowCursor;Priority(0);RestrictKeysForKbCheck([]);
disp(colours);
disp(['Isoluminant flicker point = ' num2str(mean(cell2mat(colours)))]);
fprintf('===>>> runEquiFlicker Finished Trials: %i\n',thisTrial);
if exist(ana.ResultDir,'dir') > 0
cd(ana.ResultDir);
end
trackerClearScreen(eL);
stopRecording(eL);
setOffline(eL);
close(eL);
if ~isempty(ana.nameExp) || ~strcmpi(ana.nameExp,'debug')
ana.colours = colours;
ana.plotAxis1 = [];
ana.plotAxis2 = [];
ana.plotAxis3 = [];
fprintf('==>> SAVE %s, to: %s\n', ana.nameExp, pwd);
save([ana.nameExp '.mat'],'ana', 'eL', 'sM', 'tL', 'colours');
end
tL.printRunLog;
clear ana eL sM tL
catch ME
if exist('eL','var'); close(eL); end
if exist('grating','var');reset(grating);end
if exist('sM','var'); close(sM); end
ListenChar(0);ShowCursor;Priority(0);Screen('CloseAll');RestrictKeysForKbCheck([]);
getReport(ME)
end
%==================================================================updateResponse
function updateResponse
switch response
case {1, 2, 3}
case -10
end
end
%==================================================================updatePlot
function updatePlot(thisTrial)
end
%==================================================================findNearest
function [idx,val,delta]=findNearest(in,value)
%find nearest value in a vector, if more than 1 index return the first
[~,idx] = min(abs(in - value));
val = in(idx);
delta = abs(value - val);
end
end