forked from onmachine/chrome-serial-monitor
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
117 lines (92 loc) · 2.98 KB
/
main.js
File metadata and controls
117 lines (92 loc) · 2.98 KB
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
// JSLint Options
/*global chrome: false */
/*jslint browser: true, devel: true*/
var connectionId = -1;
var bitrates = [300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200];
function setStatus(status) {
document.getElementById('status').innerText = status;
}
function onOpen() {
if (!serial_lib.isConnected()) {
setStatus('Could not open');
return;
}
setStatus('Connected');
serial_lib.startListening(onRead);
}
function openSelectedPort() {
var portPicker = document.getElementById('port-picker');
var selectedPort = portPicker.options[portPicker.selectedIndex].value;
var bitratePicker = document.getElementById('bitrate-picker');
var selectedBitrate = bitratePicker.options[bitratePicker.selectedIndex].value;
serial_lib.setBitrate(parseInt(selectedBitrate,10));
serial_lib.openSerial(selectedPort, onOpen);
}
function buildPortPicker(ports) {
var eligiblePorts = ports.filter(function (port) {
return !port.match(/[Bb]luetooth/);
});
var portPicker = document.getElementById('port-picker');
eligiblePorts.forEach(function (port) {
var portOption = document.createElement('option');
portOption.value = portOption.innerText = port;
portPicker.appendChild(portOption);
});
portPicker.onchange = function () {
if (connectionId !== -1) {
serial_lib.closeSerial(serial_lib.onClose);
return;
}
openSelectedPort();
};
}
function buildBitratePicker(bitrates) {
var brPicker = document.getElementById('bitrate-picker');
bitrates.forEach(function (br) {
var brOption = document.createElement('option');
brOption.value = brOption.innerText = br;
brPicker.appendChild(brOption);
});
brPicker.value = serial_lib.getBitrate();
brPicker.onchange = function () {
if (connectionId !== -1) {
serial_lib.closeSerial(serial_lib.onClose);
return;
}
openSelectedPort();
};
}
var writeSerial = function (writeString) {
if (!serial_lib.isConnected()) {
return;
}
if (!writeString) {
console.log("Nothing to write");
return;
}
//if (writeString.charAt(writeString.length - 1) !== '\r') {
// writeString += "\r";
//}
serial_lib.writeSerial(writeString);
};
var onRead = function (readData) {
if (readData.indexOf("log:") >= 0) {
console.log("log:");
return;
}
var output = document.getElementById('output');
output.innerHTML += readData + '<br/>';
};
onload = function () {
var sendButton = document.getElementById('send');
sendButton.addEventListener('click', function () {
var input = document.getElementById('input').value;
console.log(input);
writeSerial(input);
});
serial_lib.getPorts(function (ports) {
buildPortPicker(ports);
buildBitratePicker(bitrates);
openSelectedPort();
});
};