forked from yihuai-gao/CommandCenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitializeJoystick.m
48 lines (41 loc) · 1.25 KB
/
initializeJoystick.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
function [t, hello] = initializeJoystick()
[t, hello] = connectSmart('localhost');
function callbackFcn(t, ~)
str = t.readline();
if strcmp(str, "FIN")
t.flush();
clear t;
disp('Closed connection.')
else
jsondecode(str)
end
end
function [t, hello] = connectSmart(host)
disp('Trying to connect')
[t, hello] = connect(host);
if strcmp(hello, 'No Server') && strcmp(host, 'localhost')
disp('Starting server')
system('python startjoystick.py');
[t, hello] = connect(host);
end
end
function [t, hello] = connect(host)
try
t = tcpclient(host, 4000, "ConnectTimeout", 1, "Timeout", 1);
hello = t.readline();
if isempty(hello) || strcmp(hello, 'No Joystick')
t.flush();
clear t;
t = [];
if isempty(hello)
hello = 'No Server';
end
else
configureCallback(t, "terminator", @callbackFcn)
end
catch
t = [];
hello = 'No Server';
end
end
end