-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
320 lines (294 loc) · 7.97 KB
/
index.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>MicroPython WebBluetooth REPL</title>
<meta name="Description" content=""/>
<link rel="shortcut icon" href=""/>
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="jquery.terminal-src.css" rel="stylesheet"/>
<style>
body {
background-color: #CCCCCC;
}
#buttons {
position: absolute;
top: 30px;
left: 65px;
z-index: 100;
padding: 8px 0 0;
box-sizing: border-box;
}
#term {
position: absolute;
top: 80px;
left: 65px;
z-index: 100;
padding: 8px 0 0;
box-sizing: border-box;
}
#log_wrapper {
position: absolute;
top: 400px;
left: 65px;
height: 200px;
}
.terminal {
--color: #95a9d5;
--background: #232e45;
}
.terminal-output > div {
padding-top: 0;
}
div.terminal-output div div, .cmd {
margin-top: -3px;
}
.cmd {
height: 1em;
}
@keyframes blink {
0% { opacity: 1; }
25% { opacity: 0; }
50% { opacity: 0; }
100% { opacity: 1; }
}
@-webkit-keyframes blink {
0% { opacity: 1; }
25% { opacity: 0; }
50% { opacity: 0; }
100% { opacity: 1; }
}
@-ms-keyframes blink {
0% { opacity: 1; }
25% { opacity: 0; }
50% { opacity: 0; }
100% { opacity: 1; }
}
@-moz-keyframes blink {
0% { opacity: 1; }
25% { opacity: 0; }
50% { opacity: 0; }
100% { opacity: 1; }
}
.cmd .cursor.blink {
background: #95a9d5;
-webkit-animation: blink 1s infinite linear;
-moz-animation: blink 1s infinite linear;
-ms-animation: blink 1s infinite linear;
animation: blink 1s linear infinite;
-webkit-box-shadow: 0 0 5px rgba(0,149,169,213);
-moz-box-shadow: 0 0 5px rgba(0,149,169,213);
-ms-box-shadow: 0 0 5px rgba(0,149,169,213);
-o-box-shadow: 0 0 5px rgba(0,149,169,213);
box-shadow: 0 0 5px rgba(0,149,169,213);
}
</style>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="jquery.terminal-1.1.0.mod.js"></script>
<script>
'use strict'
const bleNusServiceUUID = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';
const bleNusCharRXUUID = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
const bleNusCharTXUUID = '6e400003-b5a3-f393-e0a9-e50e24dcca9e';
const MTU = 20;
var term;
var bleDevice;
var bleServer;
var nusService;
var rxCharacteristic;
var txCharacteristic;
var gattServer;
var notification;
var accept;
$(function() {
notification = false;
accept = false;
term = $('#term').terminal(function() {
}, {
prompt: '',
greetings: 'MicroPython WebBluetooth REPL\n\n' +
'Connect to the board using the button above.\n',
keypress: function(e, term) {
if (e.which != 13) {
term.insert(String.fromCharCode(e.which));
}
return false;
},
onBeforeCommand: function(e, command) {
if (notification === false) {
sendString(command);
}
if (accept === true) {
return true;
}
return notification;
},
width: 600,
height: 300
});
$('#connect').on('click', connect);
$('#disconnect').on('click', disconnect);
$('#get-msg').on('click', get_msg);
function get_msg(){
log('> Notifications started');
sendString("0")
return txCharacteristic.addEventListener('characteristicvaluechanged',
handleNotifications);
}
function get_wrap_pos(text) {
let r = text.indexOf('\r');
let n = text.indexOf('\n');
let max = r;
if (n > r) {
max = n;
}
return max;
}
function inject_text(text) {
notification = true;
let last_line_wrap = get_wrap_pos(text);
if (last_line_wrap > 0) {
let prmt = term.get_prompt();
text = prmt + text;
term.set_prompt("");
do {
let next_line_pos = get_wrap_pos(text);
term.exec(text.slice(0, next_line_pos));
text = text.slice(next_line_pos + 1);
} while (get_wrap_pos(text) > 0);
term.set_prompt(text);
} else {
let prmt = term.get_prompt();
term.set_prompt(prmt + text);
}
notification = false;
}
function sendString(s) {
log("send" + s);
let val_arr = new Uint8Array(s.length + 1)
for (let i = 0; i < s.length; i++) {
let val = s[i].charCodeAt(0);
val_arr[i] = val;
}
val_arr[s.length] = 13;
sendNextChunk(val_arr);
}
function sendNextChunk(a) {
let chunk = a.slice(0, MTU);
rxCharacteristic.writeValue(chunk)
.then(function() {
if (a.length > MTU) {
sendNextChunk(a.slice(MTU));
}
});
}
function handleError(error) {
log(error);
gattServer = null;
nusService = null;
rxCharacteristic = null;
txCharacteristic = null;
dialog.open();
}
function connect() {
if (!navigator.bluetooth) {
log('> WebBluetooth API is not available.\n' +
'> Please make sure the Web Bluetooth flag is enabled.');
return;
}
log('> Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: [bleNusServiceUUID]
})
.then(device => {
bleDevice = device;
log('> Found ' + device.name);
log('> Connecting to GATT Server...');
return device.gatt.connect();
})
.then(server => {
log('> Locate NUS service');
return server.getPrimaryService('6e400001-b5a3-f393-e0a9-e50e24dcca9e');
}).then(service => {
nusService = service;
log('> Found NUS service');
})
.then(() => {
log('> Locate RX characteristic');
return nusService.getCharacteristic('6e400002-b5a3-f393-e0a9-e50e24dcca9e');
})
.then(characteristic => {
rxCharacteristic = characteristic;
log('> Found RX characteristic');
})
.then(() => {
log('> Locate TX characteristic');
return nusService.getCharacteristic('6e400003-b5a3-f393-e0a9-e50e24dcca9e');
})
.then(characteristic => {
txCharacteristic = characteristic;
log('> Found TX characteristic');
})
.then(() => {
log('> Enable notifications');
return txCharacteristic.startNotifications();
})
.then(() => {
log('> Notifications started');
sendString("0")
txCharacteristic.addEventListener('characteristicvaluechanged',
handleNotifications);
})
.catch(error => {
log('> ERROR: ' + error);
});
}
function handleNotifications(event) {
log('> notification');
let value = event.target.value;
let a = [];
// Convert raw data bytes to character values and use these to
// construct a string.
let str = "";
for (let i = 0; i < value.byteLength; i++) {
var number = value.getUint8(i);
str += String.fromCharCode(number);
}
inject_text(str);
}
function disconnect() {
if (!bleDevice) {
log('No Bluetooth Device connected...');
return;
}
log('> Disconnecting from Bluetooth Device...');
if (bleDevice.gatt.connected) {
bleDevice.gatt.disconnect();
log('> Bluetooth Device connected: ' + bleDevice.gatt.connected);
} else {
log('> Bluetooth Device is already disconnected');
}
}
function log(text) {
console.log(text);
$('#log').append(text + '\n');
}
});
</script>
</head>
<body>
<div id="buttons">
<button id="connect" type="button" class="btn btn-default">CONNECT</button>
<button id="disconnect" type="button" class="btn btn-default">DISCONNECT</button>
<button id="get-msg" type="button" class="btn btn-default">GET MESSAGE</button>
</div>
<div id="term"></div>
<div id="log_wrapper">
<pre>Bluetooth log:</pre>
<textarea cols="72" rows="10" id="log"></textarea>
</div>
</body>
</html>