Skip to content

Commit dc601ab

Browse files
authored
Merge pull request #78 from PTS93/master
Made node.js hid example more friendly for new users
2 parents 1d5069e + be72caf commit dc601ab

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed
Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,67 @@
1+
// IMPORTANT: install dependency via 'npm i node-hid' in the same location as the script
2+
// If the install fails on windows you may need to run 'npm i -g windows-build-tools' first to be able to compile native code needed for this library
3+
14
var HID = require('node-hid');
5+
var os = require('os')
6+
7+
var isWin = (os.platform() === 'win32');
28
var devices = HID.devices();
3-
var deviceInfo = devices.find( function(d) {
4-
var isNRF = d.vendorId===0Xcafe && d.productId===0X4004;
5-
return isNRF;
6-
});
9+
10+
// choose either of the following supported devices:
11+
// Metro_nRF52840, Feather_nRF52840
12+
13+
var deviceInfo = devices.find(Feather_nRF52840);
714
var reportLen = 64;
15+
16+
var message = "Hello World!"
17+
18+
// Turn our string into an array of integers e.g. 'ascii codes', though charCodeAt spits out UTF-16
19+
// This means if you have characters in your string that are not Latin-1 you will have to add additional logic for character codes above 255
20+
var messageBuffer = Array.from(message, function(c){return c.charCodeAt(0)});
21+
22+
// Windows wants you to prepend a 0 to whatever you send
23+
if(isWin){
24+
messageBuffer.unshift(0)
25+
}
26+
27+
// Some OSes expect that you always send a buffer that equals your report length
28+
// So lets fill up the rest of the buffer with zeros
29+
var paddingBuf = Array(reportLen-messageBuffer.length);
30+
paddingBuf.fill(0)
31+
messageBuffer = messageBuffer.concat(paddingBuf)
32+
33+
// check if we actually found a device and if so send our messageBuffer to it
834
if( deviceInfo ) {
935
console.log(deviceInfo)
1036
var device = new HID.HID( deviceInfo.path );
11-
device.on("data", function(data) { console.log(data.toString('hex')); });
1237

38+
// register an event listener for data coming from the device
39+
device.on("data", function(data) {
40+
// Print what we get from the device
41+
console.log(data.toString('ascii'));
42+
});
43+
44+
// the same for any error that occur
1345
device.on("error", function(err) {console.log(err)});
1446

47+
// send our message to the device every 500ms
1548
setInterval(function () {
16-
var buf = Array(reportLen);
17-
for( var i=0; i<buf.length; i++) {
18-
buf[i] = 0x30 + i; // 0x30 = '0'
19-
}
20-
device.write(buf);
49+
device.write(messageBuffer);
2150
},500)
2251
}
52+
53+
54+
55+
56+
function Feather_nRF52840(d) {
57+
return isDevice(0X239A,0X8029,d)
58+
}
59+
60+
function Metro_nRF52840(d) {
61+
return isDevice(0X239A,0X803F,d)
62+
}
63+
64+
65+
function isDevice(vid,pid,d){
66+
return d.vendorId==vid && d.productId==pid;
67+
}

0 commit comments

Comments
 (0)