-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCHummer_Gamepad.html
295 lines (252 loc) · 8.99 KB
/
RCHummer_Gamepad.html
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!--
Connect to the raspberry pi using websockets.
Send the position of the two levers according to the user touch.
python -m SimpleHTTPServer 8000
python3 -m http.server
-->
<!doctype html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<head>
<title>RCHummer Gamepad</title>
<style>
html, body {
height: 100%;
width: 100%;
margin: 0;
overflow:hidden;
-webkit-overflow-scrolling: touch;
}
#gamepad {
/* display:block; */
width: 100%;
}
@media only screen and (orientation:landscape){
#gamepad {
text-align:center;
transform: rotate(0deg);
background-color:greenyellow;
height: 100vh; /* https://www.quirksmode.org/css/units-values/viewport.html*/
position: absolute;
top:0;
}
#left_right {
width:50vw;
height:95vh;
}
#forward_backward {
width:50vw;
height:95vh;
}
#portrait_msg{
display: none;
}
}
@media only screen and (orientation:portrait){
#gamepad{
text-align:center;
display: none;
}
#portrait_msg{
position:absolute;
top:50%;
height:50%;
width:100%;
text-align:center;
} /*
I need to test what happens if you change from landscape to portrait while using the gamepad.
I could use if(window.innerHeight > window.innerWidth){
*/
/* #gamepad {
transform: rotate(90deg);
transform-origin: left top;
width: 100vh;
overflow-x: hidden;
position: absolute;
top: 0;
left: 100%;
background-color:greenyellow;
}
#left_right {
width:50vh;
height:95vw;
}
#forward_backward {
width:50vh;
height:95vw;
} */
}
#left_right {
float: left;
background-color:gainsboro;
}
#forward_backward {
float: left;
background-color:gainsboro;
}
#server_status{
text-align:center;
}
button {
display: block;
padding: 16px;
margin: 0 auto 16px auto;
box-sizing: border-box;
border-style: none;
border-radius: 6px;
text-align:center;
text-decoration: none;
user-select: none;
}
.btn {
background-color:dodgerblue;
background-image: none;
touch-action: manipulation;
}
#btn_LR{
position: relative;
width: 30%;
height: 20%;
top: 40%;
left: 0%;
}
#btn_FB{
position: relative;
width: 20%;
height: 30%;
top: 30%;
left: 0%;
}
</style>
<script>
var MAX_SAMPLE_RATE = 50; //in Hertz
var btn_init_pos = { btn_LR: null, btn_FB: null };
var btn_timeout = { btn_LR: true, btn_FB: true };
var ws = null;
var timeout_holder = null;
function send_cmd(btn_id, cmd){
if(cmd==0){
if(timeout_holder){
clearTimeout(timeout_holder)
}
btn_timeout[btn_id] = true;
}
if(btn_timeout[btn_id]){
btn_timeout[btn_id] = false;
timeout_holder = setTimeout(set_btn_timeout, 1000/MAX_SAMPLE_RATE, btn_id); //in ms
var msg = {
cmd: cmd,
id: btn_id,
timestamp: Date.now()
};
if (ws.readyState==ws.OPEN){
// Send the msg object as a JSON-formatted string.
ws.send(JSON.stringify(msg));
}else{
console.log('Connection is closed - ' + JSON.stringify(msg));
}
// console.log('send_cmd - '+ btn_id + ' :' + cmd);
}
}
function set_btn_timeout(btn_id){
btn_timeout[btn_id] = true;
}
function touchstart_btn(evt){
if(evt.target.id=='btn_FB'){
btn_init_pos[evt.targetTouches[0].target.id] = evt.targetTouches[0].clientY
}else{
btn_init_pos[evt.targetTouches[0].target.id] = evt.targetTouches[0].clientX
}
// console.log('Start:'+evt.target.id + ' - ' + btn_init_pos)
}
function touchmove_btn(evt){
if(evt.target.id=='btn_FB'){
var half_Y = evt.targetTouches[0].screenY/2;
var diff = evt.targetTouches[0].clientY-btn_init_pos[evt.targetTouches[0].target.id];
if(diff>100){
diff=100;
} else if(diff<-100){
diff=-100;
}
evt.targetTouches[0].target.style.transform = 'translateY('+diff+'px)';
}else{
var half_Y = evt.targetTouches[0].screenX/2;
var diff = evt.targetTouches[0].clientX-btn_init_pos[evt.targetTouches[0].target.id];
if(diff>100){
diff=100;
} else if(diff<-100){
diff=-100;
}
evt.targetTouches[0].target.style.transform = 'translateX('+diff+'px)';
}
send_cmd(evt.target.id, diff)
// console.log('Move:'+evt.target.id + ' - diff='+diff);
}
function touchend_btn(evt){
if(evt.target.id=='btn_FB'){
evt.target.style.transform = 'translateY(0px)';
}else{
evt.target.style.transform = 'translateX(0px)';
}
send_cmd(evt.target.id, 0)
// console.log('End:'+evt.target.id);
}
window.onload = function (){
// to avoid moving the screen during touch on iOS and
// to disable double touch zoom.
// https://stackoverflow.com/a/49582193
window.addEventListener('touchstart', function(evt) {
evt.preventDefault();
}, { passive: false });
var LR_btn = document.getElementById('btn_LR');
var FB_btn = document.getElementById('btn_FB');
LR_btn.addEventListener('touchstart',touchstart_btn);
LR_btn.addEventListener('touchmove',touchmove_btn);
LR_btn.addEventListener('touchend',touchend_btn);
FB_btn.addEventListener('touchstart',touchstart_btn);
FB_btn.addEventListener('touchmove',touchmove_btn);
FB_btn.addEventListener('touchend',touchend_btn);
ws = new WebSocket("ws://"+window.location.hostname+":8080/ws");
if (ws){
ws.onopen = function(evt) {
document.getElementById('server_status').innerHTML = "Ok. You are in!";
}
ws.onmessage = function(evt) {
console.log(evt.data);
}
ws.onclose = function(evt) {
document.getElementById('server_status').innerHTML = "Not connected...";
}
window.onblur = function(){
if(ws){
ws.close();
document.getElementById('server_status').innerHTML = "Not connected...";
location.reload()
}
}
window.window.onorientationchange = function() {
if(ws){
if(window.orientation==0 || window.orientation==180){
ws.close();
document.getElementById('server_status').innerHTML = "Not connected...";
location.reload()
}
}
}
}
}
</script>
</head>
<body>
<div id="portrait_msg">Please, use landscape mode!</div>
<div id="gamepad">
<div id="left_right">steering
<button id="btn_LR" class="btn" tabindex="1"></button>
</div>
<div id="forward_backward">throttle
<button id="btn_FB" class="btn" tabindex="2"></button>
</div>
<p id="server_status">status...</p>
</div>
</body>
</html>