-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
opticka.m
2168 lines (2009 loc) · 72.1 KB
/
opticka.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% ======================================================================
%> @class opticka
%> @brief GUI Manager for runExperiment() class
%>
%> Opticka is an object-oriented experiment manager wrapping the Psychophysics
%> toolbox; see http://iandol.github.com/opticka/ for more details. This
%> class builds and controls the GUI that manages interaction with
%> runExperiment and other classes (screenManager, metaStimulus,
%> taskSequence, stateMachine etc.)
%>
%> @todo expose maskStimulus settings in the optickaGUI
%> @todo more flexible tweaking of arduino settings
%>
%> Copyright ©2014-2022 Ian Max Andolina — released: LGPL3, see LICENCE.md
% ======================================================================
classdef opticka < optickaCore
properties (SetAccess = protected, GetAccess = public)
%> version number
optickaVersion char = '2.16.1'
%> is this a remote instance?
remote = false
end
properties
%> this is the main runExperiment object
r runExperiment
%> run in verbose mode?
verbose = false
end
properties (SetAccess = public, GetAccess = public, Transient = true)
%> general store for misc properties
store struct = struct()
%> initialise UI?
initUI logical = true
end
properties (SetAccess = protected, GetAccess = public, Transient = true)
%> all of the handles to the opticka_ui GUI
ui
end
properties (SetAccess = protected, GetAccess = public, Hidden = true)
%> omniplex connection, via TCP
oc
end
properties (SetAccess = private, GetAccess = private)
%> history of display objects
history
%> spash screen handle
ss
%> used to sanitise passed values on construction
allowedProperties = {'verbose','initUI'}
%> which UI settings should be saved locally to the machine?
uiPrefsList cell = {'OKOmniplexIP','OKMonitorDistance','OKpixelsPerCm',...
'OKbackgroundColour','OKAntiAliasing','OKbitDepth','OKUseRetina',...
'OKHideFlash','OKlogFrames','OKlogStateTimers','OKUsePhotoDiode',...
'OKResearcher','OKSubject','OKLabName',...
'OKSessionPrefix','OKLabLocation','OKAlyxIP',...
'OKaudioDevice','OKverbosityLevel',...
'OKarduinoPort','OKarduinoType',...
'OKrewardType','OKTTLPin','OKTTLTime',...
'OKOpenGLBlending','OKWindowSize',...
'OKUseDummy','OKINTANPort', 'OKstrobeOFF',...
'OKELCalibProp','OKELCalibDevice','OKELManualMode','OKELCalibBeep',...
'OKTobiiCal','OKTobiiVal','OKTobiiAddress',...
'OKTobiiManualMode', 'OKTobiiTrackingMode','OKTobiiCalStimulus',...
'OKTobiiTracker','OKTobiiOperatorScreen',...
'OKiRecCal','OKiRecVal','OKiRecAddress','OKiRecTCP','OKiRecUDP',...
'OKiRecCalStim','OKiRecSize','OKiRecMovie'}
end
%=======================================================================
methods %------------------PUBLIC METHODS
%=======================================================================
% ===================================================================
function me = opticka(varargin)
%> @fn opticka
%> @brief Class constructor
%>
%> @param varargin are passed as a structure of properties which is
%> parsed.
%> @return instance of opticka class.
% ===================================================================
args = optickaCore.addDefaults(varargin,struct('name','opticka'));
me=me@optickaCore(args); %superclass constructor
me.parseArgs(args, me.allowedProperties);
if me.cloning == false
if ~exist('OKStartTask_image.png','file'); addOptickaToPath; end
if me.initUI; me.initialiseUI; end
end
end
% ===================================================================
function amIRemote(me)
%> @fn amIRemote
%> @brief Check if we are remote by checking existance of UI
% ===================================================================
if ~ishandle(me.ui.output)
me.remote = true;
end
end
% ===================================================================
function connectToOmniplex(me)
%> @fn connectToOmniplex
%>
%> Gets the settings from the UI and connects to omniplex
% ===================================================================
rPort = me.gn(me.ui.OKOmniplexPort);
rAddress = me.gs(me.ui.OKOmniplexIP);
status = me.ping(rAddress);
if status > 0
set(me.ui.OKOmniplexStatus,'Value','Omniplex: machine ping ERROR!');
errordlg('Cannot ping Omniplex machine, please ensure it is connected!!!');
error('Cannot ping Omniplex, please ensure it is connected!!!');
end
if isempty(me.oc)
in = struct('verbosity',0,'rPort',rPort,'rAddress',rAddress,'protocol','tcp');
me.oc = dataConnection(in);
else
me.oc.rPort = me.gn(me.ui.OKOmniplexPort);
me.oc.rAddress = me.gs(me.ui.OKOmniplexIP);
end
if me.oc.checkStatus < 1
loop = 1;
while loop <= 10
me.oc.close('conn',1);
fprintf('\nTrying to connect...\n');
me.oc.open;
if me.oc.checkStatus > 0
break
end
pause(0.1);
end
me.oc.write('--ping--');
loop = 1;
while loop < 8
in = me.oc.read(0);
fprintf('\n{opticka said: %s}\n',in);
if regexpi(in,'(opened|ping)')
fprintf('\nWe can ping omniplex master on try: %d\n',loop);
set(me.ui.OKOmniplexStatus,'Value','Omniplex: connected via TCP');
break
else
fprintf('\nOmniplex master not responding, try: %d\n',loop);
set(me.ui.OKOmniplexStatus,'Value','Omniplex: not responding');
end
loop=loop+1;
pause(0.2);
end
%drawnow;
end
end
% ===================================================================
function sendOmniplexStimulus(me,sendLog)
%> @fn sendOmniplexStimulus
%>
%> Gets the settings from the UI and connects to omniplex.
%>
%> @param sendLog send the run log [default = false]
% ===================================================================
if ~exist('sendLog','var')
sendLog = false;
end
if me.oc.checkStatus > 0
%flush read buffer
data = me.oc.read('all');
tLog=[];
if me.oc.checkStatus > 0 %check again to make sure we are still open
me.oc.write('--readStimulus--');
pause(0.25);
tt=tic;
if sendLog == false
if ~isempty(me.r.runLog);tLog = me.r.runLog;end
me.r.deleteRunLog; %so we don't send too much data over TCP
end
tmpobj=me.r;
me.oc.writeVar('o',tmpobj);
if sendLog == false
if ~isempty(tLog);me.r.restoreRunLog(tLog);end
end
fprintf('>>>Opticka: It took %g seconds to write and send stimulus to Omniplex machine\n',toc(tt));
loop = 1;
while loop < 10
in = me.oc.read(0);
fprintf('\n{omniplex said: %s}\n',in);
if regexpi(in,'(stimulusReceived)')
set(me.ui.OKOmniplexStatus,'Value','Omniplex: connected+stimulus received');
break;
elseif regexpi(in,'(stimulusFailed)')
set(me.ui.OKOmniplexStatus,'Value','Omniplex: connected, stimulus ERROR!');
end
loop=loop+1;
pause(0.2);
end
end
end
end
end % END PUBLIC METHODS
%========================================================
methods (Hidden = true) %these have to be available publically, but lets hide them from obvious view
%========================================================
% ===================================================================
function initialiseUI(me)
%> @fn initialiseUI
%>
%> @brief Start the UI
% ===================================================================
try
tt = tic;
jv = version('-java');
if contains(jv,'not enabled');isjava=false;else;isjava=true;end
if isjava
me.ss = SplashScreen(['Opticka V' me.optickaVersion],'opticka.png');
if isdeployed
me.ss.addText( 10, 30, ['Loading Opticka [D] V' me.optickaVersion '…'], 'FontSize', 20, 'Color', [1 0.8 0.5] )
else
me.ss.addText( 10, 30, ['Loading Opticka V' me.optickaVersion '…'], 'FontSize', 20, 'Color', [1 0.8 0.5] )
end
end
me.paths.filename = mfilename;
me.paths.whereami = fileparts(which(mfilename));
me.paths.startServer = [me.paths.whereami filesep 'udpserver' filesep 'launchDataConnection'];
if ismac
me.store.serverCommand = ['!osascript -e ''tell application "Terminal"'' -e ''activate'' -e ''do script "matlab -nodesktop -r \"runServer\""'' -e ''end tell'''];
else
me.store.serverCommand = '!matlab -nodesktop -nosplash -r "d=dataConnection(struct(''autoServer'',1,''lPort'',5678));" &';
end
me.paths.temp=tempdir;
if ~isfield(me.paths,'protocols')
me.paths.protocols = [me.paths.parent filesep 'Protocols'];
if ~isfolder(me.paths.protocols); mkdir(me.paths.protocols); end
end
try cd(me.paths.protocols); end
me.paths.currentPath = pwd;
if ~isfield(me.paths,'calibration')
me.paths.calibration = [me.paths.parent filesep 'Calibration'];
if ~isfolder(me.paths.calibration); mkdir(me.paths.calibration); end
end
if ~isfield(me.paths,'historypath')
me.paths.historypath = [me.paths.parent filesep 'History'];
if ~isfolder(me.paths.historypath); mkdir(me.paths.historypath); end
end
if ~isfield(me.paths,'savedData')
me.paths.savedData = [me.paths.parent filesep 'SavedData'];
if ~isfolder(me.paths.savedData); mkdir(me.paths.savedData); end
end
if ismac || isunix
if ~isdeployed && ~exist([me.paths.parent filesep 'Protocols' filesep 'CoreProtocols'],'dir')
src = [me.paths.whereami filesep 'CoreProtocols'];
dst = [me.paths.parent filesep 'Protocols' filesep];
cmd = ['!ln -s ' src ' ' dst];
eval(cmd);
end
end
me.ui = opticka_ui(me); %our GUI file
me.store.protocolsPath = me.paths.protocols;
loadPrefs(me);
getScreenVals(me);
getTaskVals(me);
loadCalibration(me);
me.ui.getEyetrackerSettings();
if exist([me.paths.root filesep 'DefaultStateInfo.m'],'file')
me.paths.stateInfoFile = [me.paths.root filesep 'DefaultStateInfo.m'];
me.r.stateInfoFile = me.paths.stateInfoFile;
elseif ~isdeployed
me.paths.stateInfoFile = [me.paths.whereami filesep 'DefaultStateInfo.m'];
me.r.stateInfoFile = me.paths.stateInfoFile;
end
if exist([me.store.protocolsPath filesep 'userFunctions.m'],'file')
me.r.userFunctionsFile = [me.paths.protocols filesep 'userFunctions.m'];
elseif ~isdeployed
me.r.userFunctionsFile = [me.paths.whereami filesep 'userFunctions.m'];
end
fprintf('===>>> Opticka UI took %.2fsecs to initialise\n',toc(tt));
try if ~isempty(me.ss); pause(0.1); delete(me.ss); me.ss = []; end; end
catch ME
try if ~isempty(me.ss); delete(me.ss); me.ss = []; end; end
warning('Problem initialising Opticka UI, please check errors on the commandline!');
try delete(me.ui.OKRoot);end %#ok<*TRYNC>
try me.ui = []; end
rethrow(ME);
end
end
% ===================================================================
function getScreenVals(me)
%> @fn getScreenVals
%>
%> Gets the settings from the UI and updates our runExperiment
%> object.
% ===================================================================
rM = initialiseGlobals(me);
if isempty(me.r)
if ~isdeployed || ~ismcc
olds = me.ui.OKOptickaVersion.Text;
me.ui.OKOptickaVersion.Text = 'Initialising Stimulus and Task objects...';
%drawnow
end
me.r = runExperiment();
me.r.optickaVersion = me.optickaVersion;
initialise(me.r); % set up the runExperiment object
s=cell(me.r.screen.maxScreen+1,1);
for i=0:me.r.screen.maxScreen
s{i+1} = num2str(i);
end
if (~isdeployed || ~ismcc) && ~isempty(s)
me.ui.OKSelectScreen.Items = s;
me.ui.OKSelectScreen.Value = s{end};
clear s;
me.ui.OKOptickaVersion.Text = olds;
end
end
rM.board = me.gv(me.ui.OKarduinoType);
if ~isempty(me.gv(me.ui.OKarduinoPort))
rM.port = me.gv(me.ui.OKarduinoPort);
end
rM.reward.type = me.gv(me.ui.OKrewardType);
rM.reward.pin = me.gv(me.ui.OKTTLPin);
rM.reward.time = me.gv(me.ui.OKTTLTime);
me.r.reward.port = me.gv(me.ui.OKarduinoPort);
me.r.reward.board = me.gv(me.ui.OKarduinoType);
me.r.askForComments = me.gl(me.ui.OKAskComments);
me.r.sessionData.subjectName = me.gv(me.ui.OKSubject);
me.r.sessionData.researcherName = me.gv(me.ui.OKResearcher);
me.r.sessionData.alyxIP = me.gv(me.ui.OKAlyxIP);
me.r.sessionData.labName = me.gv(me.ui.OKLabName);
me.r.sessionData.labLocation = me.gv(me.ui.OKLabLocation);
me.r.sessionData.sessionPrefix = me.gv(me.ui.OKSessionPrefix);
me.r.audioDevice = me.gn(me.ui.OKaudioDevice);
me.r.screen.screen = me.gd(me.ui.OKSelectScreen);
me.r.screen.distance = me.gd(me.ui.OKMonitorDistance);
me.r.screen.pixelsPerCm = me.gd(me.ui.OKpixelsPerCm);
me.r.screen.screenXOffset = me.gd(me.ui.OKscreenXOffset);
me.r.screen.screenYOffset = me.gd(me.ui.OKscreenYOffset);
me.r.screen.srcMode = me.gv(me.ui.OKGLSrc);
me.r.screen.dstMode = me.gv(me.ui.OKGLDst);
me.r.screen.bitDepth = me.gv(me.ui.OKbitDepth);
me.r.screen.blend = me.gv(me.ui.OKOpenGLBlending);
me.r.screen.verbosityLevel = me.gd(me.ui.OKverbosityLevel);
value = me.gp(me.ui.OKUseGamma);
if isprop(me.r.screen,'gammaTable') && isa(me.r.screen.gammaTable,'calibrateLuminance') && ~isempty(me.r.screen.gammaTable)
me.r.screen.gammaTable.choice = value - 1;
end
s=str2num(me.gv(me.ui.OKWindowSize)); %#ok<ST2NM>
if isempty(s)
me.r.screen.windowed = false;
else
me.r.screen.windowed = s;
end
me.r.logFrames = me.gl(me.ui.OKlogFrames);
me.r.logStateTimers = me.gl(me.ui.OKlogStateTimers);
me.r.benchmark = me.gl(me.ui.OKbenchmark);
me.r.screen.hideFlash = me.gl(me.ui.OKHideFlash);
me.r.screen.useRetina = me.gl(me.ui.OKUseRetina);
if strcmpi(me.r.screen.bitDepth,'8bit')
%me.ui.OKAntiAliasing.Value = '0';
end
me.r.screen.antiAlias = me.gd(me.ui.OKAntiAliasing);
me.r.photoDiode = me.gl(me.ui.OKUsePhotoDiode);
me.r.screen.movieSettings.record = me.gl(me.ui.OKrecordMovie);
me.r.verbose = me.gl(me.ui.OKVerbose); %set method
me.verbose = me.r.verbose;
me.r.screen.debug = me.gl(me.ui.OKDebug);
me.r.debug = me.r.screen.debug;
me.r.screen.disableSyncTests = ~me.gl(me.ui.OKSync);
me.r.diaryMode = me.gl(me.ui.OKDiaryMode);
me.r.screen.visualDebug = me.r.screen.debug;
me.r.screen.backgroundColour = me.gn(me.ui.OKbackgroundColour);
try me.r.screen.useVulkan = me.gl(me.ui.OKuseVulkan); end
me.r.control.port = me.ui.OKINTANPort.Value;
if me.ui.OKControlIntan.Checked == true
me.r.control.device = 'intan';
else
me.r.control.device = '';
end
me.r.strobe.mode = me.ui.OKstrobeMode.Value;
me.r.strobe.stimOFFValue = me.ui.OKstrobeOFF.Value;
if me.ui.OKuseLabJackTStrobe.Checked == true
me.r.strobe.device = 'labjackt';
elseif me.ui.OKuseLabJackStrobe.Checked == true
me.r.strobe.device = 'labjack';
elseif me.ui.OKuseDataPixx.Checked == true
me.r.strobe.device = 'datapixx';
elseif me.ui.OKuseDisplayPP.Checked == true
me.r.strobe.device = 'display++';
elseif me.ui.OKUseNirSmart.Checked == true
me.r.strobe.device = 'nirsmart';
else
me.r.strobe.device = '';
end
if me.ui.OKuseArduino.Checked == true
me.r.reward.device = 'arduino';
elseif me.ui.OKuseLabJackReward.Checked == true
me.r.reward.device = 'labjack';
me.r.reward.port = '';
me.r.reward.board = '';
else
me.r.reward.device = '';
me.r.reward.port = '';
me.r.reward.board = '';
end
me.r.eyetracker.dummy = logical(me.ui.OKUseDummy.Checked);
if me.ui.OKuseIRec2HS.Checked == true
me.r.eyetracker.device = 'irec';
elseif me.ui.OKuseEyelink.Checked == true
me.r.eyetracker.device = 'eyelink';
elseif me.ui.OKuseTobii.Checked == true
me.r.eyetracker.device = 'tobii';
else
me.r.eyetracker.device = '';
end
end
% ===================================================================
function getTaskVals(me, randomise)
%> @fn getTaskVals
%>
%> Gets the settings from the UI and updates our task object.
%>
%> @param randomise do we run randomiseTask()? [default=FALSE]
% ===================================================================
if ~exist('randomise','var'); randomise = true; end
if isempty(me.r.task)
me.r.task = taskSequence;
me.r.task.initialise;
end
if isfield(me.r.screenVals,'fps')
me.r.task.fps = me.r.screenVals.fps;
end
me.r.task.trialTime = me.gd(me.ui.OKtrialTime);
me.r.task.randomSeed = me.gn(me.ui.OKRandomSeed);
me.r.task.randomGenerator = me.gs(me.ui.OKrandomGenerator);
me.r.task.ibTime = me.gn(me.ui.OKibTime);
me.r.task.randomise = me.gl(me.ui.OKRandomise);
me.r.task.isTime = me.gn(me.ui.OKisTime);
me.r.task.nBlocks = me.gd(me.ui.OKnBlocks);
me.r.task.realTime = me.gl(me.ui.OKrealTime);
if ~isempty(me.r.task.blockVar)
me.r.task.blockVar.values = me.ge(me.ui.OKBlockValues);
me.r.task.blockVar.probability = me.gn(me.ui.OKBlockProbability);
if length(me.r.task.blockVar.values) ~= length(me.r.task.blockVar.probability)
randomise = false;
end
end
if ~isempty(me.r.task.trialVar)
me.r.task.trialVar.values = me.ge(me.ui.OKTrialValues);
me.r.task.trialVar.probability = me.gn(me.ui.OKTrialProbability);
if length(me.r.task.trialVar.values) ~= length(me.r.task.trialVar.probability)
randomise = false;
end
end
if isempty(me.r.task.taskStream); me.r.task.initialiseGenerator; end
if randomise && me.r.task.nVars > 0; me.r.task.randomiseTask; end
end
% ===================================================================
function getStateInfo(met, kind)
%> @fn getStateInfo -- NOTE: to load the state info file we need to
%> change the name of SELF from 'me' as this is what is used within
%> runExperiment. In this case me is a fake self just to load state info
%> file.
%>
%> Load the state info and user function files into the UI.
% ===================================================================
if ~exist('kind','var'); kind = 'b'; end
if contains(kind,{'b','s'}) && ~isempty(met.r.stateInfoFile) && ischar(met.r.stateInfoFile)
if ~exist(met.r.stateInfoFile,'file')
if ~isempty(regexpi(met.r.stateInfoFile,'^\w:\\', 'once')) %is it a windows path?
f = split(met.r.stateInfoFile,'\');
f = f{end};
else
[~,f,e] = fileparts(met.r.stateInfoFile);
f = [f e];
end
met.r.stateInfoFile = [pwd filesep f];
end
if exist(met.r.stateInfoFile,'file')
o.store.statetext = {};
fid = fopen(met.r.stateInfoFile);
tline = fgetl(fid);
i=1;
while ischar(tline)
tline = regexprep(tline,'\t',' ');
o.store.statetext{i} = tline;
tline = fgetl(fid);
i=i+1;
end
fclose(fid);
set(met.ui.OKTrainingText,'Value',o.store.statetext);
set(met.ui.OKTrainingFileName,'Text',['State-File: ' met.r.stateInfoFile]);
try
stims = metaStimulus;
me = runExperiment;
eT = eyelinkManager;
run(met.r.stateInfoFile)
if exist('stateInfoTmp','var')
stateInfoTmp{1,1} = 'STATE';
met.ui.OKStateTable.ColumnName = stateInfoTmp(1,:); %#ok<*USENS>
met.ui.OKStateTable.Data = cell2table(stateInfoTmp(2:end,:));
met.ui.OKStateFcnView.Value = {''};
end
clear me stims eT
catch ME
getReport(ME);
met.ui.OKStateTable.ColumnName = {'STATE','Next','time','entry','within','transition','exit'}; %#ok<*USENS>
met.ui.OKStateTable.Data = cell2table(cell(7,7));
met.ui.OKStateFcnView.Value = {'Error loading state info',ME.message};
end
else
set(met.ui.OKTrainingText,'Value','');
set(met.ui.OKTrainingFileName,'Text','No File Specified...');
end
end
if contains(kind,{'b','f'}) && ~isempty(met.r.userFunctionsFile) && exist(met.r.userFunctionsFile,'file')
o.store.usertext = {};
fid = fopen(met.r.userFunctionsFile);
tline = fgetl(fid);
i=1;
while ischar(tline)
tline = regexprep(tline,'\t',' ');
o.store.usertext{i} = tline;
tline = fgetl(fid);
i=i+1;
end
fclose(fid);
set(met.ui.OKFunctionsText,'Value',o.store.usertext);
set(met.ui.OKFunctionsFileName,'Text',['User-Functions File:' met.r.userFunctionsFile]);
elseif ~exist(met.r.userFunctionsFile,'file')
set(met.ui.OKFunctionsText,'Value','');
set(met.ui.OKFunctionsFileName,'Text','No Valid File Specified...');
end
end
% ===================================================================
function clearStimulusList(me)
%> @fn clearStimulusList
%> Erase any stimuli in the list.
%> @param
% ===================================================================
if ~isempty(me.r)
if isempty(me.r.stimuli)
me.r.stimuli = metaStimulus();
end
end
fn = fieldnames(me.store);
for i = 1:length(fn)
if isa(me.store.(fn{i}),'baseStimulus')
try closePanel(me.store.(fn{i})); end
try me.store = rmfield(me.store, fn{i}); end
end
end
ch = get(me.ui.OKPanelStimulus,'Children');
for i = 1:length(ch)
if strcmpi(get(ch(i),'Type'),'uipanel')
delete(ch(i));
end
end
me.ui.OKStimList.Items = {}; me.ui.OKStimList.Value = {};
end
% ===================================================================
function clearVariableList(me)
%> @fn getScreenVals
%> Gets the settings from th UI and updates our runExperiment object
%> @param
% ===================================================================
if ~isempty(me.r)
if ~isempty(me.r.task) && me.r.task.nVars > 0
me.r.task = taskSequence();
end
end
refreshVariableList(me);
end
% ===================================================================
function addStimulus(me)
%> @fn addStimulus
%> Run when we've added a new stimulus
%> @param
% ===================================================================
me.refreshStimulusList;
nidx = me.r.stimuli.n;
me.ui.OKStimList.Value = me.ui.OKStimList.Items{end};
if isfield(me.store,'evnt') %delete our previous event
delete(me.store.evnt);
me.store = rmfield(me.store,'evnt');
end
me.store.evnt = addlistener(me.r.stimuli{nidx},'readPanelUpdate',@me.readPanel);
if isfield(me.store,'visibleStimulus')
if ~strcmp(me.r.stimuli{nidx}.uuid,me.store.visibleStimulus.uuid) || ~me.store.visibleStimulus.isGUI
me.store.visibleStimulus.closePanel();
makePanel(me.r.stimuli{nidx},me.ui.OKPanelStimulus);
me.store.visibleStimulus = me.r.stimuli{nidx};
else
me.store.visibleStimulus.showPanel;
end
else
makePanel(me.r.stimuli{nidx},me.ui.OKPanelStimulus);
me.store.visibleStimulus = me.r.stimuli{nidx};
end
end
% ===================================================================
function deleteStimulus(me)
%> @fn deleteStimulus
%>
%> @param
% ===================================================================
if ~isempty(me.r.stimuli.n) && me.r.stimuli.n > 0
v=me.gp(me.ui.OKStimList);
if isfield(me.store,'visibleStimulus')
if strcmp(me.store.visibleStimulus.uuid,me.r.stimuli{v}.uuid)
closePanel(me.r.stimuli{v});
me.store.visibleStimulus = [];
end
end
me.r.stimuli(v) = [];
if me.r.stimuli.n > 0
v = v - 1;
if v == 0; v = 1; end
me.ui.OKStimList.Value = me.ui.OKStimList.Items{v};
me.store.visibleStimulus = me.r.stimuli{v};
if ~isempty(me.store.visibleStimulus) && ~me.store.visibleStimulus.isGUI
makePanel(me.r.stimuli{v},me.ui.OKPanelStimulus);
me.store.visibleStimulus = me.r.stimuli{v};
else
me.store.visibleStimulus.showPanel;
end
end
me.refreshStimulusList;
end
end
% ===================================================================
function readPanel(me, src, varargin)
%> @fn readPanel
%>
%> @param src source object
%> @param varargin
% ===================================================================
me.salutation('readPanel', ['Triggered by: ' src.fullName], true);
me.refreshStimulusList;
end
% ===================================================================
function editStimulus(me)
%> @fn editStimulus
%> Gets the settings from the UI and updates our runExperiment
%> object.
% ===================================================================
if me.r.stimuli.n > 0
skip = false;
if ~isfield(me.store, 'visibleStimulus') || ~isa(me.store.visibleStimulus, 'baseStimulus')
v = 1;
me.store.visibleStimulus = me.r.stimuli{1};
else
v = me.gp(me.ui.OKStimList);
if isempty(v) || v == 0; v = 1; end
if strcmpi(me.r.stimuli{v}.uuid, me.store.visibleStimulus.uuid)
skip = true;
end
end
if v <= me.r.stimuli.n && ~skip
if isfield(me.store, 'evnt')
delete(me.store.evnt);
me.store = rmfield(me.store, 'evnt');
end
if me.r.stimuli{v}.isGUI
hidePanel(me.store.visibleStimulus);
showPanel(me.r.stimuli{v});
else
hidePanel(me.store.visibleStimulus);
makePanel(me.r.stimuli{v}, me.ui.OKPanelStimulus);
end
me.store.evnt = addlistener(me.r.stimuli{v}, 'readPanelUpdate', @me.readPanel);
me.store.visibleStimulus = me.r.stimuli{v};
refreshStimulusList(me);
end
end
end
% ===================================================================
function modifyStimulus(me)
%> @fn modifyStimulus
%> Gets the settings from the UI and updates our runExperiment object
%> @param
% ===================================================================
me.refreshStimulusList;
end
% ===================================================================
%> @brief addVariable
%> Gets the settings from th UI and updates our runExperiment object
%> @param
% ===================================================================
function addVariable(me)
validate(me.r.task);
revertN = me.r.task.nVars;
try
me.r.task.nVar(revertN+1).name = me.gs(me.ui.OKVariableName);
s = me.gs(me.ui.OKVariableValues);
if isempty(regexpi(s,'^\{'))
me.r.task.nVar(revertN+1).values = str2num(s);
else
me.r.task.nVar(revertN+1).values = eval(s);
end
me.r.task.nVar(revertN+1).stimulus = me.gn(me.ui.OKVariableStimuli);
offset = eval(['{' me.ui.OKVariableOffset.Value '}']);
if isempty(offset) || (iscell(offset) && isempty(offset{1}))
me.r.task.nVar(revertN+1).offsetstimulus = [];
me.r.task.nVar(revertN+1).offsetvalue = [];
else
me.r.task.nVar(revertN+1).offsetstimulus = offset{1};
me.r.task.nVar(revertN+1).offsetvalue = offset{2};
end
try
me.r.task.randomiseTask;
validate(me.r.task);
catch
warndlg('There is a problem with the stimulus variables, please check!')
end
me.refreshVariableList;
catch ME
getReport(ME)
rethrow(ME);
end
end
% ===================================================================
%> @brief updateVariable
%> Gets the values from the UI and updates that var
%> @param
% ===================================================================
function updateVariable(me)
try
pos = me.gp(me.ui.OKVarList);
if isempty(pos) || pos == 0; return; end
me.r.task.nVar(pos).name = me.gs(me.ui.OKVariableName);
s = me.gs(me.ui.OKVariableValues);
if isempty(regexpi(s,'^\{', 'once'))
me.r.task.nVar(pos).values = str2num(s);
else
me.r.task.nVar(pos).values = eval(s);
end
me.r.task.nVar(pos).stimulus = me.gn(me.ui.OKVariableStimuli);
offset = eval(['{' me.ui.OKVariableOffset.Value '}']);
if isempty(offset) || (iscell(offset) && isempty(offset{1}))
me.r.task.nVar(pos).offsetstimulus = [];
me.r.task.nVar(pos).offsetvalue = [];
else
me.r.task.nVar(pos).offsetstimulus = offset{1};
me.r.task.nVar(pos).offsetvalue = offset{2};
end
try
me.r.task.randomiseTask;
validate(me.r.task);
catch
warndlg('There is a problem with the stimulus variables, please check!')
end
me.refreshVariableList;
catch ME
getReport(ME);
rethrow(ME);
end
end
% ===================================================================
%> @brief editVariable
%> Gets the settings from the UI and updates our runExperiment object
%> @param
% ===================================================================
function editVariable(me)
if isobject(me.r.task) && me.r.task.nVars > 0
pos = me.gp(me.ui.OKVarList);
if isempty(pos);pos = 1; end
me.ui.OKVariableName.Value = me.r.task.nVar(pos).name;
v=me.r.task.nVar(pos).values;
if iscell(v)
v = me.cellAsString(v);
else
v = num2str(me.r.task.nVar(pos).values);
end
str = v;
str = regexprep(str,'\s+',' ');
me.ui.OKVariableValues.Value = str;
str = num2str(me.r.task.nVar(pos).stimulus);
str = regexprep(str,'\s+',' ');
me.ui.OKVariableStimuli.Value = str;
if isnumeric(me.r.task.nVar(pos).offsetvalue)
str=[num2str(me.r.task.nVar(pos).offsetstimulus) '; ' num2str(me.r.task.nVar(pos).offsetvalue)];
else
str=[num2str(me.r.task.nVar(pos).offsetstimulus) '; ''' me.r.task.nVar(pos).offsetvalue ''''];
end
me.ui.OKVariableOffset.Value = str;
end
end
% ===================================================================
%> @brief deleteVariable
%> Gets the settings from the UI and updates our runExperiment object
%> @param
% ===================================================================
function deleteVariable(me)
if isobject(me.r.task)
nV = me.r.task.nVar;
pos = me.gp(me.ui.OKVarList);
if isempty(pos) || pos < 1; return; end
if pos <= me.r.task.nVars
nV(pos)=[];
me.r.task.nVar = [];
me.r.task.nVar = nV;
if me.r.task.nVars > 0
me.r.task.randomiseTask;
end
end
me.refreshVariableList;
end
end
% ===================================================================
%> @brief copyVariable
%> Gets the settings from the UI and updates our runExperiment object
%> @param
% ===================================================================
function copyVariable(me)
if isobject(me.r.task)
val = me.gp(me.ui.OKVarList);
me.r.task.nVar(end+1)=me.r.task.nVar(val);
me.refreshVariableList;
end
end
% ===================================================================
%> @brief Load calibration file, better that this is manual...
%>
% ===================================================================
function loadCalibration(me)
d = dir(me.paths.calibration);
for i = 1:length(d)
if isempty(regexp(d(i).name, '^\.+', 'once')) && d(i).isdir == false && d(i).bytes > 0
ftime(i) = d(i).datenum;
else
ftime(i) = 0;
end
end
if max(ftime) > 0
[~,idx]=max(ftime);
disp(['===>>> Opticka has found a potential calibration file: ' [me.paths.calibration filesep d(idx).name]]);
%tmp = load([me.paths.calibration filesep d(idx).name]);
%if isstruct(tmp)
% fn = fieldnames(tmp);
% tmp = tmp.(fn{1});
%end
%if isa(tmp,'calibrateLuminance')
% tmp.filename = [me.paths.calibration filesep d(idx).name];
% if isa(me.r,'runExperiment') && isa(me.r.screen,'screenManager')
% me.r.screen.gammaTable = tmp;
% me.h.OKUseGamma.Items =[ {'None'}; {'Gamma'}; me.r.screen.gammaTable.analysisMethods{:}']';
% me.r.screen.gammaTable.choice = 2;
% end
%end
end
end
% ===================================================================
%> @brief
%>
% ===================================================================
function saveCalibration(me)
if isa(me.r.screen.gammaTable, 'calibrateLuminance')
saveThis = true;
tmp = me.r.screen.gammaTable;
d = dir(me.paths.calibration);
for i = 1:length(d)
if isempty(regexp(d(i).name, '^\.+', 'once')) && d(i).isdir == false && d(i).bytes > 0
if strcmp(d(i).name, tmp.filename)
saveThis = false;
end
end
end
if saveThis == true
save([me.paths.calibration filesep 'calibration-' date], 'tmp');
end
end
end
% ===================================================================
%> @brief loadPrefs Load prefs better left local to the machine
%>
% ===================================================================
function loadPrefs(me)
if ~ispref('opticka'); return; end
anyLoaded = false; prefnames = '';
for i = 1:length(me.uiPrefsList)
prfname = me.uiPrefsList{i};
if ispref('opticka',prfname) %pref exists
if isprop(me.ui, prfname) %ui widget exists
myhandle = me.ui.(prfname);
prf = getpref('opticka', prfname);
uiType = myhandle.Type;
thisVal = '';
switch uiType
case 'uieditfield'
if ischar(prf)
myhandle.Value = prf;
thisVal = prf;
else
myhandle.Value = num2str(prf);
thisVal = myhandle.Value;
end
case 'uicheckbox'
if islogical(prf) || isnumeric(prf)
myhandle.Value = prf;
thisVal = num2str(prf);
end
case 'uidropdown'
str = myhandle.Items;
if ischar(prf) && any(contains(prf, str))
myhandle.Value = prf;
thisVal = prf;
end
case 'uimenu'
myhandle.Checked = prf;
thisVal = char(prf);
case 'uirockerswitch'
if strcmpi(prf,'on') || strcmpi(prf,'off')
myhandle.Value = prf;
thisVal = prf;
end
end
prefnames = [prefnames ' ' prfname '«' thisVal '»'];
if ~mod(i,4);prefnames = [prefnames '\n']; end
if ~anyLoaded; anyLoaded = true; end
end
end
end
if anyLoaded
fprintf('\n===>>> Opticka Load Preferences:\n'); fprintf(prefnames); fprintf('\n');
end
end
% ===================================================================
%> @brief savePrefs Save prefs better left local to the machine
%>
% ===================================================================
function savePrefs(me)
if ispref('opticka'); rmpref('opticka'); end