]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / GenericHID / HostTestApp / test_generic_hid_libusb.js
1 #!/usr/bin/env node
2
3 // LUFA Library
4 // Copyright (C) Dean Camera, 2014.
5 //
6 // dean [at] fourwalledcubicle [dot] com
7 // www.lufa-lib.org
8
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
12 // the terminal.
13 //
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
17
18 var usb = require('usb');
19 var async = require('async');
20 var sprintf = require('sprintf');
21
22 var deviceVid = 0x03EB;
23 var devicePid = 0x204F;
24 var reportLength = 8;
25
26 function getAndInitHidDeviceAndInterface()
27 {
28 device = usb.findByIds(deviceVid, devicePid);
29 if (!device) {
30 console.log('No device found');
31 process.exit(1);
32 }
33 device.open();
34
35 var hidInterface = device.interface(0);
36 if (hidInterface.isKernelDriverActive()) {
37 hidInterface.detachKernelDriver();
38 }
39 hidInterface.claim();
40
41 async.series([
42 function(callback) {
43 setConfiguration(0, function(error, data) {
44 callback();
45 });
46 }
47 ]);
48
49 return {hidDevice:device, hidInterface:hidInterface};
50 }
51
52 function read(hidInterface, callback)
53 {
54 endpoint = hidInterface.endpoints[0];
55 endpoint.transfer(reportLength, function(error, data) {
56 if (error) {
57 console.log(error)
58 } else {
59 console.log("Received LED Pattern:", data.slice(0, 4));
60 }
61 callback();
62 });
63 }
64
65 function write(hidDevice, message, callback)
66 {
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))
75 callback();
76 }
77 );
78 }
79
80 function setConfiguration(configurationNumber, callback)
81 {
82 device.controlTransfer( // Send a Set Configuration control request
83 parseInt('00000000', 2), // bmRequestType
84 0x09, // bmRequest
85 0, // wValue (Configuration value)
86 0, // wIndex
87 new Buffer(0), // message to be sent
88 callback // callback to be executed upon finishing the transfer
89 );
90 }
91
92 // @TODO: Fix this function because apparently it doesn't work for some reason.
93 function getStringDescriptor(stringId, languageId, callback)
94 {
95 var STRING_DESCRIPTOR_TYPE = 0x03;
96 var wValue = (STRING_DESCRIPTOR_TYPE << 8) | stringId;
97
98 device.controlTransfer( // Send a Get Descriptor control request
99 parseInt('10000000', 2), // bmRequestType
100 0x06, // bmRequest
101 wValue, // wValue
102 languageId, // wIndex
103 64, // response length
104 callback // callback to be executed upon finishing the transfer
105 );
106 }
107
108 function setNextPattern()
109 {
110 var pattern = [
111 hidInterface.interface,
112 (p >> 3) & 1,
113 (p >> 2) & 1,
114 (p >> 1) & 1,
115 (p >> 0) & 1
116 ];
117
118 async.series([
119 function(callback) {
120 write(hidDevice, new Buffer(pattern), callback);
121 },
122 function(callback) {
123 read(hidInterface, callback);
124 },
125 function(callback) {
126 p = (p + 1) % 16
127 setTimeout(setNextPattern, 200);
128 callback();
129 }]);
130 }
131
132 var hidDeviceAndInterface = getAndInitHidDeviceAndInterface();
133 var hidDevice = hidDeviceAndInterface.hidDevice
134 var hidInterface = hidDeviceAndInterface.hidInterface;
135
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));
141
142 p = 0
143 setNextPattern();
Imprint / Impressum