4 // Copyright (C) Dean Camera, 2014.
6 // dean [at] fourwalledcubicle [dot] com
9 // LUFA Generic HID device demo host test script. This script will send a
10 // continuous stream of generic reports to the device, to show a variable LED
11 // pattern on the target board. Send and received report data is printed to
14 // You have to install the usb and async modules prior to executing this script:
15 // apt-get install libusb-1.0-0-dev
16 // npm install usb async sprintf
18 var usb = require('usb');
19 var async = require('async');
20 var sprintf = require('sprintf');
22 var deviceVid = 0x03EB;
23 var devicePid = 0x204F;
26 function getAndInitHidDeviceAndInterface()
28 device = usb.findByIds(deviceVid, devicePid);
30 console.log('No device found');
35 var hidInterface = device.interface(0);
36 if (hidInterface.isKernelDriverActive()) {
37 hidInterface.detachKernelDriver();
43 setConfiguration(0, function(error, data) {
49 return {hidDevice:device, hidInterface:hidInterface};
52 function read(hidInterface, callback)
54 endpoint = hidInterface.endpoints[0];
55 endpoint.transfer(reportLength, function(error, data) {
59 console.log("Received LED Pattern:", data.slice(0, 4));
65 function write(hidDevice, message, callback)
67 hidDevice.controlTransfer( // Send a Set Report control request
68 parseInt('00100001', 2), // bmRequestType (constant for this control request)
69 0x09, // bmRequest (constant for this control request)
70 0x0809, // wValue (MSB is report type, LSB is report number)
71 0, // wIndex (interface number)
72 message, // message to be sent
73 function(error, data) { // callback to be executed upon finishing the transfer
74 console.log("Sent LED Pattern:", message.slice(1, 5))
80 function setConfiguration(configurationNumber, callback)
82 device.controlTransfer( // Send a Set Configuration control request
83 parseInt('00000000', 2), // bmRequestType
85 0, // wValue (Configuration value)
87 new Buffer(0), // message to be sent
88 callback // callback to be executed upon finishing the transfer
92 // @TODO: Fix this function because apparently it doesn't work for some reason.
93 function getStringDescriptor(stringId, languageId, callback)
95 var STRING_DESCRIPTOR_TYPE = 0x03;
96 var wValue = (STRING_DESCRIPTOR_TYPE << 8) | stringId;
98 device.controlTransfer( // Send a Get Descriptor control request
99 parseInt('10000000', 2), // bmRequestType
102 languageId, // wIndex
103 64, // response length
104 callback // callback to be executed upon finishing the transfer
108 function setNextPattern()
111 hidInterface.interface,
120 write(hidDevice, new Buffer(pattern), callback);
123 read(hidInterface, callback);
127 setTimeout(setNextPattern, 200);
132 var hidDeviceAndInterface = getAndInitHidDeviceAndInterface();
133 var hidDevice = hidDeviceAndInterface.hidDevice
134 var hidInterface = hidDeviceAndInterface.hidInterface;
136 console.log(sprintf("Connected to device 0x%04X/0x%04X - %s [%s]",
137 hidDevice.deviceDescriptor.idVendor,
138 hidDevice.deviceDescriptor.idProduct,
139 hidDevice.deviceDescriptor.iProduct,
140 hidDevice.deviceDescriptor.iManufacturer));