diff options
author | tmk <hasu@tmk-kbd.com> | 2015-05-13 11:13:10 +0900 |
---|---|---|
committer | tmk <hasu@tmk-kbd.com> | 2015-05-13 11:13:10 +0900 |
commit | f1f2066657f4a0998adc016c95d7e541b436e09f (patch) | |
tree | d056cb656b538c4f3a3c205363e0070976655e2e /tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js | |
parent | 1c73e574f109a17566db99e399bdf86007488d2a (diff) | |
parent | f6d56675f9f981c5464f0ca7a1fbb0162154e8c5 (diff) |
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
Diffstat (limited to 'tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js')
m--------- | tmk_core/protocol/lufa/LUFA-git | 0 | ||||
-rwxr-xr-x | tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js | 143 |
2 files changed, 143 insertions, 0 deletions
diff --git a/tmk_core/protocol/lufa/LUFA-git b/tmk_core/protocol/lufa/LUFA-git deleted file mode 160000 -Subproject b6c18b2a7c544653efbe12a1d4e8ba65e7d83c3 diff --git a/tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js b/tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js new file mode 100755 index 0000000000..4203a08e47 --- /dev/null +++ b/tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js @@ -0,0 +1,143 @@ +#!/usr/bin/env node + +// LUFA Library +// Copyright (C) Dean Camera, 2014. +// +// dean [at] fourwalledcubicle [dot] com +// www.lufa-lib.org + +// LUFA Generic HID device demo host test script. This script will send a +// continuous stream of generic reports to the device, to show a variable LED +// pattern on the target board. Send and received report data is printed to +// the terminal. +// +// You have to install the usb and async modules prior to executing this script: +// apt-get install libusb-1.0-0-dev +// npm install usb async sprintf + +var usb = require('usb'); +var async = require('async'); +var sprintf = require('sprintf'); + +var deviceVid = 0x03EB; +var devicePid = 0x204F; +var reportLength = 8; + +function getAndInitHidDeviceAndInterface() +{ + device = usb.findByIds(deviceVid, devicePid); + if (!device) { + console.log('No device found'); + process.exit(1); + } + device.open(); + + var hidInterface = device.interface(0); + if (hidInterface.isKernelDriverActive()) { + hidInterface.detachKernelDriver(); + } + hidInterface.claim(); + + async.series([ + function(callback) { + setConfiguration(0, function(error, data) { + callback(); + }); + } + ]); + + return {hidDevice:device, hidInterface:hidInterface}; +} + +function read(hidInterface, callback) +{ + endpoint = hidInterface.endpoints[0]; + endpoint.transfer(reportLength, function(error, data) { + if (error) { + console.log(error) + } else { + console.log("Received LED Pattern:", data.slice(0, 4)); + } + callback(); + }); +} + +function write(hidDevice, message, callback) +{ + hidDevice.controlTransfer( // Send a Set Report control request + parseInt('00100001', 2), // bmRequestType (constant for this control request) + 0x09, // bmRequest (constant for this control request) + 0x0809, // wValue (MSB is report type, LSB is report number) + 0, // wIndex (interface number) + message, // message to be sent + function(error, data) { // callback to be executed upon finishing the transfer + console.log("Sent LED Pattern:", message.slice(1, 5)) + callback(); + } + ); +} + +function setConfiguration(configurationNumber, callback) +{ + device.controlTransfer( // Send a Set Configuration control request + parseInt('00000000', 2), // bmRequestType + 0x09, // bmRequest + 0, // wValue (Configuration value) + 0, // wIndex + new Buffer(0), // message to be sent + callback // callback to be executed upon finishing the transfer + ); +} + +// @TODO: Fix this function because apparently it doesn't work for some reason. +function getStringDescriptor(stringId, languageId, callback) +{ + var STRING_DESCRIPTOR_TYPE = 0x03; + var wValue = (STRING_DESCRIPTOR_TYPE << 8) | stringId; + + device.controlTransfer( // Send a Get Descriptor control request + parseInt('10000000', 2), // bmRequestType + 0x06, // bmRequest + wValue, // wValue + languageId, // wIndex + 64, // response length + callback // callback to be executed upon finishing the transfer + ); +} + +function setNextPattern() +{ + var pattern = [ + hidInterface.interface, + (p >> 3) & 1, + (p >> 2) & 1, + (p >> 1) & 1, + (p >> 0) & 1 + ]; + + async.series([ + function(callback) { + write(hidDevice, new Buffer(pattern), callback); + }, + function(callback) { + read(hidInterface, callback); + }, + function(callback) { + p = (p + 1) % 16 + setTimeout(setNextPattern, 200); + callback(); + }]); +} + +var hidDeviceAndInterface = getAndInitHidDeviceAndInterface(); +var hidDevice = hidDeviceAndInterface.hidDevice +var hidInterface = hidDeviceAndInterface.hidInterface; + +console.log(sprintf("Connected to device 0x%04X/0x%04X - %s [%s]", + hidDevice.deviceDescriptor.idVendor, + hidDevice.deviceDescriptor.idProduct, + hidDevice.deviceDescriptor.iProduct, + hidDevice.deviceDescriptor.iManufacturer)); + +p = 0 +setNextPattern(); |