-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblinking.m
256 lines (190 loc) · 6.85 KB
/
blinking.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
function basic_stim_serial()
%% Installation instruction for Psychtoolbox
% bash
% sudo apt-get install octave
% sudo apt-get install liboctave-dev
% sudo apt-get install octave-psychtoolbox-3
% sudo octave --no-gui
%% Installing instrument crontol package
% octave
% pkg install -forge instrument-control
% pkg load instrument-control
sca;
close all;
clear;
%% Experimental parameters
SAVEFOLDER = '/mnt/data/ptb/';
paramsField = {'animalName', ...
'time', ...
'nBlock', ...
'nTrialTest', ...
'nTrialControl', ...
'stimulusDuration', ...
'itiStart', ...
'itiMean', ...
'itiEnd', ...
'laserLatency', ...
'laserDuration', ...
'laserEnable'};
paramsDefault = {'ptb', ... % animal name
strftime('%Y%m%d_%H%M%S', localtime(time())), ... % time
10, ... % n block
10, ... % n trial test per block
5, ... % n trial control per block
0.5, ... % stimulus duration in second
0.5, ... % iti start in second
1.5, ... % iti mean in second
3, ... % iti end in second
0, ... % laser latency in ms
20, ... % laser duration in ms
1}; % laser enable
paramsValue = inputdlg(paramsField, '', 1, paramsDefault);
if isempty(paramsValue); return; end
for iParams = 3:length(paramsValue)
paramsValue{iParams} = str2double(paramsValue{iParams});
end
params = cell2struct(paramsValue, paramsField);
params.nTrial = params.nBlock * (params.nTrialTest + params.nTrialControl);
%% Serial commnunication
pkg load instrument-control
ser = serial('/dev/ttyACM0');
srl_write(ser, '0d');
pause(0.05);
srl_write(ser, ['l', params.laserLatency]); % set laser latency from FPGA output
srl_write(ser, ['D', params.laserDuration]); % set laser duration
%% Psychtoolbox setup
% Change preference
PsychDefaultSetup(2);
Screen('Preference', 'VisualDebuglevel', 3);
oldEnableFlag = Screen('Preference', 'Verbosity', [1]);
oldSyncFlag = Screen('Preference', 'SkipSyncTests', 2);
% Get screen information
screens = Screen('Screens');
screenNumber = max(screens);
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
% Open window
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, black);
% Make this top priority
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);
% Set the blend function for the screen (I don't know what it means...)
%Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Get window information
[params.screenXpixels, params.screenYpixels] = Screen('WindowSize', window);
params.ifi = Screen('GetFlipInterval', window); % inter-flip interval
params.frameRate = 1 / params.ifi;
params.stimulusFrame = round(params.stimulusDuration * params.frameRate);
% Open shader (I don't know what it means...)
AssertGLSL;
glsl = MakeTextureDrawShader(window, 'SeparateAlphaChannel');
% Keyboard information
startKey = KbName('F12');
escapeKey = KbName('SCROLLLOCK');
%% Make stimuli
% Square
squareRect = [0, 0, params.screenXpixels, params.screenYpixels];
if params.laserEnable
enableBase = [true(params.nTrialTest, 1); false(params.nTrialControl, 1)];
else
enableBase = false(params.nTrialTest + params.nTrialControl, 1);
end
%% Plot
% Initialize window
Screen('FillRect', window, black, squareRect);
Screen('TextSize', window, 40);
DrawFormattedText(window, 'Press F12 to start, and SCROLLLOCK to exit', 'center', params.screenYpixels * 0.975, [0.25, 0.25, 0.25]);
Screen('Flip', window);
inExperiment = checkWaitKey(startKey, escapeKey);
while inExperiment
% Generate trial structure
enable = [];
for iBlock = 1:params.nBlock
idx = randperm(params.nTrialTest + params.nTrialControl);
enable = [enable; enableBase(idx)];
end
iti = exprnd(params.itiMean - params.itiStart, params.nTrial, 1) + params.itiStart;
iti(iti > params.itiEnd) = params.itiEnd;
itiFrame = round(iti / params.ifi);
% Save setup data
fileName = [params.animalName, '_', strftime('%Y%m%d_%H%M%S', localtime(time())), '.mat'];
fullFileName = fullfile(SAVEFOLDER, fileName);
% Initial iti
Screen('FillRect', window, black, squareRect);
srl_write(ser, '0d');
vbl = Screen('Flip', window);
% Start trial
vbl = Screen('Flip', window, vbl + 1);
for iTrial = 1:params.nTrial
% Inter-trial interval
for iFrame = 1:itiFrame(iTrial)
Screen('FillRect', window, black, squareRect);
if checkKey(escapeKey)
srl_write(ser, '0d');
enable = enable(1:(iTrial-1));
itiFrame = itiFrame(1:(iTrial-1));
save('-mat7-binary', fullFileName, 'enable', 'itiFrame', 'params');
finishTask();
return
end
if iFrame == 3
if enable(iTrial)
srl_write(ser, '0e');
else
srl_write(ser, '0d');
end
end
vbl = Screen('Flip', window, vbl + 0.5 * params.ifi);
end
% Trial
for iFrame = 1:params.stimulusFrame
Screen('FillRect', window, white, squareRect);
if checkKey(escapeKey)
srl_write(ser, '0d');
enable = enable(1:(iTrial-1));
itiFrame = itiFrame(1:(iTrial-1));
save('-mat7-binary', fullFileName, 'enable', 'itiFrame', 'params');
finishTask();
return
end
if iFrame == 3
srl_write(ser, '1');
end
vbl = Screen('Flip', window, vbl + 0.5 * params.ifi);
end
end
% Last iti
Screen('FillRect', window, black, squareRect);
srl_write(ser, '0d');
vbl = Screen('Flip', window, vbl + 1);
save('-mat7-binary', fullFileName, 'enable', 'itiFrame', 'params');
Screen('FillRect', window, black, squareRect);
DrawFormattedText(window, 'Press F12 to start, and SCROLLLOCK to exit', 'center', params.screenYpixels * 0.975, [0.25, 0.25, 0.25]);
Screen('Flip', window);
inExperiment = checkWaitKey(startKey, escapeKey);
end
srl_write(ser, '0d');
finishTask();
fclose(ser);
function inExperiment = checkWaitKey(startKey, escapeKey)
notDone = true;
while notDone
[~, keyCode] = KbStrokeWait;
if keyCode(escapeKey)
inExperiment = false;
notDone = false;
elseif keyCode(startKey)
inExperiment = true;
notDone = false;
end
end
function stop = checkKey(escapeKey)
[keyDown, ~, keyCode] = KbCheck;
stop = false;
if keyCode(escapeKey)
stop = true;
end
function finishTask()
% Clear the screen
Priority(0);
sca;