test-teensyrawhid.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // test-rawhid64.js -- demonstrate sending data to Teensy RawHID device
  3. // also demonstrates filtering on usagePage & usage
  4. //
  5. // For more details, see: https://www.pjrc.com/teensy/rawhid.html
  6. // and https://github.com/node-hid/node-hid/issues/165
  7. //
  8. //
  9. // Tod E. Kurt / github.com/todbot
  10. //
  11. var os = require('os');
  12. var HID = require('../');
  13. var devices = HID.devices();
  14. console.log("HID devices:",devices);
  15. // We must filter devices by vendorId, productId, usagePage, and usage
  16. // because Teensy RawHID sketch shows up as TWO devices to node-hid
  17. // Note this only works on Mac & Windows though as the underlying
  18. // hidapi C library doesn't support usagePage on libusb or hidraw
  19. var isTeensy = function(d) { return d.vendorId===0x16C0 && d.productId===0x0486;}
  20. var isRawHidUsage = function(d) {
  21. return ((d.usagePage===0xFFAB && d.usage===0x2000) || (d.usagePage===0xFFAB && d.usage===0x0200));
  22. }
  23. var device; // to be filled out below
  24. var deviceInfo
  25. if( os.platform() == 'linux' ) {
  26. var deviceList = devices.filter(function(d) { return isTeensy(d) });
  27. if( deviceList.length == 2 ) {
  28. deviceInfo = deviceList[0]
  29. device = new HID.HID( deviceInfo.path ); // normally first device
  30. }
  31. }
  32. else { // Mac & Windows
  33. var deviceInfo = devices.find( function(d) {
  34. return isTeensy(d) && isRawHidUsage(d);
  35. });
  36. if( deviceInfo ) {
  37. device = new HID.HID( deviceInfo.path );
  38. }
  39. }
  40. console.log("deviceInfo: ", deviceInfo);
  41. if( !device ) {
  42. console.log(devices);
  43. console.log("Could not find RawHID device in device list");
  44. process.exit(1);
  45. }
  46. console.log("Attaching receive 'data' handler");
  47. device.on('data', function(data) {
  48. console.log("got data:", data.toString('hex') );
  49. });
  50. device.on('error', function(err) {
  51. console.log("error:",err);
  52. });
  53. var messageA = [];
  54. for(var i=0; i < 64; i++) {
  55. messageA[i] = 120 + i;
  56. }
  57. // for Windows, must prepend report number, even when there isn't one
  58. if( os.platform() == 'win32' ) {
  59. messageA.unshift( 0x00 );
  60. }
  61. console.log("Sending messages to Teensy, watch Teensy Serial Montor for data");
  62. console.log('Sending message A: ', JSON.stringify(messageA))
  63. var numsentA = device.write(messageA);
  64. console.log('messageA len:', messageA.length, 'actual sent len:', numsentA);
  65. var messageB = [];
  66. for(var i=0; i < 64; i++) {
  67. messageB[i] = 1 + i;
  68. }
  69. // for Windows, must prepend report number, even when there isn't one
  70. if( os.platform() == 'win32' ) {
  71. messageB.unshift( 0x00 );
  72. }
  73. console.log('Sending message B: ', JSON.stringify(messageB))
  74. var numsentB = device.write(messageB);
  75. console.log('messageB len:', messageB.length, 'actual sent len:', numsentB);
  76. console.log("Waiting 10 seconds for data from Teensy");
  77. setTimeout( function() {
  78. console.log("Done");
  79. device.close();
  80. }, 10000);