-
Notifications
You must be signed in to change notification settings - Fork 0
/
netClient.js
137 lines (110 loc) · 3.16 KB
/
netClient.js
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
var net = require('net');
var XboxController = require('xbox-controller');
var xbox = new XboxController();
console.log('started');
var config = {
host: '192.168.1.139',
port: 3000
};
var Client = (function () {
'use strict';
function Client(options) {
// enforces new
if (!(this instanceof Client)) {
return new Client(options);
}
var self = this;
this.client = new net.Socket();
self.button = {
aux1: false,
aux2: false,
aux3: false,
aux4: false
};
self.armed = false;
this.client.connect(options.port, options.host, function () {
self.bindJoy.call(self);
setInterval(function () {
self.sendRaw(105, 105);
}, 1000);
});
this.client.on('data', function (data) {
console.log(JSON.parse(data));
});
}
Client.prototype.sendRaw = function (code, data) {
this.client.write(JSON.stringify({
action: 'raw',
data: [code, data]
}) + '\n');
};
Client.prototype.sendRc = function (data) {
this.client.write(JSON.stringify({
action: 'rc',
data: data
}) + '\n');
};
Client.prototype.bindJoy = function () {
var self = this;
xbox.on('left:move', function (data) {
if (data.x > 30000) {
data.x = 30000;
}
if (data.x < -30000) {
data.x = -30000;
}
self.sendRc({
pitch: parseInt(1500 - data.x / 60, 10)
});
});
xbox.on('y:press', function (key) {
self.aux1 = self.aux1 ? false : true;
self.sendRc({
aux1: self.aux1 ? 1000 : 2000
});
});
xbox.on('b:press', function (key) {
self.aux2 = self.aux1 ? false : true;
self.sendRc({
aux2: self.aux2 ? 1000 : 2000
});
});
xbox.on('a:press', function (key) {
self.aux3 = self.aux3 ? false : true;
self.sendRc({
aux3: self.aux3 ? 1000 : 2000
});
});
xbox.on('x:press', function (key) {
self.aux4 = self.aux4 ? false : true;
self.sendRc({
aux4: self.aux4 ? 1000 : 2000
});
});
xbox.on('right:move', function (data) {
if (data.x > 30000) {
data.x = 30000;
}
if (data.x < -30000) {
data.x = -30000;
}
if (data.y > 30000) {
data.y = 30000;
}
if (data.y < -30000) {
data.y = -30000;
}
self.sendRc({
roll: parseInt(1500 - data.x / 60, 10),
yaw: parseInt(1500 - data.x / 60, 10)
});
});
xbox.on('lefttrigger', function (position) {
self.sendRc({
throttle: parseInt(1000 + (position * 4), 10)
});
});
};
return Client;
}());
new Client(config);