-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwinnus.js
53 lines (52 loc) · 1.23 KB
/
winnus.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
const winnus = require('./build/Release/winnus');
var rxInterval;
var rxCallback;
exports.getDevices = function() {
// Merge the 2 sets of data we have
var names = winnus.getDeviceNames();
var paths = winnus.getDevicePaths();
var devices = [];
paths.forEach(function(path) {
var found;
names.forEach(function(name) {
name.mac = name.addr.substr(-12);
if (path.path.indexOf(name.mac)>=0)
found = name;
});
if (found) {
var mac = "";
for (var i=0;i<found.mac.length;i+=2) {
if (i) mac+=":";
mac+=found.mac.substr(i,2);
}
devices.push({
name : found.name,
address : mac,
path : path.path
});
}
});
return devices;
};
exports.connect = function(device, dataCallback) {
rxCallback = dataCallback;
winnus.connect(device.path);
rxInterval = setInterval(function() {
var d = winnus.read();
while (d !== undefined) {
if (rxCallback) rxCallback(d);
d = winnus.read();
}
}, 50);
};
exports.write = function(data) {
winnus.write(data);
};
exports.disconnect = function(device) {
if (rxInterval) {
clearInterval(rxInterval);
rxInterval=undefined;
}
rxCallback = undefined;
winnus.disconnect();
};