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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }
  27. devices = [InputDevice(path) for path in list_devices()]
  28. for device in devices:
  29. c = device.capabilities()
  30. keep = False
  31. # check for key events
  32. if ecodes.EV_KEY in c:
  33. # check for gamepad
  34. if ecodes.BTN_A in c[ecodes.EV_KEY]:
  35. print("Gamepad detected: ", device.name)
  36. keep = True
  37. # check for arrow keys
  38. elif ecodes.KEY_LEFT in c[ecodes.EV_KEY]:
  39. print("Keyboard detected:", device.name)
  40. keep = True
  41. if not keep:
  42. continue
  43. d = InputDevice(device.path)
  44. self.devices.append(d)
  45. self.selector.register(d, EVENT_READ)
  46. def get(self, verbose = False):
  47. for key, mask in self.selector.select(0):
  48. device = key.fileobj
  49. for event in device.read():
  50. if event.type != ecodes.EV_KEY:
  51. continue
  52. v = False
  53. if event.value != 0:
  54. v = True
  55. if (event.code == ecodes.KEY_LEFT) or (event.code == ecodes.BTN_WEST):
  56. self.keys["left"] = v
  57. elif (event.code == ecodes.KEY_RIGHT) or (event.code == ecodes.BTN_EAST):
  58. self.keys["right"] = v
  59. elif (event.code == ecodes.KEY_UP) or (event.code == ecodes.BTN_NORTH):
  60. self.keys["up"] = v
  61. elif (event.code == ecodes.KEY_DOWN) or (event.code == ecodes.BTN_SOUTH):
  62. self.keys["down"] = v
  63. elif (event.code == ecodes.KEY_SPACE) or (event.code == ecodes.BTN_A):
  64. self.keys["a"] = v
  65. elif (event.code == ecodes.KEY_LEFTCTRL) or (event.code == ecodes.BTN_B):
  66. self.keys["b"] = v
  67. elif (event.code == ecodes.KEY_LEFTALT) or (event.code == ecodes.BTN_X):
  68. self.keys["x"] = v
  69. elif (event.code == ecodes.KEY_ENTER) or (event.code == ecodes.BTN_Y):
  70. self.keys["y"] = v
  71. else:
  72. return self.keys
  73. if verbose:
  74. print(categorize(event), event)
  75. return self.keys
  76. if __name__ == "__main__":
  77. from pprint import pprint
  78. import sys
  79. if len(sys.argv) > 1:
  80. devices = [InputDevice(path) for path in list_devices()]
  81. for device in devices:
  82. print(device.path, device.name, device.phys)
  83. pprint(device.capabilities(verbose=True))
  84. print()
  85. else:
  86. i = InputWrapper()
  87. while True:
  88. i.get(True)