/iohub_docs/iohub/api_and_manual/device_details/xinput_gamepad.rst

https://github.com/smathot/psychopy · ReStructuredText · 73 lines · 52 code · 21 blank · 0 comment · 0 complexity · 11843a40dff3fdb10b8d4dadc7dba17e MD5 · raw file

  1. ###########################
  2. The XInput Gamepad Device
  3. ###########################
  4. **Platforms:** Windows
  5. .. autoclass:: psychopy.iohub.devices.xinput.Gamepad
  6. :exclude-members: DEVICE_LABEL , ALL_EVENT_CLASSES, CLASS_ATTRIBUTE_NAMES, DEVICE_BUFFER_LENGTH_INDEX, DEVICE_CLASS_NAME_INDEX, DEVICE_MAX_ATTRIBUTE_INDEX, DEVICE_TIMEBASE_TO_SEC, DEVICE_TYPE_ID, DEVICE_TYPE_ID_INDEX, DEVICE_TYPE_STRING, DEVICE_USER_LABEL_INDEX, NUMPY_DTYPE, e, DEVICE_FIRMWARE_VERSION_INDEX, DEVICE_HARDWARE_VERSION_INDEX,DEVICE_MANUFACTURER_NAME_INDEX,DEVICE_MODEL_NAME_INDEX, DEVICE_MODEL_NUMBER_INDEX, DEVICE_NUMBER_INDEX, DEVICE_SERIAL_NUMBER_INDEX, DEVICE_SOFTWARE_VERSION_INDEX, EVENT_CLASS_NAMES
  7. :member-order: bysource
  8. GamePad Device Configuration Settings
  9. ########################################
  10. .. literalinclude:: default_yaml_configs/default_xinput.yaml
  11. :language: yaml
  12. GamePad Event Types
  13. ########################
  14. .. autoclass:: psychopy.iohub.devices.xinput.GamepadStateChangeEvent
  15. :exclude-members: DEVICE_ID_INDEX, filter_id, device_id, NUMPY_DTYPE, BASE_EVENT_MAX_ATTRIBUTE_INDEX, CLASS_ATTRIBUTE_NAMES, EVENT_CONFIDENCE_INTERVAL_INDEX, EVENT_DELAY_INDEX, EVENT_DEVICE_TIME_INDEX, EVENT_EXPERIMENT_ID_INDEX, EVENT_FILTER_ID_INDEX, EVENT_HUB_TIME_INDEX, EVENT_ID_INDEX, EVENT_LOGGED_TIME_INDEX, EVENT_SESSION_ID_INDEX, EVENT_TYPE_ID, EVENT_TYPE_ID_INDEX, EVENT_TYPE_STRING, IOHUB_DATA_TABLE, PARENT_DEVICE, createEventAsClass, createEventAsDict, createEventAsNamedTuple, e, namedTupleClass
  16. :member-order: bysource
  17. Notes and Considerations
  18. ###########################
  19. ..note:: If gamepad thumbstick position data is going to be used to control
  20. the position of a stim object on the PsychoPy Window, the following equation
  21. can be used to convert the normalized thumbstick data to Display coordinates::
  22. def normalizedValue2Coord(normalized_position,normalized_magnitude,display_coord_dim_size):
  23. x,y=normalized_position[0]*normalized_magnitude,normalized_position[1]*normalized_magnitude
  24. w,h=display_coord_dim_size
  25. return x*(w/2.0),y*(h/2.0)
  26. # example usage:
  27. display=io.devices.display
  28. gamepad=io.devices.gamepad
  29. keyboard=io.devices.keyboard
  30. # create a PsychoPy stim to move with each thumb stick
  31. #
  32. # thumb_left_stim = .......
  33. # thumb_right_stim = .......
  34. dl,dt,dr,db=display.getCoordBounds()
  35. coord_size=dr-dl,dt-db
  36. io.clearEvents('all')
  37. while not keyboard.getEvents():
  38. # thumb stick state is returned as a 3 item lists (x , y , magnitude)
  39. x,y,mag=gamepad.getThumbSticks()['right_stick']
  40. xx,yy=self.normalizedValue2Coord((x,y),mag,coord_size)
  41. thumb_right_stim.setPos((xx, yy))
  42. # thumb stick state is returned as a 3 item lists (x , y , magnitude)
  43. x,y,mag=gamepad.getThumbSticks()['left_stick'] # sticks are 3 item lists (x,y,magnitude)
  44. xx,yy=self.normalizedValue2Coord((x,y),mag,coord_size)
  45. thumb_left_stim.setPos((xx, yy))
  46. thumb_right_stim.draw()
  47. thumb_left_stim.draw()
  48. io.clearEvents('all')
  49. window.flip()
  50. * Ensure that XInput version 1.3 is installed on your computer.
  51. * If using a wireless gamepad, ensure the gamepad has been powered on befor stating the experiment.
  52. * For the supported Logitech gamepads, be sure that the switch on the gamepad is set to the 'X' position, indicating that the gamepad will use the XInput protocal.