-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathconsole.js
81 lines (70 loc) · 2.01 KB
/
console.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
(function() {
'use strict';
hterm.defaultStorage = new lib.Storage.Local();
var port;
let textEncoder = new TextEncoder();
let t = new hterm.Terminal();
t.onTerminalReady = () => {
console.log('Terminal ready.');
let io = t.io.push();
io.onVTKeystroke = str => {
if (port !== undefined) {
port.send(textEncoder.encode(str)).catch(error => {
t.io.println('Send error: ' + error);
});
}
};
io.sendString = str => {
if (port !== undefined) {
port.send(textEncoder.encode(str)).catch(error => {
t.io.println('Send error: ' + error);
});
}
};
};
document.addEventListener('DOMContentLoaded', event => {
let connectButton = document.querySelector('#connect');
t.decorate(document.querySelector('#terminal'));
t.setWidth(80);
t.setHeight(24);
t.installKeyboard();
function connect() {
t.io.println('Connecting to ' + port.device_.productName + '...');
port.connect().then(() => {
console.log(port);
t.io.println('Connected.');
connectButton.textContent = 'Disconnect';
port.onReceive = data => {
let textDecoder = new TextDecoder();
t.io.print(textDecoder.decode(data));
}
port.onReceiveError = error => {
t.io.println('Receive error: ' + error);
};
}, error => {
t.io.println('Connection error: ' + error);
});
};
connectButton.addEventListener('click', function() {
if (port) {
port.disconnect();
connectButton.textContent = 'Connect';
} else {
serial.requestPort().then(selectedPort => {
port = selectedPort;
connect();
}).catch(error => {
t.io.println('Connection error: ' + error);
});
}
});
serial.getPorts().then(ports => {
if (ports.length == 0) {
t.io.println('No devices found.');
} else {
port = ports[0];
connect();
}
});
});
})();