Blame view

src/lufa-LUFA-170418/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py 3.09 KB
ca85a266   gperson   le vrai commit, c...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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()