-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
141 lines (114 loc) · 3.22 KB
/
main.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
% START THIS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add folders
addpath(genpath('liblsl-Matlab'));
addpath(genpath('components'));
addpath(genpath('protocols'));
addpath(genpath('ui'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cleanup
close all force; % close old windows
clear % clear workspace
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% globals
global mylsl;
global mysession;
global mydevices;
global myprotocols;
global myselectchannels;
global mysettings;
global myfeedback;
% init globals
mylsl = lsl();
mysession = session();
mydevices = devices();
myprotocols = protocols();
myselectchannels = selectchannels();
mysettings = app();
myfeedback = feedback();
% add listeners to lsl
lhsample = addlistener(mylsl, "NewSample", @onNewSample);
% add listeners to session
lhstart = addlistener(mysession, "Started", @onSessionStarted);
lhstop = addlistener(mysession, "Stopped", @onSessionStopped);
lhwindow = addlistener(mysession, "Window", @onSessionWindow);
lhepoch = addlistener(mysession, "Epoch", @onSessionEpoch);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% main thread loop
while isvalid(mysettings) && isvalid(myfeedback.hFig)
% update components
mylsl.update();
mysettings.update();
mysession.update();
myfeedback.centerBar();
% update ui and run callbacks
drawnow limitrate;
end
% shutdown
if isvalid(mysettings)
delete(mysettings);
end
if isvalid(myfeedback)
delete(myfeedback);
end
% close old windows
close all force;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function onNewSample(src, ~)
global mysession;
mysession.pushSample(src.sample, src.timestamp);
mysession.update();
end
function onSessionStarted(src, ~)
global mylsl;
global myfeedback;
global myprotocols;
mylsl.marker = 0;
mylsl.trigger(100);
myfeedback.showBar();
myprotocols.selected.fh.init();
end
function onSessionStopped(src, ~)
global mylsl;
global myfeedback;
global myprotocols;
mylsl.marker = 0;
mylsl.trigger(101);
myfeedback.setBackground(src.bgcolor);
myfeedback.hideBar();
myprotocols.selected.fh.finish(src);
end
function onSessionEpoch(src, ~)
global mylsl;
global myfeedback;
mylsl.marker = src.marker;
myfeedback.setBackground(src.bgcolor);
if src.fbvisible
myfeedback.showBar();
else
myfeedback.hideBar();
end
end
function onSessionWindow(src, ~)
global myfeedback;
global myprotocols;
prevfeedback = 0.5;
if src.idx > 1
prevfeedback = src.feedback(src.idx-1);
end
prevmarker = 0;
if src.idx > 1
prevmarker = src.markers(src.idx-1);
end
tick = tic();
r = myprotocols.selected.fh.process (...
src.marker, src.srate, ...
src.idx, src.data, ...
src.windownum, src.window, ...
src.windowidx >= src.windowsize, ...
prevfeedback, prevmarker);
span = toc(tick);
r = min(max(r,0.0),1.0);
myfeedback.setFeedback(r);
src.pushFeedback(r, span);
end