summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2019-06-20 18:36:35 +0700
committerhathach <[email protected]>2019-06-20 18:36:35 +0700
commit991aaf0158fcb02f7951a895a0caa6637e84fd8a (patch)
tree18a4febc3609ba45ca520d359dc0a0dd71b50666
parent73d7ab201e46e1eca9a190e65f96db7158805a65 (diff)
parent393492823ce037a2e46d367d61fad1235859af2e (diff)
Merge branch 'master' into develop
-rw-r--r--examples/device/hid_generic_inout/boards.js4
-rw-r--r--examples/device/hid_generic_inout/hid_test.js66
2 files changed, 64 insertions, 6 deletions
diff --git a/examples/device/hid_generic_inout/boards.js b/examples/device/hid_generic_inout/boards.js
new file mode 100644
index 000000000..e8437cf53
--- /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 d4fc800c7..eed6b78e2 100644
--- a/examples/device/hid_generic_inout/hid_test.js
+++ b/examples/device/hid_generic_inout/hid_test.js
@@ -1,17 +1,71 @@
+// 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();
-var deviceInfo = devices.find( function(d) {
- var isNRF = d.vendorId===0Xcafe && d.productId===0X4004;
- return isNRF;
-});
+
+// this will choose any device found in the boards.js file
+var deviceInfo = devices.find(anySupportedBoard);
+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)});
+ // 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 () {
- device.write([0x00, 0x01, 0x01, 0x05, 0xff, 0xff]);
+ device.write(messageBuffer);
},500)
}
+
+
+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(board,d){
+ return d.vendorId==board[0] && d.productId==board[1];
+}
+