-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumo2.html
246 lines (229 loc) · 13.6 KB
/
sumo2.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
<html><head><title>Sumo</title>
<!--script src="source/js/resizeCamera.js"></script-->
</head>
<style>
#camera {
width: 100%;
height: 100%;
}
</style>
<body id="kp">
<!--script src="controller/gyronorm/dist/gyronorm.complete.js"></script>-->
<!--<script type="text/javascript" src="source/js/resizeCamera.js"></script>-->
<!--script type="text/javascript" src="controller/sumoController.js"></script-->
<!--<script src="./gyronorm/dist/gyronorm.complete.js"></script>-->
<!--<script type="text/javascript" src="/js/resizeCamera.js"></script>-->
<!--script type="text/javascript" src="./sumoController.js"></script-->
<script src="https://pi.pe/iot/js/jquery-1.8.3.js"></script>
<script src="https://pi.pe/iot/js/phono.sdp.js"></script>
<script src="https://pi.pe/iot/js/pipeDb.js"></script>
<script src="https://pi.pe/iot/js/pipeDuct.js"></script>
<link rel="stylesheet" href="source/css/style.css">
<!--<link rel="stylesheet" href="./css/style.css">-->
<script type="text/javascript">
var myId = undefined;
var duct = null;
var pipe;
var framedata;
var friendList = {};
var imagen =0;
var img = new Image();
var when =0;
// public method for encoding an Uint8Array to base64
function encode (input) {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
while (i < input.length) {
chr1 = input[i++];
chr2 = i < input.length ? input[i++] : Number.NaN; // Not sure if the index
chr3 = i < input.length ? input[i++] : Number.NaN; // checks are needed here
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output += keyStr.charAt(enc1) + keyStr.charAt(enc2) +
keyStr.charAt(enc3) + keyStr.charAt(enc4);
}
return output;
}
function showFriends(friends){
var pics = "<ol>";
friends.forEach(function (fr){
friendList[fr.finger] = fr;
pics += "<li><img src='"+fr.tag+"' onclick=\"makedc('"+fr.finger+"');\"></li>";
});
pics += "</ol>";
$('#friendList').html(pics);
}
function gotId(id) {
myId = id;
duct = new PipeDuct(id);
duct.setOnDataChannel(onNewDc);
duct.connect().then(function(d){
PipeDb.dbListPrint(showFriends);
});
}
function showSpeed(){
var ticks = Date.now() - when;
var rate = (1000.0 * imagen) / ticks;
console.log("rate = "+rate);
}
function onDcMessage(msg) {
var arrayBuffer = msg.data;
var bytes = new Uint8Array(arrayBuffer);
img.onload = function(){
var cam = document.getElementById('camera');
var ctx = cam.getContext('2d');
cam.width = this.naturalWidth;
cam.height = this.naturalHeight;
ctx.drawImage(this, 0, 0, this.width, this.height);
}
img.src = 'data:image/jpeg;base64,'+encode(bytes);
imagen++;
showSpeed();
}
function onNewDc(channel) {
console.log("New DC ")
channel.onmessage = onDcMessage;
}
function updateUI(l){
const forwards = document.getElementById("forwards");
const backwards = document.getElementById("backwards");
const left = document.getElementById("left");
const right = document.getElementById("right");
if (l == "f") {forwards.style.fill = "#00A7FF";}
else {forwards.style.fill = "#ffffff";}
if (l == "b") {backwards.style.fill = "#00A7FF";}
else {backwards.style.fill = "#ffffff";}
if (l == "l") {left.style.fill = "#00A7FF";}
else {left.style.fill = "#ffffff";}
if (l == "r") {right.style.fill ="#00A7FF";}
else {right.style.fill = "#ffffff";}
}
function button(l) {
console.log("sending command "+l);
pipe.send(l);
updateUI(l);
}
function setKpTarget(){
var kp = document.getElementById("kp");
kp.onkeydown = function(evt){
var key = String.fromCharCode(evt.keyCode).toLowerCase();
switch (evt.keyCode){
case 37: key='l';break;
case 38: key='f';break;
case 39: key='r';break;
case 40: key='b';break;
default:
}
button(key);
};
kp.onkeyup = function(evt){
var keyCode = evt.keyCode;
button('h');
};
}
function makedc(toId) {
console.log("setting to" + toId);
duct.setTo(toId);
var channel = duct.createDataChannel("Sumo", {});
channel.onopen = function() {
console.log("Sumo channel ");
when = Date.now();
$('#friendList').hide();
$('#camera').show();
$('#controls').show();
setKpTarget();
//start_gn();
};
channel.onmessage = onDcMessage;
pipe = channel;
}
$(document).ready(function() {
$('#controls').hide();
PipeDb.whoAmI(gotId, function(err) {
console.log("could not create identity " + err);
});
});
function stopGn() {
button("h");
//stop_gn();
// pipe.close();
}
function startGn() {
//start_gn();
}
</script>
<div id="friendList">
</div>
<canvas id="camera"></canvas>
<div id="controls" ontouchend="button('h')">
<div class="bf-controls">
<div id="forwardButton" ontouchstart="button('f');" class="control" >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="40.23 128 73.539 73.539">
<path id="forwards" class="forwards" d="M0,0H52L18.449,18.449,0,52Z" transform="translate(77 128) rotate(45)" />
</svg>
</div>
<div id="backButton" ontouchstart="button('b');" class="control" >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="40.23 159 73.539 73.539">
<path id="backwards" class="backwards" d="M0,0H52L18.449,18.449,0,52Z" transform="translate(77 232.539) rotate(-135)" />
</svg>
</div>
</div>
<div class="lr-controls">
<div id="leftButton" ontouchstart="button('l');" class="control" >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="502.73 143 73.539 73.539">
<path id="left" class="left" d="M0,0H52L18.449,18.449,0,52Z" transform="translate(502.73 179.77) rotate(-45)" />
</svg>
</div>
<div id="rightButton" ontouchstart="button('r');" class="control" >
<svg xmlns="http://www.w3.org/2000/svg" viewBox="533.73 143 73.539 73.539">
<path id="right" class="right" d="M0,0H52L18.449,18.449,0,52Z" transform="translate(607.27 179.77) rotate(135)"></path>
</svg>
</div>
</div>
</div>
<div class="stopngo">
<div id="go" class="go" onmousedown="startGn()">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="354 -357 102 102">
<defs>
<style>
.go-style-1 {
fill: #21d500;
}
.go-style-2 {
fill: #fff;
}
</style>
</defs>
<circle id="elipseGo" data-name="Ellipse 2" class="go-style-1" cx="51" cy="51" r="51" transform="translate(354 -357)"/>
<path id="Pfad_55" data-name="Pfad 55" class="go-style-2" d="M18.215-17.163l.477,2.416h2.983V-26.26H12.727v3.49h4.713a5.12,5.12,0,0,1-1.476,3.415,5.084,5.084,0,0,1-3.594,1.178,5.466,5.466,0,0,1-2.684-.611A5.121,5.121,0,0,1,7.9-20.414a7.053,7.053,0,0,1-1-2.282,10.841,10.841,0,0,1-.313-2.61A11.653,11.653,0,0,1,6.9-28.02a7.188,7.188,0,0,1,1-2.341A5.068,5.068,0,0,1,9.685-32a5.466,5.466,0,0,1,2.684-.611,4.684,4.684,0,0,1,2.893.895,4.481,4.481,0,0,1,1.611,2.684h4.474a7.949,7.949,0,0,0-.984-3.221,7.922,7.922,0,0,0-2.043-2.356,9,9,0,0,0-2.774-1.447,10.347,10.347,0,0,0-3.176-.492,10.756,10.756,0,0,0-4.4.865,9.41,9.41,0,0,0-3.3,2.386,10.639,10.639,0,0,0-2.058,3.564A13.306,13.306,0,0,0,1.9-25.306a12.862,12.862,0,0,0,.716,4.34,10.364,10.364,0,0,0,2.058,3.5,9.427,9.427,0,0,0,3.3,2.341,10.92,10.92,0,0,0,4.4.85,7.847,7.847,0,0,0,3.072-.641A7.018,7.018,0,0,0,18.215-17.163Zm11.006-8.142a11.653,11.653,0,0,1,.313-2.714,7.188,7.188,0,0,1,1-2.341A5.068,5.068,0,0,1,32.322-32a5.466,5.466,0,0,1,2.684-.611A5.466,5.466,0,0,1,37.691-32a5.068,5.068,0,0,1,1.79,1.64,7.188,7.188,0,0,1,1,2.341,11.653,11.653,0,0,1,.313,2.714,10.841,10.841,0,0,1-.313,2.61,7.053,7.053,0,0,1-1,2.282,5.121,5.121,0,0,1-1.79,1.626,5.466,5.466,0,0,1-2.684.611,5.466,5.466,0,0,1-2.684-.611,5.121,5.121,0,0,1-1.79-1.626,7.053,7.053,0,0,1-1-2.282A10.841,10.841,0,0,1,29.221-25.306Zm-4.683,0a12.862,12.862,0,0,0,.716,4.34,10.364,10.364,0,0,0,2.058,3.5,9.427,9.427,0,0,0,3.3,2.341,10.92,10.92,0,0,0,4.4.85,10.892,10.892,0,0,0,4.414-.85A9.46,9.46,0,0,0,42.7-17.461a10.364,10.364,0,0,0,2.058-3.5,12.862,12.862,0,0,0,.716-4.34,13.306,13.306,0,0,0-.716-4.429A10.639,10.639,0,0,0,42.7-33.3a9.444,9.444,0,0,0-3.281-2.386,10.729,10.729,0,0,0-4.414-.865,10.756,10.756,0,0,0-4.4.865,9.41,9.41,0,0,0-3.3,2.386,10.639,10.639,0,0,0-2.058,3.564A13.306,13.306,0,0,0,24.538-25.306Z" transform="translate(381.1 -280.45)"/>
</svg>
</div>
<div id="stop" class="stop" onmousedown="stopGn()">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="235 -357 102 102">
<defs>
<style>
.stop-style-1 {
fill: #f88;
}
.stop-style-2 {
fill: #fff;
}
</style>
</defs>
<circle id="elipseStop" data-name="Ellipse 3" class="stop-style-1" cx="51" cy="51" r="51" transform="translate(235 -357)"/>
<path id="Pfad_56" data-name="Pfad 56" class="stop-style-2" d="M5.493-14.507H.96a7.033,7.033,0,0,0,.716,3.4A6.7,6.7,0,0,0,3.688-8.751,8.589,8.589,0,0,0,6.611-7.394a13.453,13.453,0,0,0,3.415.432,13.111,13.111,0,0,0,3.832-.507,8.1,8.1,0,0,0,2.774-1.417,5.9,5.9,0,0,0,1.685-2.162,6.507,6.507,0,0,0,.567-2.714,5.221,5.221,0,0,0-.761-2.938,6.352,6.352,0,0,0-1.8-1.834,7.932,7.932,0,0,0-2.1-1q-1.059-.313-1.655-.432-2-.507-3.236-.835a12,12,0,0,1-1.939-.656,2.13,2.13,0,0,1-.939-.716,1.914,1.914,0,0,1-.239-1.014,2,2,0,0,1,.3-1.133,2.59,2.59,0,0,1,.761-.746A3.032,3.032,0,0,1,8.3-25.482,5.488,5.488,0,0,1,9.43-25.6a7.986,7.986,0,0,1,1.6.149,3.846,3.846,0,0,1,1.3.507,2.635,2.635,0,0,1,.91.984,3.765,3.765,0,0,1,.4,1.581h4.533a6.54,6.54,0,0,0-.7-3.146,6.11,6.11,0,0,0-1.894-2.132,8,8,0,0,0-2.729-1.208,13.547,13.547,0,0,0-3.206-.373,10.911,10.911,0,0,0-2.863.388A7.993,7.993,0,0,0,4.21-27.659a6.35,6.35,0,0,0-1.834,2.013,5.576,5.576,0,0,0-.7,2.848,5.207,5.207,0,0,0,.552,2.49,5.267,5.267,0,0,0,1.446,1.715A7.992,7.992,0,0,0,5.7-17.475a20.39,20.39,0,0,0,2.326.731q1.163.328,2.3.6a14.437,14.437,0,0,1,2.028.626,4.376,4.376,0,0,1,1.446.895,1.868,1.868,0,0,1,.552,1.4,2.058,2.058,0,0,1-.418,1.327,2.928,2.928,0,0,1-1.044.82,4.67,4.67,0,0,1-1.342.4,9.328,9.328,0,0,1-1.342.1,7.118,7.118,0,0,1-1.789-.224,4.454,4.454,0,0,1-1.506-.686,3.388,3.388,0,0,1-1.029-1.208A3.906,3.906,0,0,1,5.493-14.507ZM26.369-24.8V-7.439h4.682V-24.8h6.382v-3.937H19.987V-24.8ZM43.637-18a11.653,11.653,0,0,1,.313-2.714,7.188,7.188,0,0,1,1-2.341,5.068,5.068,0,0,1,1.789-1.64,5.466,5.466,0,0,1,2.684-.611,5.466,5.466,0,0,1,2.684.611,5.068,5.068,0,0,1,1.789,1.64,7.188,7.188,0,0,1,1,2.341A11.653,11.653,0,0,1,55.209-18a10.84,10.84,0,0,1-.313,2.61,7.052,7.052,0,0,1-1,2.282,5.121,5.121,0,0,1-1.789,1.625,5.466,5.466,0,0,1-2.684.611,5.466,5.466,0,0,1-2.684-.611,5.121,5.121,0,0,1-1.789-1.625,7.053,7.053,0,0,1-1-2.282A10.84,10.84,0,0,1,43.637-18Zm-4.682,0a12.861,12.861,0,0,0,.716,4.339,10.364,10.364,0,0,0,2.058,3.5,9.426,9.426,0,0,0,3.3,2.341,10.919,10.919,0,0,0,4.4.85,10.891,10.891,0,0,0,4.414-.85,9.459,9.459,0,0,0,3.281-2.341,10.364,10.364,0,0,0,2.058-3.5A12.861,12.861,0,0,0,59.891-18a13.305,13.305,0,0,0-.716-4.429,10.638,10.638,0,0,0-2.058-3.564,9.443,9.443,0,0,0-3.281-2.386,10.728,10.728,0,0,0-4.414-.865,10.756,10.756,0,0,0-4.4.865,9.409,9.409,0,0,0-3.3,2.386,10.638,10.638,0,0,0-2.058,3.564A13.305,13.305,0,0,0,38.955-18Zm28.81-.716v-6.382H71.4a9.806,9.806,0,0,1,1.551.119,3.609,3.609,0,0,1,1.312.462,2.518,2.518,0,0,1,.91.969,3.407,3.407,0,0,1,.343,1.64,3.407,3.407,0,0,1-.343,1.64,2.518,2.518,0,0,1-.91.969,3.609,3.609,0,0,1-1.312.462,9.806,9.806,0,0,1-1.551.119ZM63.083-28.733V-7.439h4.682v-7.635h4.921a8.869,8.869,0,0,0,3.4-.582,6.361,6.361,0,0,0,2.282-1.536,5.88,5.88,0,0,0,1.282-2.192,8.1,8.1,0,0,0,.4-2.52,8.053,8.053,0,0,0-.4-2.535,5.9,5.9,0,0,0-1.282-2.177,6.361,6.361,0,0,0-2.282-1.536,8.869,8.869,0,0,0-3.4-.582Z" transform="translate(245.042 -287.76)"/>
</svg>
</div>
</div>
<div id="error-message"></div>
</body>
</html>