-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_breathing.m
126 lines (90 loc) · 2.26 KB
/
monitor_breathing.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
%displays data from bluetooth respiration units in 'real time'
%press Q and ESC simultaneously during plotting to end script
%Dominik Graetz, July 2022
clear all
addpath(genpath(pwd));
%for exiting program
KbName('UnifyKeyNames');
KEYS.Q = KbName('q');
KEYS.ESC = KbName('ESCAPE');
Nsamples = 300; %this is the length of the x axis
dev1 = connectdevice;
another = 'x';
while ~strcmp(another, 'y') && ~strcmp(another, 'n')
another = input('Do you want to connect another device? (y/n) ', 's');
end
%recode another
if strcmp(another, 'y')
another = 1;
elseif strcmp(another, 'n')
another = 0;
end
%connect second device
if another
dev2 = connectdevice;
fopen(dev2);
end
fopen(dev1);
xx = figure('Position', [0 200 1000 1080]);
x = 1:Nsamples;
% y1 = zeros(1, Nsamples-1);
% y2 = zeros(1, Nsamples-1);
y1 = zeros(1, Nsamples);
y2 = zeros(1, Nsamples);
t = GetSecs;
%flush device(s)
while GetSecs - t < 2
fgets(dev1);
if another
fgets(dev2);
end
end
x = 1:Nsamples;
disp('Press Q and ESC simultaneously to end live view.');
iter = 0;
while 1
dontSleep;
iter = iter + 1;
y1 = getDataAndDraw(dev1, x, y1);
if another
y2 = getDataAndDraw(dev2, x, y2);
end
%don't draw every single iteration
if iter == 4
%reference figure
figure(xx);
if another
%if we have two devices connected, make two separate figures
subplot(2,1,1);
end
plot(x, y1);
title(dev1.RemoteName,'Interpreter', 'none'); %Interpreter 'none' prevents title to be ugly (underscore _ is interpreted to mean subscript
if another
subplot(2,1,2);
plot(x, y2);
title(dev2.RemoteName,'Interpreter', 'none');
end
drawnow;
iter = 0; %resets iterations
end
%this is to keep the plot moving
% y1(1) = [];
%
% if another
% y2(1) = [];
% end
[~, ~, keys] = KbCheck;
if keys(KEYS.Q) && keys(KEYS.ESC)
close; %closes figure
disp('Session ended by user.');
fclose(dev1);
delete dev1
clear dev1
if another
fclose(dev2);
delete dev2
clear dev2
end
break
end
end