-
Notifications
You must be signed in to change notification settings - Fork 5
/
readstream.m
64 lines (58 loc) · 1.76 KB
/
readstream.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
function dataVector = readstream(ip, port)
% READSTREAM Read OpenVibe TCP/IP EEG data stream.
% readstream() use default localhost:5678 address with
% plotting on (ploton - true/false boolean).
%
% readstream(ip, port, graph) use custom ip address (ip - string),
% port (port - integer).
%
% To map the LEDs color see LEDTOPO
%
% Michael Tesar <michtesar@gmail.com>
% 2017
if nargin < 1
ip = 'localhost';
port = 5678;
end
try
t = tcpclient(ip, port);
catch ME
warning('Cannot reach TCP/IP communication!');
rethrow(ME);
end
% Header format is 32 bytes from the start as
% documentation of OpenVibe says:
% http://openvibe.inria.fr/documentation/1.2.0/Doc_BoxAlgorithm_TCPWriter.html
% Format version
% Endianness
% Sampling frequency
% Number of chunks
% Samples per chunk
% Reserved0
% Reserved1
% Reserved2
% Start communication with Arduino
arduino = serial('COM3', 'BAUD', 9600);
fopen(arduino);
pause(3);
header = read(t, 8, 'uint32');
if ~isempty(header)
while true
% FIX-ME: THere are 8 values e.g. 32 bytes. mabye this should be fixed.
buffer = read(t, header(5) * header(4), 'double');
stream = reshape(buffer, [header(4), header(5)])';
plot(stream);
dataVector = mean(stream, 1);
% Print to user
disp('OpenVibe stream mean:'); disp(dataVector);
disp('OpenVibe stream color packet'); disp(ledtopo(dataVector));
% Sending values to Arduino to led LEDs
disp('This goes to Arduino'); disp(ledlights(ledtopo(dataVector)));
try
colorArray = sprintf('%d, ', ledlights(ledtopo(dataVector)));
fprintf(arduino, [colorArray(1:end-1), '\n']);
catch
fclose(arduino);
end
end
end