forked from UBCSailbot/arduino-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (95 loc) · 2.82 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
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
/**
* A module which scans for available arduino ports, and
* emits an event when an valid port is found.
*
* @author UBCSailbot
* @author areksredzki
* @author joshuabaker2
*/
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var serialport = require('serialport');
var boards = require('./boards.js');
/**
* Constructor
*
* @param {object} options - Options for the consumer to pass in.
*/
var ArduinoScanner = function(opts) {
var self = this;
EventEmitter.call(self);
opts = opts || {};
self.options = {
debug: opts.debug || false,
board: opts.board // Restricts matching if defined
};
self.debug = self.options.debug ? function (message) {
console.log('Arduino: ' + message);
} : function() {};
/**
* Searches the serial ports for any device that has a vendor id and product id
* that matches the arduino's. It only emits the first Arduino it finds. If you
* want it to return multiple Arduinos, take out the 'return matched' within the
* ports.some(function(port){}). If you want more information on all the ports
* that it is skipping over, pass 'true' as the second parameter to scan.start()
*
* i.e.
* scan.start(500, true)
*/
this.search = function() {
var self = this;
serialport.list(function(err, ports) {
if (err || ports.length === 0) {
self.emit('noPortsFound', {
message: 'Nothing detected in serial ports. Check connections.'
});
return;
}
ports.some(function(port) {
var matched =
(port.productId in boards) &&
(!self.options.board ||
(boards[port.productId].indexOf(self.options.board) > -1));
if (matched) {
self.emit('arduinoFound', {
port: port.comName,
message: 'Arduino found at port ' + port.comName + '.'
});
} else {
self.emit('arduinoNotFound', {
message: 'Arduino not at port ' + port.comName + '.'
});
}
return matched;
});
});
};
};
util.inherits(ArduinoScanner, EventEmitter);
/**
* Starts scanning for valid Arduino serial ports.
* It will emit an 'arduinoFound event once a port is found.
*
* @param interval - time in milliseconds before trying port reads again.
*/
ArduinoScanner.prototype.start = function(interval) {
// If the interval isn't set, default to 500 ms.
interval = interval || 500;
var self = this;
self.searchInterval = setInterval(function() {
self.search();
}, interval);
};
/**
* Stop searching.
*/
ArduinoScanner.prototype.stop = function() {
var self = this;
if (self.searchInterval) {
clearInterval(self.searchInterval);
self.debug('Arduino scan stopped.');
} else {
self.debug('Arduino scan was not active.');
}
};
module.exports = ArduinoScanner;