-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·46 lines (42 loc) · 1.3 KB
/
index.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
'use strict';
let Board = function(serialPort) {
this.serialport = serialPort;
this.used = [];
this.rport = /usb|acm|^com/i;
};
Board.prototype.detect = function(cb) {
// Request a list of available ports, from the result set
// filter for valid paths via known path pattern match.
let that = this;
this.serialport.list(function(err, result) {
let ports;
ports = result.filter(function(val) {
let available = true;
// Match only ports that Arduino cares about
// ttyUSB#, cu.usbmodem#, COM#
if (!that.rport.test(val.comName)) {
available = false;
}
// Don't allow already used/encountered usb device paths
if (that.used.indexOf(val.comName) > -1) {
available = false;
}
// Allow only if is an arduino
if (val.manufacturer.indexOf('Arduino') === -1 && val.manufacturer.indexOf('wch') === -1) {
available = false;
}
return available;
}).map(function(val) {
return val.comName;
});
// If no ports are detected...
if (!ports.length) {
cb.call(that, new Error('No ports detected'), null);
} else {
// Get the first available device path from the list of detected ports
that.used.push(ports[0]);
cb.call(that, null, ports[0]);
}
});
};
module.exports = Board;