Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_generic_hid_libusb.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env node
  2. // LUFA Library
  3. // Copyright (C) Dean Camera, 2014.
  4. //
  5. // dean [at] fourwalledcubicle [dot] com
  6. // www.lufa-lib.org
  7. // LUFA Generic HID device demo host test script. This script will send a
  8. // continuous stream of generic reports to the device, to show a variable LED
  9. // pattern on the target board. Send and received report data is printed to
  10. // the terminal.
  11. //
  12. // You have to install the usb and async modules prior to executing this script:
  13. // apt-get install libusb-1.0-0-dev
  14. // npm install usb async sprintf
  15. var usb = require('usb');
  16. var async = require('async');
  17. var sprintf = require('sprintf');
  18. var deviceVid = 0x03EB;
  19. var devicePid = 0x204F;
  20. var reportLength = 8;
  21. function getAndInitHidDeviceAndInterface()
  22. {
  23. device = usb.findByIds(deviceVid, devicePid);
  24. if (!device) {
  25. console.log('No device found');
  26. process.exit(1);
  27. }
  28. device.open();
  29. var hidInterface = device.interface(0);
  30. if (hidInterface.isKernelDriverActive()) {
  31. hidInterface.detachKernelDriver();
  32. }
  33. hidInterface.claim();
  34. async.series([
  35. function(callback) {
  36. setConfiguration(0, function(error, data) {
  37. callback();
  38. });
  39. }
  40. ]);
  41. return {hidDevice:device, hidInterface:hidInterface};
  42. }
  43. function read(hidInterface, callback)
  44. {
  45. endpoint = hidInterface.endpoints[0];
  46. endpoint.transfer(reportLength, function(error, data) {
  47. if (error) {
  48. console.log(error)
  49. } else {
  50. console.log("Received LED Pattern:", data.slice(0, 4));
  51. }
  52. callback();
  53. });
  54. }
  55. function write(hidDevice, message, callback)
  56. {
  57. hidDevice.controlTransfer( // Send a Set Report control request
  58. parseInt('00100001', 2), // bmRequestType (constant for this control request)
  59. 0x09, // bmRequest (constant for this control request)
  60. 0x0809, // wValue (MSB is report type, LSB is report number)
  61. 0, // wIndex (interface number)
  62. message, // message to be sent
  63. function(error, data) { // callback to be executed upon finishing the transfer
  64. console.log("Sent LED Pattern:", message.slice(1, 5))
  65. callback();
  66. }
  67. );
  68. }
  69. function setConfiguration(configurationNumber, callback)
  70. {
  71. device.controlTransfer( // Send a Set Configuration control request
  72. parseInt('00000000', 2), // bmRequestType
  73. 0x09, // bmRequest
  74. 0, // wValue (Configuration value)
  75. 0, // wIndex
  76. new Buffer(0), // message to be sent
  77. callback // callback to be executed upon finishing the transfer
  78. );
  79. }
  80. // @TODO: Fix this function because apparently it doesn't work for some reason.
  81. function getStringDescriptor(stringId, languageId, callback)
  82. {
  83. var STRING_DESCRIPTOR_TYPE = 0x03;
  84. var wValue = (STRING_DESCRIPTOR_TYPE << 8) | stringId;
  85. device.controlTransfer( // Send a Get Descriptor control request
  86. parseInt('10000000', 2), // bmRequestType
  87. 0x06, // bmRequest
  88. wValue, // wValue
  89. languageId, // wIndex
  90. 64, // response length
  91. callback // callback to be executed upon finishing the transfer
  92. );
  93. }
  94. function setNextPattern()
  95. {
  96. var pattern = [
  97. hidInterface.interface,
  98. (p >> 3) & 1,
  99. (p >> 2) & 1,
  100. (p >> 1) & 1,
  101. (p >> 0) & 1
  102. ];
  103. async.series([
  104. function(callback) {
  105. write(hidDevice, new Buffer(pattern), callback);
  106. },
  107. function(callback) {
  108. read(hidInterface, callback);
  109. },
  110. function(callback) {
  111. p = (p + 1) % 16
  112. setTimeout(setNextPattern, 200);
  113. callback();
  114. }]);
  115. }
  116. var hidDeviceAndInterface = getAndInitHidDeviceAndInterface();
  117. var hidDevice = hidDeviceAndInterface.hidDevice
  118. var hidInterface = hidDeviceAndInterface.hidInterface;
  119. console.log(sprintf("Connected to device 0x%04X/0x%04X - %s [%s]",
  120. hidDevice.deviceDescriptor.idVendor,
  121. hidDevice.deviceDescriptor.idProduct,
  122. hidDevice.deviceDescriptor.iProduct,
  123. hidDevice.deviceDescriptor.iManufacturer));
  124. p = 0
  125. setNextPattern();