-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.js
124 lines (105 loc) · 3.17 KB
/
serial.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
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
const SerialPort = require('serialport');
const ReadLine = require('@serialport/parser-readline');
const serialconnect = new Event('serialconnect');
const serialdisconnect = new Event('serialdisconnect');
const serialtuffpadverify = new Event('serialtuffpadverify');
var vendorId = "16C0";
var oldProductId = "0487";
var productId = "0486";
var serialDevice = null;
var parser = null;
async function getSerialPorts() {
let serialPorts = [];
await SerialPort.list().then((ports, err) => {
if (err) {
//document.getElementById('message').textContent = err.message
//return
} else {
//document.getElementById('message').textContent = ''
}
console.log('ports', ports);
if (ports.length === 0) {
//document.getElementById('message').textContent = 'No ports discovered'
}
//document.getElementById('seriallist').innerHTML = JSON.stringify(ports);
serialPorts = ports;
})
return serialPorts;
}
function onSerialPortOpen() {
window.dispatchEvent(serialconnect);
}
function onSerialPortClose() {
window.dispatchEvent(serialdisconnect);
}
function onParserDataReceive(data) {
console.log('Serial receive: ' + data);
if (data && data.length > 1) {
switch (data[0]) {
case 'N':
handleSerialNotification(data);
break;
case 'S':
handleSerialSet(data);
break;
}
}
}
function connectSerial(port) {
if (serialDevice === null) {
serialDevice = new SerialPort(port, { baudRate: 9600 });
parser = serialDevice.pipe(new ReadLine({ delimiter: '\n' }));
serialDevice.on("open", onSerialPortOpen);
serialDevice.on("close", onSerialPortClose);
parser.on('data', onParserDataReceive);
console.log('Serial connected.');
}
}
function disconnectSerial() {
if (serialDevice !== null) {
serialDevice.close();
serialDevice = null;
}
}
function sendSerialMessage(bytes) {
if (serialDevice) {
bytes.push(10);
serialDevice.write(bytes, (err) => {
console.log('Serial send: ' + bytes);
if (err) {
console.log('Error on serial send: ' + err.message);
}
});
}
}
function handleSerialNotification(data) {
switch (data[1]) {
case '0':
window.dispatchEvent(serialtuffpadverify);
break;
}
}
function handleSerialSet(data) {
switch (data[1]) {
case 'P': // set profile key
let serialsetprofilekeyvalue = new CustomEvent('serialsetprofilekeyvalue', { detail: data });
window.dispatchEvent(serialsetprofilekeyvalue);
break;
case 'A': // set active profile
let serialsetactiveprofile = new CustomEvent('serialsetactiveprofile', { detail: data });
window.dispatchEvent(serialsetactiveprofile);
break;
case 'B': // set stick axis bounds
let serialsetstickaxisboundary = new CustomEvent('serialsetstickaxisboundary', { detail: data });
window.dispatchEvent(serialsetstickaxisboundary);
break;
case 'C': // set keyboard mode offsets
let serialsetkeyboardmodeoffset = new CustomEvent('serialsetkeyboardmodeoffset', { detail: data });
window.dispatchEvent(serialsetkeyboardmodeoffset);
break;
case 'D': // set is keyboard mode
let serialsetiskeyboardmodeactive = new CustomEvent('serialsetiskeyboardmodeactive', { detail: data });
window.dispatchEvent(serialsetiskeyboardmodeactive);
break;
}
}