Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Original Author: Michael Lelli <[email protected]>
  4.  
  5. import usb.core
  6. import usb.util
  7. #import uinput
  8. import sys
  9. import getopt
  10.  
  11. controllers = [None, None, None, None]
  12. controllers_state = [None, None, None, None]
  13.  
  14. DIGITAL_BUTTONS = {
  15.   'BTN_DPAD_UP':    0x8000,
  16.   'BTN_DPAD_DOWN':  0x4000,
  17.   'BTN_DPAD_LEFT':  0x1000,
  18.   'BTN_DPAD_RIGHT': 0x2000,
  19.   'BTN_NORTH':      0x0800,
  20.   'BTN_SOUTH':      0x0100,
  21.   'BTN_EAST':       0x0400,
  22.   'BTN_WEST':       0x0200,
  23.   'BTN_START':      0x0001,
  24.   'BTN_TL':         0x0008,
  25.   'BTN_TR':         0x0004,
  26.   'BTN_TR2':        0x0002
  27. }
  28.  
  29. AXIS_BYTES = {
  30.     'ABS_X':  3,
  31.     'ABS_Y':  4,
  32.     'ABS_RX': 5,
  33.     'ABS_RY': 6,
  34.     'ABS_Z':  7,
  35.     'ABS_RZ': 8
  36. }
  37.  
  38. def create_device(index, raw):
  39.   if raw:
  40.     axis_cal = (0, 255, 0, 0)
  41.     cstick_cal = (0, 255, 0, 0)
  42.     trigger_cal = (0, 255, 0, 0)
  43.   else:
  44.     axis_cal = (20, 235, 0, 0)
  45.     cstick_cal = (30, 225, 0, 0)
  46.     trigger_cal = (25, 225, 0, 0)
  47.  
  48. #  controllers[index] = uinput.Device(events, name="Wii U GameCube Adapter Port {}".format(index+1))
  49.   controllers_state[index] = (
  50.     0,
  51.     {
  52.       'ABS_X':  -1,
  53.       'ABS_Y':  -1,
  54.       'ABS_RX': -1,
  55.       'ABS_RY': -1,
  56.       'ABS_Z':  -1,
  57.       'ABS_RZ': -1
  58.     }
  59.   )
  60.  
  61. STATE_NORMAL   = 0x10
  62. STATE_WAVEBIRD = 0x20
  63.  
  64. def is_connected(state):
  65.   return state & (STATE_NORMAL | STATE_WAVEBIRD) != 0
  66.  
  67. def help():
  68.   print "usage: " + sys.argv[0] + " [-h/--help] [-r/--raw]"
  69.   print
  70.   print "    -h/--help: display this message"
  71.   print "    -r/--raw:  do not do scaling on axis"
  72.   sys.exit(-1)
  73.  
  74. def main():
  75.   dev = usb.core.find(find_all=True)
  76.   print 'Devices: {}'.format(dev)
  77.   for cfg in dev:
  78.     print 'VendorID={:02x}, ProductID={:02x}'.format(cfg.idVendor, cfg.idProduct)
  79.   dev = usb.core.find(idVendor=0x057e, idProduct=0x0337)
  80.   raw_mode = False
  81.  
  82.   try:
  83.     opts, args = getopt.getopt(sys.argv[1:], "hr", ["help", "raw"])
  84.   except getopt.GetoptError:
  85.     help()
  86.  
  87.   for opt, arg in opts:
  88.     if opt in ("-h", "--help"):
  89.       help()
  90.     elif opt in ("-r", "--raw"):
  91.       print "raw mode"
  92.       raw_mode = True
  93.  
  94.   if dev is None:
  95.     raise ValueError('GC adapter not found')
  96.  
  97. #  if dev.is_kernel_driver_active(0):
  98. #    reattach = True
  99. #    dev.detach_kernel_driver(0)
  100.  
  101.   dev.set_configuration()
  102.   cfg = dev.get_active_configuration()
  103.   intf = cfg[(0,0)]
  104.  
  105.   out_ep = usb.util.find_descriptor(
  106.     intf,
  107.     custom_match = \
  108.     lambda e: \
  109.       usb.util.endpoint_direction(e.bEndpointAddress) == \
  110.       usb.util.ENDPOINT_OUT)
  111.  
  112.   in_ep = usb.util.find_descriptor(
  113.     intf,
  114.     custom_match = \
  115.     lambda e: \
  116.       usb.util.endpoint_direction(e.bEndpointAddress) == \
  117.       usb.util.ENDPOINT_IN)
  118.  
  119.   # might not be necessary, but doesn't hurt
  120.   dev.ctrl_transfer(0x21, 11, 0x0001, 0, [])
  121.  
  122.   out_ep.write([0x13])
  123.  
  124.   try:
  125.     while 1:
  126.       try:
  127.         data = in_ep.read(37)
  128.       except (KeyboardInterrupt, SystemExit):
  129.         raise
  130.       except:
  131.         print "read error"
  132.         continue
  133.       if data[0] != 0x21:
  134.         print "unknown message {:02x}}".format(data[0])
  135.         continue
  136.  
  137.       payloads = [data[1:10], data[10:19], data[19:28], data[28:37]]
  138.  
  139.       index = 0
  140.  
  141.       for i, d in enumerate(payloads):
  142.         #print 'Controller {}'.format(i)
  143.         status = d[0]
  144.         # check for connected
  145.         if is_connected(status) and controllers[i] is None:
  146.           print 'Controller {} is connected'.format(i)
  147.           create_device(i, raw_mode)
  148.           controllers[i] = i
  149.         elif not is_connected(status):
  150.           controllers[i] = None
  151.  
  152.         if controllers[i] is None:
  153.           continue
  154.  
  155.         # if status & 0x04 != 0:
  156.         #   do something about having both USB plugs connected
  157.  
  158.         btns = d[1] << 8 | d[2]
  159.         newmask = 0
  160.         for btn, mask in DIGITAL_BUTTONS.iteritems():
  161.           pressed = btns & mask
  162.           if pressed:
  163.             print 'Button {}'.format(btn)
  164.           newmask |= pressed
  165.  
  166.           # state change
  167. #          if controllers_state[i][0] & mask != pressed:
  168. #            controllers[i].emit(btn, 1 if pressed != 0 else 0, syn=False)
  169.  
  170.         newaxis = {}
  171.         for axis, offset in AXIS_BYTES.iteritems():
  172.           value = d[offset]
  173. #          if value != 0:
  174. #            print 'Axis {}: {}'.format(axis, value)
  175.           newaxis[axis] = value
  176.           if axis == 'ABS_Y' or axis == 'ABS_RY':
  177.             # flip from 0 - 255 to 255 - 0
  178.             value ^= 0xFF
  179.           #elif axis == uinput.ABS_RZ or axis == uinput.ABS_Z:
  180.             # scale from 0 - 255 to 127 - 255
  181.             #value = (value >> 1) + 127
  182.  
  183. #          if controllers_state[i][1][axis] != value:
  184. #            controllers[i].emit(axis, value, syn=False)
  185.  
  186. #        controllers[i].syn()
  187.         controllers_state[i] = (
  188.           newmask,
  189.           newaxis
  190.         )
  191.  
  192.   except:
  193.     raise
  194.  
  195. if __name__ == "__main__":
  196.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement