-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfNIRS_GUI_1.m
212 lines (200 loc) · 7.63 KB
/
fNIRS_GUI_1.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
function varargout = fNIRS_GUI_1(varargin)
% FNIRS_GUI_1 MATLAB code for fNIRS_GUI_1.fig
% FNIRS_GUI_1, by itself, creates a new FNIRS_GUI_1 or raises the existing
% singleton*.
%
% H = FNIRS_GUI_1 returns the handle to a new FNIRS_GUI_1 or the handle to
% the existing singleton*.
%
% FNIRS_GUI_1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FNIRS_GUI_1.M with the given input arguments.
%
% FNIRS_GUI_1('Property','Value',...) creates a new FNIRS_GUI_1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before fNIRS_GUI_1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to fNIRS_GUI_1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help fNIRS_GUI_1
% Last Modified by GUIDE v2.5 14-May-2014 01:46:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fNIRS_GUI_1_OpeningFcn, ...
'gui_OutputFcn', @fNIRS_GUI_1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
end
% --- Executes just before fNIRS_GUI_1 is made visible.
function fNIRS_GUI_1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to fNIRS_GUI_1 (see VARARGIN)
global y t
t = 0;
y = 0;
handles.graph = false;
handles.plot = plot(t,y,'YDataSource','y','XDataSource','t');
handles.inputdata = uint8([]);
handles.outputdata.t = t;
handles.outputdata.y = y;
handles.output = hObject;
handles.counter = 0;
handles.clear_input_flag = false;
handles.clear_input_index = 1;
s1 = serial('COM29','BaudRate',9600,'DataBits',8,'Parity','None','FlowControl','None','StopBits',1);
s1.BytesAvailableFcnCount = 40;
s1.BytesAvailableFcnMode = 'byte';
s1.BytesAvailableFcn = @(src, event) mycallback(src, event, hObject, handles);
handles.serialport = s1;
fopen(s1);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes fNIRS_GUI_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
% --- Outputs from this function are returned to the command line.
function varargout = fNIRS_GUI_1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
end
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
handles.graph = true;
status = get(handles.serialport,'Status');
handles.inputdata = uint8([]); %clear pending data
if~strcmp(status,'open')
%handles.serialport.BytesAvailableFcn = @(src, event) mycallback(src, event, hObject,handles);
fopen(handles.serialport);
end
guidata(hObject, handles);
while handles.graph
testdata = handles.inputdata;
handles = guidata(hObject); %The callback might have changed the values
currentdata = handles.inputdata(handles.clear_input_index:end);
%currentdata = handles.inputdata; %read pending data
t = handles.outputdata.t;
y = handles.outputdata.y;
if handles.clear_input_flag ~= true
handles.clear_input_index = length(currentdata)+1;
handles.clear_input_flag = true;
% Update handles structure
guidata(hObject, handles);
end
%now do something with the currentdata
while ~isempty(currentdata)
start_index = find_start(currentdata);
if(start_index ~= -1 && length(currentdata)>=2+3)
data = uint8(currentdata);
data2=uint16(zeros(1,1));
data2=typecast(data(start_index:start_index+1),'uint16');
y = [y data2'];
temp_time = linspace(t(end),t(end)+1,1);
t = [t temp_time];
currentdata = currentdata(start_index+2:end);
if(handles.counter == 0)
set(handles.plot,'XData',t,'YData',y);
refreshdata(handles.plot,'caller');
drawnow;
handles.counter = 0;
else
handles.counter = handles.counter+1;
end
end
end
% Get a new copy of the latest data and publish our changes to the
% handles
new_handles = guidata(hObject);
% we are only using these but we need to get the latest
% handles.graph
new_handles.outputdata.t = t;
new_handles.outputdata.y = y;
new_handles.counter = handles.counter;
%new_handles.plot = handles.plot;
% handles.plot????
% Update handles structure
guidata(hObject, new_handles);
end
end
function mycallback(src, event, FigureObject,handles)
handles = guidata(FigureObject);
s1 = handles.serialport;
if(handles.clear_input_flag == true)
handles.inputdata = handles.inputdata(handles.clear_input_index:end);
handles.clear_input_index = 1;
handles.clear_input_flag = 0;
guidata(FigureObject, handles);
end
if s1.BytesAVailable~=0
thisdata = fread(s1, s1.BytesAvailable);
handles.inputdata = [handles.inputdata; uint8(thisdata)];
guidata(FigureObject, handles);
end
end
% --- Executes on button press in Stop.
function Stop_Callback(hObject, eventdata, handles)
% hObject handle to Stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.graph = false;
guidata(hObject, handles);
status = get(handles.serialport,'Status');
if strcmp(status,'open')
fclose(handles.serialport);
%handles.serialport.BytesAvailableFcn = @null;
end
guidata(hObject, handles);
%handles.output = get(handles.plot,'XData');
%handles.output = get(handles.plot,'YData');
end
function [start_index] = find_start(data)
start_index = -1;
index = 1;
i = 0;
len = length(data);
while(index <= len && start_index == -1)
switch i
case 0
if(char(data(index)) == '<')
i = 1;
else
i=0;
end
index = index+1;
case 1
if(char(data(index)) == '(')
i = 2;
else
i = 0;
end
index = index+1;
case 2
if(char(data(index)) == '{')
start_index = index+1;
else
i = 0;
index = index+1;
end
end
end
end