generated from cpp-lln-lab/template_PTB_experiment
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mainScript.m
216 lines (166 loc) · 7.54 KB
/
mainScript.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
% (C) Copyright 2018 Mohamed Rezk
% (C) Copyright 2020 CPP visual motion localizer developpers
%% mainScript
% 1- Nom de l'experience ici
% 2- Courte description de l'experience & de la consigne pour le participant
% 3- Nom du fichier de l'experience (script principal) & noms des fonctions utilisees
% 4- Courte description de ce qui est enregistre (output du script)
% 5- Ce dont on a besoin pour rouler votre script (e.g.,: PsychToolBox, fichier des conditions/essais)
% 6- L'annee de naissance, vos noms & courriels
%Code ci-dessous:
% Clear all previous stuff
clear all;
clc;
% make sure we got access to all the required functions and inputs
initEnv();
%Cfg: configuration, a structure that holds all the info of the experiment
% Set and load all the parameters to run the experiment
cfg = setParameters;
% Get subject name & run number before the start of the experiment
cfg = userInputs(cfg);
% % %Other option for user inputs:
% % %Get subject name and run number
% % subjectName = input('Enter Subject Name: ','s');
% % if isempty(subjectName)
% % subjectName = 'trial';
% % end
% % %
% % runNumber = input('Enter the run Number: ','s');
% % if isempty(runNumber)
% % runNumber = 'trial';
% % end
% %
% % % % If the run number is set to zero, it is a training session
% % if strcmp(runNumber,'0')
% % numberTraining = 10;
% % Cfg.numEvents = numberTraining;
% % end
% Create the BIDS directories & fileNames for the behavioral output
% for this subject / session / run using the information from cfg.
cfg = createFilename(cfg);
%% Experiment
% Safety loop: close the screen if code crashes (try-catch)
try
%% Init the experiment
[cfg] = initPTB(cfg);
% % Basic elements to include to initialize the experiment:
% AssertOpenGL; % Verify if PTB is based on OpenGL & Screen(), break & error if not
%
% Screen('Preference','SkipSyncTests', 1); % forces script to continue if sync tests fail
% Screen('Preference','TextRenderer', 0)
% % Select screen with maximum id for output window:
% screenid = max(Screen('Screens'));
%
% % Open a fullscreen, onscreen window with grey background.
% PsychImaging('PrepareConfiguration'); %Prepare setup of imaging pipeline for onscreen window
%
% if TestingSmallScreen
% [Cfg.win, Cfg.winRect] = PsychImaging('OpenWindow', screenid, Cfg.Background_color, [0,0, 480, 270]); % Open window in a small screen (when modifying/testing script)
% else
% [Cfg.win, Cfg.winRect] = PsychImaging('OpenWindow', screenid, Cfg.Background_color); % Open window on selected (max) screen
% end
%
% % Set maximum priority level
% priorityLevel = MaxPriority(Cfg.win); % prioritize over other computer processes
% Priority(priorityLevel)
%
% % Get the Center of the Screen
% Cfg.center = [Cfg.winRect(3), Cfg.winRect(4)]/2; %it assumes the rectangle starts from (0,0); 3 and 4 are the components
% Finalize PTB setup
cfg = postInitializationSetup(cfg);
% % generic function to finalize some set up after PTB has been
% % initialized
%
% cfg = deal(varargin{:});
%
% % Convert some values from degrees to pixels
%
% % Get some values in pixels per frame
%
% varargout = {cfg};
% Prepare output logfiles
cfg = checkCFG(cfg);
logFile = saveEventsFile('init', cfg);
logFile = saveEventsFile('open', cfg, logFile);
disp(cfg);
% Show experiment instruction
standByScreen(cfg);
% Screen('FillRect', cfg.screen.win, cfg.color.background, cfg.screen.winRect);
%
% DrawFormattedText(cfg.screen.win, ...
% cfg.task.instruction, ...
% 'center', 'center', cfg.text.color);
%
% Screen('Flip', cfg.screen.win);
%
% % Wait for space key to be pressed
% pressSpaceForMe();
% Prepare the KbQueue to collect responses
getResponse('init', cfg.keyboard.responseBox, cfg);
%% Experiment Start
cfg = getExperimentStart(cfg);
% drawFixation(cfg);
% vbl = Screen('Flip', cfg.screen.win);
% cfg.experimentStart = vbl;
getResponse('start', cfg.keyboard.responseBox);
%% For Each Block
for iBlock = 1:cfg.design.nbBlocks
waitFor(cfg, cfg.timing.onsetDelay);
for iTrial = 1:cfg.design.nbTrials
fprintf('\n - Running trial %.0f \n', iTrial);
% Check for experiment abortion from operator
checkAbort(cfg, cfg.keyboard.keyboard);
% generic function to prepare some structure before each trial
[thisEvent, thisFixation, cfg] = preTrialSetup(cfg, iBlock, iTrial);
% play the trial and collect onset and duration of the event
[onset, duration] = doTrial(cfg, thisEvent, thisFixation);
% generic function to prepare structures before saving
thisEvent = preSaveSetup( ...
thisEvent, ...
iBlock, ...
iTrial, ...
duration, onset, ...
cfg, ...
logFile);
saveEventsFile('save', cfg, thisEvent);
% collect the responses and appends to the event structure for
% saving in the tsv file
responseEvents = getResponse('check', cfg.keyboard.responseBox, cfg);
responseEvents(1).isStim = logFile.isStim;
responseEvents(1).fileID = logFile.fileID;
responseEvents(1).extraColumns = logFile.extraColumns;
saveEventsFile('save', cfg, responseEvents);
waitFor(cfg, cfg.timing.ISI);
end
end
% End of the run (if doing an fMRI experiment: allows the BOLD signal to go down )
waitFor(cfg, cfg.timing.endDelay);
cfg = getExperimentEnd(cfg);
% drawFixation(cfg);
% endExpmt = Screen('Flip', cfg.screen.win);
%
% disp(' ');
% ExpmtDur = endExpmt - cfg.experimentStart;
% ExpmtDurMin = floor(ExpmtDur / 60);
% ExpmtDurSec = mod(ExpmtDur, 60);
% disp(['Experiment lasted ', ...
% num2str(ExpmtDurMin), ' minutes ', ...
% num2str(ExpmtDurSec), ' seconds']);
% disp(' ');
% Close the logfiles
saveEventsFile('close', cfg, logFile);
getResponse('stop', cfg.keyboard.responseBox);
getResponse('release', cfg.keyboard.responseBox);
createJson(cfg, cfg);
farewellScreen(cfg);
% Screen('FillRect', cfg.screen.win, cfg.color.background, cfg.screen.winRect);
% DrawFormattedText(cfg.screen.win, 'Thank you!', 'center', 'center', cfg.text.color);
% Screen('Flip', cfg.screen.win);
% if isfield(cfg, 'mri')
% WaitSecs(cfg.mri.repetitionTime * 2);
% end
cleanUp(); % close all windows, ports, show mouse cursor, close keyboard queues & give access back to the keyboards
catch
cleanUp();
psychrethrow(psychlasterror); % will catch bug in last error and display
end