-
Notifications
You must be signed in to change notification settings - Fork 1
/
BrainCamSystem.m
246 lines (224 loc) · 6.79 KB
/
BrainCamSystem.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
classdef BrainCamSystem < SubSystem
properties % OBLIGATORY
experimentSyncObj
trialSyncObj
frameSyncObj
end
properties
cameraObj
end
properties (Hidden)
lastError
end
events
ExperimentStart
ExperimentStop
NewTrial
NewStimulus
FrameAcquired
end
methods % SETUP
function obj = BrainCamSystem(varargin)
if nargin > 1
for k = 1:2:length(varargin)
obj.(varargin{k}) = varargin{k+1};
end
end
obj.defineDefaults()
obj.checkProperties()
obj.updateExperimentName()
obj.createSystemComponents()
end
function defineDefaults(obj)
persistent instancenum
if isempty(instancenum)
instancenum = 1;
else
instancenum = instancenum+1;
end
obj.defineDefaults@SubSystem;
obj.default.autoSaveFrequency = 10;
end
function checkProperties(obj)
obj.savedDataFiles = VideoFile.empty(1,0);
obj.currentDataFileSet = VideoFile.empty(1,0);
obj.framesAcquired = 0;
obj.checkProperties@SubSystem;
end
function createSystemComponents(obj)
% (Required by SubSystem Parent Class)
% CREATE CAMERA
obj.cameraObj = Camera(...
'camAdaptor', 'hamamatsu',...
'videoFormat', 'MONO16_BIN2x2_1024x1024_FastMode');
setup(obj.cameraObj);
obj.cameraObj.videoInputObj.FramesPerTrigger = 1;
obj.cameraObj.videoInputObj.LoggingMode = 'memory';
trigconfig = struct(...
'TriggerType','hardware',...
'TriggerCondition','RisingEdge',...
'TriggerSource','SynchronousReadoutTrigger');
obj.cameraObj.triggerConfiguration = trigconfig;
obj.cameraObj.videoInputObj.FramesAcquiredFcn = @(src,evnt)frameAcquiredFcn(obj,src,evnt);
end
end
methods % CONTROL
function start(obj)
% (Required by SubSystem Parent Class)
obj.updateExperimentName()
fprintf('STARTING VRSYSTEM:\n\tSession-Path: %s\n',...
obj.sessionPath);
if ~isdir(obj.sessionPath)
mkdir(obj.sessionPath)
end
if isempty(obj.frameSyncListener)
warning('BrainCamSystem:start:NoFrameSyncListener',...
'The Behavior-Control sysem is not connected to a camera, and will not record data every frame');
else
obj.frameSyncListener.Enabled = true;
end
obj.trialStateListener.Enabled = true;
obj.experimentStateListener.Enabled = true;
obj.ready = true;
obj.experimentRunning = false;
fprintf('BrainCamSystem ready... waiting for ExperimentStart event from Virmen\n');
end
function stop(obj)
% (Required by SubSystem Parent Class)
if ~isempty(obj.frameSyncListener)
obj.frameSyncListener.Enabled = false;
end
obj.trialStateListener.Enabled = false;
obj.experimentStateListener.Enabled = false;
if obj.experimentRunning
obj.experimentRunning = false;
if ~isempty(obj.currentDataFile) ...
&& isopen(obj.currentDataFile) ...
&& ~issaved(obj.currentDataFile)
obj.saveDataFile;
obj.currentDataFile = VideoFile.empty(1,0);
end
fprintf('Experiment Stopped\n');
end
end
end
methods % EVENT RESPONSE
function experimentStateChangeFcn(obj,~,evnt)
% (Required by SubSystem Parent Class)
fprintf('BrainCamSystem: Received ExperimentStateChange event\n')
switch evnt.EventName
case 'ExperimentStart'
stop(obj.cameraObj.histogramTimer)
stop(obj.cameraObj.autoRangeTimer)
if ~obj.experimentRunning
start(obj.cameraObj.videoInputObj)
end
case 'ExperimentStop'
stop(obj.cameraObj);
obj.saveDataSet();
obj.clearDataSet();
end
end
function trialStateChangeFcn(obj,~,~)
% (Required by SubSystem Parent Class)
fprintf('BrainCamSystem: Received TrialStateChange event\n')
persistent trialNumberLocal;
if isempty(trialNumberLocal)
trialNumberLocal = 0;
end
% obj.currentTrialNumber = trialNumberLocal;
try
if ~isempty(obj.currentDataFile)
obj.currentDataFile.experimentName = obj.currentExperimentName;
obj.saveDataFile();
end
trialNumberLocal = trialNumberLocal + 1;
% prepare next DataFile with info for next trial
% (or minute of recording)
obj.currentDataFile.experimentName = obj.currentExperimentName;
catch me
obj.lastError = me;
end
end
function frameAcquiredFcn(obj, src, evnt)
% (Required by SubSystem Parent Class)
try
frameNum = obj.framesAcquired + 1;
obj.framesAcquired = frameNum;
% FIRST FRAME
if isempty(obj.currentDataFile)
obj.currentDataFile = VideoFile(...
'rootPath',obj.currentDataSetPath,...
'experimentName',obj.currentExperimentName);
end
% EVERY FRAME
[frameData, time, metadata] = getdata(src, 1, 'uint16');
frameInfo.frameSavedTime = hat;
frameInfo.frameAcquiredTime = time;
frameInfo.frameAcquiredAbsTime = datenum(metadata.AbsTime);
% frameInfo.frameAcquiredTime = evnt.Data.AbsTime;
frameInfo.frameNumber = frameNum;
frameInfo.frameNumFromCam = evnt.Data.FrameNumber;
frameInfo.frameNumRelative = evnt.Data.RelativeFrame;
frameInfo.memoryUsed = evnt.Data.FrameMemoryUsed;
frameInfo.memoryLimit = evnt.Data.FrameMemoryLimit;
if isclosed(obj.currentDataFile)
if ~issaved(obj.currentDataFile)
obj.saveDataFile;
end
obj.currentDataFile = VideoFile(...
'rootPath',obj.currentDataSetPath,...
'experimentName',obj.currentExperimentName);
end
addFrame2File(obj.currentDataFile,frameData,frameInfo);
catch me
obj.lastError = me;
end
end
end
methods % SET
function set.experimentSyncObj(obj,bhv)
if ~isempty(obj.experimentStateListener)
obj.experimentStateListener.Enabled = false;
end
obj.experimentSyncObj = bhv;
obj.experimentStateListener = addlistener(obj.experimentSyncObj,...
'ExperimentStart',@(src,evnt)experimentStateChangeFcn(obj,src,evnt));
addlistener(obj.experimentSyncObj,...
'ExperimentStop',@(src,evnt)experimentStateChangeFcn(obj,src,evnt));
obj.experimentStateListener.Enabled = true;
end
function set.trialSyncObj(obj,bhv)
obj.trialSyncObj = bhv;
if ~isempty(obj.trialStateListener)
obj.trialStateListener.Enabled = false;
end
obj.trialStateListener = addlistener(obj.trialSyncObj,...
'NewTrial',@(src,evnt)trialStateChangeFcn(obj,src,evnt));
obj.trialStateListener.Enabled = false;
obj.autoSaveFrequency = bhv.autoSaveFrequency;
end
function set.frameSyncObj(obj,cam)
if isa(cam, 'Camera')
if ~isempty(obj.frameSyncListener)
obj.frameSyncListener.Enabled = false;
end
obj.frameSyncObj = cam;
% Define Listener
obj.frameSyncListener = addlistener(obj.frameSyncObj,...
'FrameAcquired',@(src,evnt)frameAcquiredFcn(obj,src,evnt));
obj.frameSyncListener.Enabled = false;
end
end
end
methods % CLEANUP
function delete(obj)
try
obj.saveDataSet();
delete(obj.cameraObj)
catch me
disp(me.message)
end
end
end
end