Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gamepad.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python3
  2. # Uses the Python evdev wrapper:
  3. # https://github.com/gvalkov/python-evdev
  4. #
  5. # ----------------------------------------------------------------------------
  6. # "THE BEER-WARE LICENSE" (Revision 42):
  7. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  8. # you can do whatever you want with this stuff. If we meet some day, and you
  9. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  10. # ----------------------------------------------------------------------------
  11. from evdev import InputDevice, list_devices, ecodes, categorize
  12. from selectors import DefaultSelector, EVENT_READ
  13. class InputWrapper:
  14. def __init__(self):
  15. self.devices = []
  16. self.selector = DefaultSelector()
  17. self.keys = {
  18. "left": False,
  19. "right": False,
  20. "up": False,
  21. "down": False,
  22. "a": False,
  23. "b": False,
  24. "x": False,
  25. "y": False,
  26. "l": False,
  27. "r": False,
  28. "start": False,
  29. "select": False,
  30. }
  31. devices = [InputDevice(path) for path in list_devices()]
  32. for device in devices:
  33. c = device.capabilities(absinfo=False)
  34. keep = False
  35. # check for axis events
  36. if ecodes.EV_ABS in c:
  37. # check for gamepad
  38. if ecodes.ABS_X in c[ecodes.EV_ABS]:
  39. print("Gamepad detected: ", device.name)
  40. keep = True
  41. # check for key events
  42. if ecodes.EV_KEY in c:
  43. # check for arrow keys
  44. if ecodes.KEY_LEFT in c[ecodes.EV_KEY]:
  45. print("Keyboard detected:", device.name)
  46. keep = True
  47. if not keep:
  48. continue
  49. d = InputDevice(device.path)
  50. self.devices.append(d)
  51. self.selector.register(d, EVENT_READ)
  52. def get(self, verbose = False):
  53. for key, mask in self.selector.select(0):
  54. device = key.fileobj
  55. for event in device.read():
  56. if event.type == ecodes.EV_KEY:
  57. v = False
  58. if event.value != 0:
  59. v = True
  60. if (event.code == ecodes.KEY_LEFT) or (event.code == ecodes.BTN_WEST):
  61. self.keys["left"] = v
  62. elif (event.code == ecodes.KEY_RIGHT) or (event.code == ecodes.BTN_EAST):
  63. self.keys["right"] = v
  64. elif (event.code == ecodes.KEY_UP) or (event.code == ecodes.BTN_NORTH):
  65. self.keys["up"] = v
  66. elif (event.code == ecodes.KEY_DOWN) or (event.code == ecodes.BTN_SOUTH):
  67. self.keys["down"] = v
  68. elif (event.code == ecodes.KEY_SPACE) or (event.code == ecodes.BTN_A) or (event.code == ecodes.BTN_THUMB):
  69. self.keys["a"] = v
  70. elif (event.code == ecodes.KEY_LEFTCTRL) or (event.code == ecodes.BTN_B) or (event.code == ecodes.BTN_THUMB2):
  71. self.keys["b"] = v
  72. elif (event.code == ecodes.KEY_LEFTALT) or (event.code == ecodes.BTN_X) or (event.code == ecodes.BTN_JOYSTICK) or (event.code == ecodes.BTN_TRIGGER):
  73. self.keys["x"] = v
  74. elif (event.code == ecodes.KEY_ENTER) or (event.code == ecodes.BTN_Y) or (event.code == ecodes.BTN_TOP):
  75. self.keys["y"] = v
  76. elif (event.code == ecodes.BTN_PINKIE) or (event.code == ecodes.KEY_D):
  77. self.keys["r"] = v
  78. elif (event.code == ecodes.BTN_TOP2) or (event.code == ecodes.KEY_A):
  79. self.keys["l"] = v
  80. elif (event.code == ecodes.BTN_BASE4) or (event.code == ecodes.KEY_BACKSPACE):
  81. self.keys["start"] = v
  82. elif event.code == ecodes.BTN_BASE3:
  83. self.keys["select"] = v
  84. elif event.type == ecodes.EV_ABS:
  85. # TODO 8bit hardcoded
  86. if event.code == ecodes.ABS_X:
  87. if event.value > 200:
  88. self.keys["right"] = True
  89. self.keys["left"] = False
  90. elif event.value < 100:
  91. self.keys["right"] = False
  92. self.keys["left"] = True
  93. else:
  94. self.keys["right"] = False
  95. self.keys["left"] = False
  96. elif event.code == ecodes.ABS_Y:
  97. if event.value > 200:
  98. self.keys["down"] = True
  99. self.keys["up"] = False
  100. elif event.value < 100:
  101. self.keys["down"] = False
  102. self.keys["up"] = True
  103. else:
  104. self.keys["down"] = False
  105. self.keys["up"] = False
  106. if verbose:
  107. print(categorize(event), event)
  108. return self.keys
  109. if __name__ == "__main__":
  110. from pprint import pprint
  111. import sys
  112. if len(sys.argv) > 1:
  113. devices = [InputDevice(path) for path in list_devices()]
  114. for device in devices:
  115. print(device.path, device.name, device.phys)
  116. pprint(device.capabilities(verbose=True))
  117. print()
  118. else:
  119. i = InputWrapper()
  120. while True:
  121. i.get(True)