-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
233 lines (201 loc) · 7.81 KB
/
main.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"use strict";
//if(!!navigator.getGamepads){
// // Browser supports the Gamepad API
// var gamepads = navigator.getGamepads();
// console.log(gamepads);
//}
var channels = [
{name: "Start (normal)"},
{name: "Start (spot)"},
{name: "Docking"},
{name: "Left wheel"},
{name: "Right wheel"},
{name: "Vacuum"},
{name: "Main brush"},
{name: "Side brushes"},
// {name: "Left brush"},
// {name: "Right brush"},
];
var mixes = [];
var controls = {};
var newControl = null;
var socket = new WebSocket("ws://" + location.host + "/websocket");
window.onbeforeunload = function(e) { socket.close(); };
socket.onmessage = function(msg) {
//console.log(msg);
var data = JSON.parse(msg.data);
if (data.c == 'info') {
Object.keys(data).forEach(function(key) {
if (key == 'c')
return;
var field = document.getElementById('field-' + key);
if (field !== null) {
if (key == 'bandwidth-in' || key == 'bandwidth-out')
field.textContent = (data[key] / 1000).toFixed(2); // kB/s
else if (key == 'battery-status')
field.textContent = (data[key] * 100).toFixed(2); // %
else if (key == 'battery-voltage' || key == 'battery-current' || key == 'battery-temperature')
field.textContent = (data[key] * 1).toFixed(2);
else
field.textContent = data[key];
} else {
logVisibly('[HTML] Missing “' + key + '” status field.');
}
});
} else if (data.c == 'log') {
logVisibly(data.str);
}
};
function logVisibly(str) {
var logDiv = document.getElementById('log');
if (document.activeElement != logDiv) {
logDiv.innerHTML += str + '\n';
logDiv.scrollTop = logDiv.scrollHeight;
} else {
var start = logDiv.selectionStart;
var end = logDiv.selectionEnd;
logDiv.innerHTML += str + '\n';
logDiv.setSelectionRange(start, end);
}
}
function canGame() {
return "getGamepads" in navigator;
}
function send(mix) {
var msg = {
control: mix.channel.name,
value: mix.finalValue,
};
socket.send(JSON.stringify(msg));
//console.log(JSON.stringify(msg));
}
function setControl(submix) {
newControl = submix;
}
function handle(submix, value) {
submix.value = value;
submix.valueLabel.innerHTML = value.toFixed(2);
processMix(submix['parent']);
}
function processMix(mix) {
var finalValue = 0;
mix.submixes.forEach(function(submix) {
submix.result = submix.value * submix.invertMult
* submix.weight + submix.shift; // TODO expo
submix.resultLabel.innerHTML = submix.result.toFixed(2);
finalValue += submix.result;
});
mix.finalValue = finalValue;
mix.finalValueLabel.innerHTML = finalValue.toFixed(2);
send(mix);
}
function newMix(channel) {
// Ouch! This is too verbose.
var submix = {value: 0, invertMult: 1, weight: 1, shift: 0, expo: 0, result: 0,
inputLabel: null, valueLabel: null, resultLabel: null,
};
var fragment = document.createDocumentFragment();
var tr = document.createElement("tr");
var outputLabel = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("span"));
outputLabel.appendChild(document.createTextNode(channel.name));
var inputLabel = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("span"));
inputLabel.appendChild(document.createTextNode("None"));
submix.inputLabel = inputLabel;
var inputButton = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("button"));
inputButton.appendChild(document.createTextNode("Set"));
inputButton.addEventListener("click", setControl.bind(undefined, submix));
var valueLabel = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("span"));
valueLabel.appendChild(document.createTextNode(submix['value']));
submix.valueLabel = valueLabel;
var invertCheckbox = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("input"));
invertCheckbox.type = "checkbox";
invertCheckbox.addEventListener("change", function(e) { submix.invertMult = e.target.checked ? -1 : 1; });
var weightNumber = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("input"));
weightNumber.type = "number";
weightNumber.step = 0.01;
weightNumber.defaultValue = submix['weight'];
weightNumber.addEventListener("change", function(e) { submix.weight = parseFloat(e.target.value); });
var shiftNumber = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("input"));
shiftNumber.type = "number";
shiftNumber.step = 0.01;
shiftNumber.defaultValue = submix['shift'];
shiftNumber.addEventListener("change", function(e) { submix.shift = parseFloat(e.target.value); });
var expoNumber = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("input"));
expoNumber.type = "number";
expoNumber.step = 0.01;
expoNumber.defaultValue = submix['expo'];
expoNumber.addEventListener("change", function(e) { submix.expo = parseFloat(e.target.value);} );
var resultLabel = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("span"));
resultLabel.appendChild(document.createTextNode(submix.result));
submix.resultLabel = resultLabel;
var finalValueLabel = tr.appendChild(document.createElement("td"))
.appendChild(document.createElement("span"));
finalValueLabel.appendChild(document.createTextNode("0"));
var mix = {channel: channel, submixes: [submix], finalValueLabel: finalValueLabel,};
submix.parent = mix;
mixes.push(mix);
fragment.appendChild(tr);
return fragment;
}
function generateControls() {
var fragment = document.createDocumentFragment();
channels.forEach(function(channel) {
fragment.appendChild(newMix(channel));
});
var table = document.getElementById("controls-table").appendChild(fragment);
}
document.addEventListener('DOMContentLoaded', function() {
generateControls();
if (canGame()) {
window.addEventListener("gamepadconnected", function(e) {
console.log("connection event");
});
window.addEventListener("gamepaddisconnected", function(e) {
console.log("disconnection event");
});
window.addEventListener("gamepadbuttondown", buttonDown);
window.addEventListener("gamepadbuttonup", buttonUp);
window.addEventListener("gamepadaxismove", axisMove);
}
}, false);
// ↓↓↓ Input handlers ↓↓↓
function buttonUp(e) {
var control = controls[e.gamepad.index + "btn" + e.button];
if (control)
handle(control, 0);
}
function buttonDown(e) {
//console.log(actions);
if (newControl !== null) {
controls[e.gamepad.index + "btn" + e.button] = newControl;
newControl.inputLabel.innerHTML = "Joypad " + e.gamepad.index + ", button " + e.button;
newControl = null;
return;
}
var control = controls[e.gamepad.index + "btn" + e.button];
if (control)
handle(control, 1);
}
function axisMove(e) {
if (newControl !== null) {
if (e.value < -0.5 || +0.5 < e.value ) {
controls[e.gamepad + "axis" + e.axis] = newControl;
newControl.inputLabel.innerHTML = "Joypad " + e.gamepad.index + ", axis " + e.axis;
newControl = null;
return;
}
}
var control = controls[e.gamepad + "axis" + e.axis];
if (control)
handle(control, e.value);
}
// ↑↑↑ Input handlers ↑↑↑