From f4dcc08de3f6551960cc05e5204cd32ecb2f76e5 Mon Sep 17 00:00:00 2001 From: "Tod E. Kurt" Date: Fri, 24 May 2019 16:32:55 -0700 Subject: [PATCH 1/3] make hid_generic_inout tester have reportLength-sized buffer, since some OSes need that --- examples/device/hid_generic_inout/hid_test.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/device/hid_generic_inout/hid_test.js b/examples/device/hid_generic_inout/hid_test.js index d4fc800c..011f2b53 100644 --- a/examples/device/hid_generic_inout/hid_test.js +++ b/examples/device/hid_generic_inout/hid_test.js @@ -4,14 +4,19 @@ var deviceInfo = devices.find( function(d) { var isNRF = d.vendorId===0Xcafe && d.productId===0X4004; return isNRF; }); +var reportLen = 64; if( deviceInfo ) { console.log(deviceInfo) var device = new HID.HID( deviceInfo.path ); - device.on("data", function(data) {console.log(data)}); + device.on("data", function(data) { console.log(data.toString('hex')); }); device.on("error", function(err) {console.log(err)}); setInterval(function () { - device.write([0x00, 0x01, 0x01, 0x05, 0xff, 0xff]); + var buf = Array(reportLen); + for( var i=0; i Date: Sun, 26 May 2019 23:28:51 +0200 Subject: [PATCH 2/3] changed node.js hid example to be more user friendly --- examples/device/hid_generic_inout/hid_test.js | 65 ++++++++++++++++--- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/examples/device/hid_generic_inout/hid_test.js b/examples/device/hid_generic_inout/hid_test.js index 011f2b53..ef627c7a 100644 --- a/examples/device/hid_generic_inout/hid_test.js +++ b/examples/device/hid_generic_inout/hid_test.js @@ -1,22 +1,67 @@ +// IMPORTANT: install dependency via 'npm i node-hid' in the same location as the script +// 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 + var HID = require('node-hid'); +var os = require('os') + +var isWin = (os.platform() === 'win32'); var devices = HID.devices(); -var deviceInfo = devices.find( function(d) { - var isNRF = d.vendorId===0Xcafe && d.productId===0X4004; - return isNRF; -}); + +// choose either of the following supported devices: +// Metro_nRF52840, Feather_nRF52840 + +var deviceInfo = devices.find(Feather_nRF52840); var reportLen = 64; + +var message = "Hello World!" + +// Turn our string into an array of integers e.g. 'ascii codes', though charCodeAt spits out UTF-16 +// 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 +var messageBuffer = Array.from(message, function(c){return c.charCodeAt(0)}); + +// Windows wants you to prepend a 0 to whatever you send +if(isWin){ + messageBuffer.unshift(0) +} + +// Some OSes expect that you always send a buffer that equals your report length +// So lets fill up the rest of the buffer with zeros +var paddingBuf = Array(reportLen-messageBuffer.length); +paddingBuf.fill(0) +messageBuffer = messageBuffer.concat(paddingBuf) + +// check if we actually found a device and if so send our messageBuffer to it if( deviceInfo ) { console.log(deviceInfo) var device = new HID.HID( deviceInfo.path ); - device.on("data", function(data) { console.log(data.toString('hex')); }); + // register an event listener for data coming from the device + device.on("data", function(data) { + // Print what we get from the device + console.log(data.toString('ascii')); + }); + + // the same for any error that occur device.on("error", function(err) {console.log(err)}); + // send our message to the device every 500ms setInterval(function () { - var buf = Array(reportLen); - for( var i=0; i Date: Wed, 29 May 2019 15:50:45 +0200 Subject: [PATCH 3/3] Modified example to use list of supported boards --- examples/device/hid_generic_inout/boards.js | 4 +++ examples/device/hid_generic_inout/hid_test.js | 36 ++++++++++--------- 2 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 examples/device/hid_generic_inout/boards.js diff --git a/examples/device/hid_generic_inout/boards.js b/examples/device/hid_generic_inout/boards.js new file mode 100644 index 00000000..e8437cf5 --- /dev/null +++ b/examples/device/hid_generic_inout/boards.js @@ -0,0 +1,4 @@ +module.exports = { + "Feather_nRF52840":[0X239A,0X8029], + "Metro_nRF52840":[0X239A,0X803F], +} \ No newline at end of file diff --git a/examples/device/hid_generic_inout/hid_test.js b/examples/device/hid_generic_inout/hid_test.js index ef627c7a..eed6b78e 100644 --- a/examples/device/hid_generic_inout/hid_test.js +++ b/examples/device/hid_generic_inout/hid_test.js @@ -1,16 +1,16 @@ -// IMPORTANT: install dependency via 'npm i node-hid' in the same location as the script +// IMPORTANT: install the dependency via 'npm i node-hid' in the same location as the script // 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 var HID = require('node-hid'); var os = require('os') +// list of supported devices +var boards = require('./boards.js') var isWin = (os.platform() === 'win32'); var devices = HID.devices(); -// choose either of the following supported devices: -// Metro_nRF52840, Feather_nRF52840 - -var deviceInfo = devices.find(Feather_nRF52840); +// this will choose any device found in the boards.js file +var deviceInfo = devices.find(anySupportedBoard); var reportLen = 64; var message = "Hello World!" @@ -51,17 +51,21 @@ if( deviceInfo ) { } - - -function Feather_nRF52840(d) { - return isDevice(0X239A,0X8029,d) -} - -function Metro_nRF52840(d) { - return isDevice(0X239A,0X803F,d) +function anySupportedBoard(d) { + + for (var key in boards) { + if (boards.hasOwnProperty(key)) { + if (isDevice(boards[key],d)) { + console.log("Found " + d.product); + return true; + } + } + } + return false; } -function isDevice(vid,pid,d){ - return d.vendorId==vid && d.productId==pid; -} \ No newline at end of file +function isDevice(board,d){ + return d.vendorId==board[0] && d.productId==board[1]; +} +