From bf630b3add2ed197ec4c9f94f06d4dec4281d09d Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Sun, 7 Jan 2018 18:38:01 +1100 Subject: Update other PyWinUSB Python host scripts to use the hidapi library. --- .../HostTestApp/test_generic_hid_libusb.py | 9 ++-- .../HostTestApp/test_generic_hid_winusb.py | 41 ++++++++---------- .../GenericHID/HostTestApp/test_generic_hid.py | 41 ++++++++---------- .../TempLogHostApp_Python/temp_log_config.py | 49 ++++++++++------------ 4 files changed, 62 insertions(+), 78 deletions(-) diff --git a/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py index a540a2c67..8da8ad767 100755 --- a/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py +++ b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py @@ -22,13 +22,9 @@ 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) + device = usb.core.find(idVendor=0x03EB, idProduct=0x204F) if device is None: sys.exit("Could not find USB device.") @@ -46,6 +42,7 @@ def get_and_init_hid_device(): 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] @@ -62,11 +59,13 @@ def send_led_pattern(device, led1, led2, led3, led4): 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() diff --git a/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_winusb.py b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_winusb.py index efb4cbeaa..72f5f076d 100644 --- a/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_winusb.py +++ b/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_winusb.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + """ LUFA Library Copyright (C) Dean Camera, 2018. @@ -17,37 +19,34 @@ 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 +import hid def get_hid_device_handle(): - hid_device_filter = hid.HidDeviceFilter(vendor_id=device_vid, - product_id=device_pid) + all_hid_devices = hid.enumerate() - valid_hid_devices = hid_device_filter.get_devices() + lufa_hid_devices = [d for d in all_hid_devices if d['vendor_id'] == 0x03EB and d['product_id'] == 0x204F] - if len(valid_hid_devices) is 0: + if len(lufa_hid_devices) is 0: return None - else: - return valid_hid_devices[0] + + device_handle = hid.device() + device_handle.open_path(lufa_hid_devices[0]['path']) + return device_handle 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))) + report_data = bytearray(9) + report_data[0] = 0 + report_data[1] = led1 + report_data[2] = led2 + report_data[3] = led3 + report_data[4] = led4 # Send the generated report to the device - device.send_output_report(report_data) + device.write(report_data) print("Sent LED Pattern: {0}".format(report_data[1:5])) @@ -64,11 +63,7 @@ def main(): 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)) + print("Connected to device.", flush=True) # Set up the HID input report handler to receive reports hid_device.set_raw_data_handler(received_led_pattern) diff --git a/Demos/Device/LowLevel/GenericHID/HostTestApp/test_generic_hid.py b/Demos/Device/LowLevel/GenericHID/HostTestApp/test_generic_hid.py index efb4cbeaa..72f5f076d 100644 --- a/Demos/Device/LowLevel/GenericHID/HostTestApp/test_generic_hid.py +++ b/Demos/Device/LowLevel/GenericHID/HostTestApp/test_generic_hid.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + """ LUFA Library Copyright (C) Dean Camera, 2018. @@ -17,37 +19,34 @@ 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 +import hid def get_hid_device_handle(): - hid_device_filter = hid.HidDeviceFilter(vendor_id=device_vid, - product_id=device_pid) + all_hid_devices = hid.enumerate() - valid_hid_devices = hid_device_filter.get_devices() + lufa_hid_devices = [d for d in all_hid_devices if d['vendor_id'] == 0x03EB and d['product_id'] == 0x204F] - if len(valid_hid_devices) is 0: + if len(lufa_hid_devices) is 0: return None - else: - return valid_hid_devices[0] + + device_handle = hid.device() + device_handle.open_path(lufa_hid_devices[0]['path']) + return device_handle 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))) + report_data = bytearray(9) + report_data[0] = 0 + report_data[1] = led1 + report_data[2] = led2 + report_data[3] = led3 + report_data[4] = led4 # Send the generated report to the device - device.send_output_report(report_data) + device.write(report_data) print("Sent LED Pattern: {0}".format(report_data[1:5])) @@ -64,11 +63,7 @@ def main(): 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)) + print("Connected to device.", flush=True) # Set up the HID input report handler to receive reports hid_device.set_raw_data_handler(received_led_pattern) diff --git a/Projects/TempDataLogger/TempLogHostApp_Python/temp_log_config.py b/Projects/TempDataLogger/TempLogHostApp_Python/temp_log_config.py index b240f1a96..eda06c418 100644 --- a/Projects/TempDataLogger/TempLogHostApp_Python/temp_log_config.py +++ b/Projects/TempDataLogger/TempLogHostApp_Python/temp_log_config.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + """ LUFA Library Copyright (C) Dean Camera, 2018. @@ -25,44 +27,41 @@ import sys from datetime import datetime -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 = 0x2063 -report_length = 1 + 7 +import hid def get_hid_device_handle(): - hid_device_filter = hid.HidDeviceFilter(vendor_id=device_vid, - product_id=device_pid) + all_hid_devices = hid.enumerate() - valid_hid_devices = hid_device_filter.get_devices() + lufa_hid_devices = [d for d in all_hid_devices if d['vendor_id'] == 0x03EB and d['product_id'] == 0x2063] - if len(valid_hid_devices) is 0: + if len(lufa_hid_devices) is 0: return None - else: - return valid_hid_devices[0] + + device_handle = hid.device() + device_handle.open_path(lufa_hid_devices[0]['path']) + return device_handle def configure_temp_log_device(device, time_date, log_interval_500ms): + report_data = bytearray(8) + # Report data for the demo is the report ID (always zero) - report_data = [0] + report_data[0] = 0 # Followed by the time/date data - report_data.extend([time_date.hour, time_date.minute, - time_date.second, time_date.day, - time_date.month, time_date.year - 2000]) + report_data[1] = time_date.hour + report_data[2] = time_date.minute, + report_data[3] = time_date.second + report_data[4] = time_date.day, + report_data[5] = time_date.month + report_data[6] = time_date.year - 2000 # Lastly the log interval in 500ms units of time - report_data.extend([log_interval_500ms]) - - # Zero-extend the array to the length the report should be - report_data.extend([0] * (report_length - len(report_data))) + report_data[7] = log_interval_500ms # Send the generated report to the device - device.send_output_report(report_data) + device.write(report_data) def main(time_date, log_interval_500ms): @@ -73,11 +72,7 @@ def main(time_date, log_interval_500ms): 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)) + print("Connected to device.", flush=True) configure_temp_log_device(hid_device, time_date, log_interval_500ms) -- cgit v1.2.3