diff options
author | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
---|---|---|
committer | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
commit | 60b30c036397cb5627fa374bb930794b225daa29 (patch) | |
tree | e156c2d6b27f01db6985f2cc560c9c345d48fdac /Demos/Device/ClassDriver/GenericHID/HostTestApp |
Squashed 'lib/lufa/' content from commit 385d40300
git-subtree-dir: lib/lufa
git-subtree-split: 385d4030035dbaf41591309dbde47653bd03841b
Diffstat (limited to 'Demos/Device/ClassDriver/GenericHID/HostTestApp')
3 files changed, 337 insertions, 0 deletions
diff --git a/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js new file mode 100755 index 0000000000..363786429a --- /dev/null +++ b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.js @@ -0,0 +1,143 @@ +#!/usr/bin/env node + +// LUFA Library +// Copyright (C) Dean Camera, 2017. +// +// 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(); diff --git a/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py new file mode 100755 index 0000000000..4efa4a0ab4 --- /dev/null +++ b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python + +""" + LUFA Library + Copyright (C) Dean Camera, 2017. + + 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. + + Requires the PyUSB library (http://sourceforge.net/apps/trac/pyusb/). +""" + +import sys +from time import sleep +import usb.core +import usb.util + +# Generic HID device VID, PID and report payload length (length is increased +# by one to account for the Report ID byte that must be pre-pended) +device_vid = 0x03EB +device_pid = 0x204F + +def get_and_init_hid_device(): + device = usb.core.find(idVendor=device_vid, idProduct=device_pid) + + if device is None: + sys.exit("Could not find USB device.") + + if device.is_kernel_driver_active(0): + try: + device.detach_kernel_driver(0) + except usb.core.USBError as exception: + sys.exit("Could not detatch kernel driver: %s" % str(exception)) + + try: + device.set_configuration() + except usb.core.USBError as exception: + sys.exit("Could not set configuration: %s" % str(exception)) + + return device + +def send_led_pattern(device, led1, led2, led3, led4): + # Report data for the demo is LED on/off data + report_data = [led1, led2, led3, led4] + + # Send the generated report to the device + number_of_bytes_written = device.ctrl_transfer( # Set Report control request + 0b00100001, # bmRequestType (constant for this control request) + 0x09, # bmRequest (constant for this control request) + 0, # wValue (MSB is report type, LSB is report number) + 0, # wIndex (interface number) + report_data # report data to be sent + ); + assert number_of_bytes_written == len(report_data) + + print("Sent LED Pattern: {0}".format(report_data)) + +def receive_led_pattern(hid_device): + endpoint = hid_device[0][(0,0)][0] + report_data = hid_device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize) + return list(report_data) + +def main(): + hid_device = get_and_init_hid_device() + + print("Connected to device 0x%04X/0x%04X - %s [%s]" % + (hid_device.idVendor, hid_device.idProduct, + usb.util.get_string(hid_device, 256, hid_device.iProduct), + usb.util.get_string(hid_device, 256, hid_device.iManufacturer))) + + p = 0 + while (True): + # Convert the current pattern index to a bit-mask and send + send_led_pattern(hid_device, + (p >> 3) & 1, + (p >> 2) & 1, + (p >> 1) & 1, + (p >> 0) & 1) + + # Receive and print the current LED pattern + led_pattern = receive_led_pattern(hid_device)[0:4] + print("Received LED Pattern: {0}".format(led_pattern)) + + # Compute next LED pattern in sequence + p = (p + 1) % 16 + + # Delay a bit for visual effect + sleep(.2) + +if __name__ == '__main__': + main() diff --git a/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_winusb.py b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_winusb.py new file mode 100644 index 0000000000..1e5f430195 --- /dev/null +++ b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_winusb.py @@ -0,0 +1,96 @@ +""" + LUFA Library + Copyright (C) Dean Camera, 2017. + + 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. + + Requires the pywinusb library (https://pypi.python.org/pypi/pywinusb/). +""" + +import sys +from time import sleep +import pywinusb.hid as hid + +# Generic HID device VID, PID and report payload length (length is increased +# by one to account for the Report ID byte that must be pre-pended) +device_vid = 0x03EB +device_pid = 0x204F +report_length = 1 + 8 + + +def get_hid_device_handle(): + hid_device_filter = hid.HidDeviceFilter(vendor_id=device_vid, + product_id=device_pid) + + valid_hid_devices = hid_device_filter.get_devices() + + if len(valid_hid_devices) is 0: + return None + else: + return valid_hid_devices[0] + + +def send_led_pattern(device, led1, led2, led3, led4): + # Report data for the demo is the report ID (always zero) followed by the + # LED on/off data + report_data = [0, led1, led2, led3, led4] + + # Zero-extend the array to the length the report should be + report_data.extend([0] * (report_length - len(report_data))) + + # Send the generated report to the device + device.send_output_report(report_data) + + print("Sent LED Pattern: {0}".format(report_data[1:5])) + + +def received_led_pattern(report_data): + print("Received LED Pattern: {0}".format(report_data[1:5])) + + +def main(): + hid_device = get_hid_device_handle() + + if hid_device is None: + print("No valid HID device found.") + sys.exit(1) + + try: + hid_device.open() + + print("Connected to device 0x%04X/0x%04X - %s [%s]" % + (hid_device.vendor_id, hid_device.product_id, + hid_device.product_name, hid_device.vendor_name)) + + # Set up the HID input report handler to receive reports + hid_device.set_raw_data_handler(received_led_pattern) + + p = 0 + while (hid_device.is_plugged()): + # Convert the current pattern index to a bit-mask and send + send_led_pattern(hid_device, + (p >> 3) & 1, + (p >> 2) & 1, + (p >> 1) & 1, + (p >> 0) & 1) + + # Compute next LED pattern in sequence + p = (p + 1) % 16 + + # Delay a bit for visual effect + sleep(.2) + + finally: + hid_device.close() + + +if __name__ == '__main__': + main() |