-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgs.m
107 lines (90 loc) · 3.5 KB
/
mgs.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
function savefile = mgs(subj, imgset, nblock, use_et)
% clear everything
% sca; close all; clearvars;
% subject - any string
if nargin < 1
subj = input('participant id: ','s');
end
% imageset - A, B, C
if nargin < 2
imgset = input('imgset (A|B): ','s');
end
% nuber of runs/blocks - probably 3
if nargin < 3
nblock = input_def('number of runs (3) [ignored if resume]: ', 3);
end
% should we use eyetracking?
if nargin < 4
fprintf('MAKE SURE EYE IS TRACKED/IDed by eyelink\n')
use_et = input('use eyetracking (1|0, defulat to 1): ');
if isempty(use_et)
use_et=1;
end
end
% validate input
validatestring(imgset,{'A','B','C'});
% initialze (or reset current block of) a matfile that we will load from/save to
savefile = resume_or_new(subj, imgset, nblock);
load(savefile, 'event_info','imgs_used','starttime', 'eventtimes', 'trial');
% some settings
ntrials = max([event_info.trial]);
nblocks = max([event_info.block]);
cblock = event_info.block(trial);
% initialze screen, DAQ, and eyetracking
[w, hid, et] = mgs_setup(subj, use_et);
% if eyetracking, what do we tell the eye tracker when we start
if ~isempty(et), startmsg = sprintf('START%d',cblock); else, startmsg=''; end
% make textures for events that need it
event_tex = make_textures(w, imgs_used, event_info.events);
% show instructions
% if cblock is > 1, ask if we should show instructions. default to no
if cblock <= 1 || input_def('see instructions (0|1, default to 0): ', 0) > 0;
instructions(w)
end
% notify what block we are on when not starting on the first
if cblock > 1
disp_til_key(w, sprintf('Starting %d/%d', cblock, nblocks))
end
% start eye recording
if ~isempty(et), Eyelink('StartRecording'); end
% initialze with fixation
prep_event(w, 'fix')
% start with 500ms of fix
start_delay=.5;
screenstart = Screen('Flip', w);
send_triggers(hid, 0, startmsg);
% half second of fix before start
starttime(cblock) = screenstart + start_delay;
nevents = length(event_info.onsets);
for eidx=trial:nevents
% get event info. including e.name, e.trial, and e.cblock
% extract from events, blocks,
e = event_from_info(event_info, starttime, event_tex, eidx);
% run event: presentation, flip, triggers, and fixation (if mgs)
% returns event onset time, and fixonset (NaN if not mgs)
[etime fixon_flip] = task_event(hid, w, et, e, ntrials)
% save output
if ~isnan(fixon_flip), eventtimes(e.trial).fix = fixon_flip; end
eventtimes(e.trial).(e.name) = etime;
save(savefile, 'event_info','imgs_used','starttime', 'eventtimes', 'trial');
%fprintf('saved to %s @ %f\n', savefile, GetSecs())
% starting new block
if cblock ~= event_info.block(min(eidx+1,nevents))
WaitSecs(.5); % show last fixation for half a second
msg = sprintf('Finished %d/%d!', cblock, nblocks);
ttlmsg='';
if ~isempty(et), ttlmsg=sprintf('BLOCKEND%d',e.cblock); end
send_triggers(hid, 0, ttlmsg);
keyhit = disp_til_key(w, msg);
% show fix for .5 seconds
fixon_flip = showfix(w, e.trial, hid, et, keyhit);
cblock = event_info.block(eidx+1);
starttime(cblock) = fixon_flip + .5;
end
end
% stop recording
if ~isempty(et), Eyelink('StopRecording'); end
disp_til_key(w, 'Finished!\n Thank you!!')
% have a lot of textures open, close them
Screen('CloseAll')
end