2019-05-29 15:50:45 +02:00
|
|
|
// IMPORTANT: install the dependency via 'npm i node-hid' in the same location as the script
|
2019-05-26 23:28:51 +02:00
|
|
|
// 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
|
|
|
|
|
2019-05-24 12:33:56 +07:00
|
|
|
var HID = require('node-hid');
|
2019-05-26 23:28:51 +02:00
|
|
|
var os = require('os')
|
2019-05-29 15:50:45 +02:00
|
|
|
// list of supported devices
|
|
|
|
var boards = require('./boards.js')
|
2019-05-26 23:28:51 +02:00
|
|
|
|
|
|
|
var isWin = (os.platform() === 'win32');
|
2019-05-24 12:33:56 +07:00
|
|
|
var devices = HID.devices();
|
2019-05-26 23:28:51 +02:00
|
|
|
|
2019-05-29 15:50:45 +02:00
|
|
|
// this will choose any device found in the boards.js file
|
|
|
|
var deviceInfo = devices.find(anySupportedBoard);
|
2019-05-24 16:32:55 -07:00
|
|
|
var reportLen = 64;
|
2019-05-26 23:28:51 +02:00
|
|
|
|
|
|
|
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
|
2019-05-24 12:33:56 +07:00
|
|
|
if( deviceInfo ) {
|
|
|
|
console.log(deviceInfo)
|
|
|
|
var device = new HID.HID( deviceInfo.path );
|
|
|
|
|
2019-05-26 23:28:51 +02:00
|
|
|
// 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
|
2019-05-24 12:33:56 +07:00
|
|
|
device.on("error", function(err) {console.log(err)});
|
|
|
|
|
2019-05-26 23:28:51 +02:00
|
|
|
// send our message to the device every 500ms
|
2019-05-24 12:33:56 +07:00
|
|
|
setInterval(function () {
|
2019-05-26 23:28:51 +02:00
|
|
|
device.write(messageBuffer);
|
2019-05-24 12:33:56 +07:00
|
|
|
},500)
|
|
|
|
}
|
2019-05-26 23:28:51 +02:00
|
|
|
|
|
|
|
|
2019-05-29 15:50:45 +02:00
|
|
|
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;
|
2019-05-26 23:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-29 15:50:45 +02:00
|
|
|
function isDevice(board,d){
|
|
|
|
return d.vendorId==board[0] && d.productId==board[1];
|
|
|
|
}
|
2019-05-26 23:28:51 +02:00
|
|
|
|