Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 = self.empty()
  18. devices = [InputDevice(path) for path in list_devices()]
  19. for device in devices:
  20. c = device.capabilities(absinfo=False)
  21. keep = False
  22. # check for axis events
  23. if ecodes.EV_ABS in c:
  24. # check for gamepad
  25. if ecodes.ABS_X in c[ecodes.EV_ABS]:
  26. print("Gamepad detected: ", device.name)
  27. keep = True
  28. # check for key events
  29. if ecodes.EV_KEY in c:
  30. # check for arrow keys
  31. if ecodes.KEY_LEFT in c[ecodes.EV_KEY]:
  32. print("Keyboard detected:", device.name)
  33. keep = True
  34. if not keep:
  35. continue
  36. d = InputDevice(device.path)
  37. self.devices.append(d)
  38. self.selector.register(d, EVENT_READ)
  39. def empty(self):
  40. return {
  41. "left": False,
  42. "right": False,
  43. "up": False,
  44. "down": False,
  45. "a": False,
  46. "b": False,
  47. "x": False,
  48. "y": False,
  49. "l": False,
  50. "r": False,
  51. "start": False,
  52. "select": False,
  53. "l2": False,
  54. "r2": False,
  55. }
  56. def get(self, verbose = False):
  57. for key, mask in self.selector.select(0):
  58. device = key.fileobj
  59. for event in device.read():
  60. if event.type == ecodes.EV_KEY:
  61. v = False
  62. if event.value != 0:
  63. v = True
  64. if (event.code == ecodes.KEY_LEFT) or (event.code == ecodes.BTN_WEST):
  65. self.keys["left"] = v
  66. elif (event.code == ecodes.KEY_RIGHT) or (event.code == ecodes.BTN_EAST):
  67. self.keys["right"] = v
  68. elif (event.code == ecodes.KEY_UP) or (event.code == ecodes.BTN_NORTH):
  69. self.keys["up"] = v
  70. elif (event.code == ecodes.KEY_DOWN) or (event.code == ecodes.BTN_SOUTH):
  71. self.keys["down"] = v
  72. elif (event.code == ecodes.KEY_SPACE) or (event.code == ecodes.BTN_A) or (event.code == ecodes.BTN_THUMB):
  73. self.keys["a"] = v
  74. elif (event.code == ecodes.KEY_LEFTCTRL) or (event.code == ecodes.BTN_B) or (event.code == ecodes.BTN_THUMB2):
  75. self.keys["b"] = v
  76. elif (event.code == ecodes.KEY_LEFTALT) or (event.code == ecodes.BTN_X) or (event.code == ecodes.BTN_JOYSTICK) or (event.code == ecodes.BTN_TRIGGER):
  77. self.keys["x"] = v
  78. elif (event.code == ecodes.KEY_ENTER) or (event.code == ecodes.BTN_Y) or (event.code == ecodes.BTN_TOP):
  79. self.keys["y"] = v
  80. elif (event.code == ecodes.BTN_PINKIE) or (event.code == ecodes.KEY_D):
  81. self.keys["r"] = v
  82. elif (event.code == ecodes.BTN_TOP2) or (event.code == ecodes.KEY_A):
  83. self.keys["l"] = v
  84. elif event.code == ecodes.BTN_BASE:
  85. self.keys["r2"] = v
  86. elif event.code == ecodes.BTN_BASE2:
  87. self.keys["l2"] = v
  88. elif (event.code == ecodes.BTN_BASE4) or (event.code == ecodes.KEY_BACKSPACE):
  89. self.keys["start"] = v
  90. elif event.code == ecodes.BTN_BASE3:
  91. self.keys["select"] = v
  92. elif event.type == ecodes.EV_ABS:
  93. # TODO 8bit hardcoded
  94. if event.code == ecodes.ABS_X:
  95. if event.value > 200:
  96. self.keys["right"] = True
  97. self.keys["left"] = False
  98. elif event.value < 100:
  99. self.keys["right"] = False
  100. self.keys["left"] = True
  101. else:
  102. self.keys["right"] = False
  103. self.keys["left"] = False
  104. elif event.code == ecodes.ABS_Y:
  105. if event.value > 200:
  106. self.keys["down"] = True
  107. self.keys["up"] = False
  108. elif event.value < 100:
  109. self.keys["down"] = False
  110. self.keys["up"] = True
  111. else:
  112. self.keys["down"] = False
  113. self.keys["up"] = False
  114. elif event.type == ecodes.EV_SYN:
  115. continue # don't print sync events
  116. if verbose:
  117. print(categorize(event))
  118. print(event)
  119. print()
  120. return self.keys
  121. if __name__ == "__main__":
  122. from pprint import pprint
  123. import sys
  124. if (len(sys.argv) > 1) and (sys.argv[1] == "list"):
  125. devices = [InputDevice(path) for path in list_devices()]
  126. for device in devices:
  127. print(device.path, device.name, device.phys)
  128. pprint(device.capabilities(verbose=True))
  129. print()
  130. elif (len(sys.argv) > 1) and (sys.argv[1] == "events"):
  131. i = InputWrapper()
  132. last_keys = None
  133. while True:
  134. i.get(True)
  135. else:
  136. i = InputWrapper()
  137. last_keys = None
  138. while True:
  139. keys = i.get()
  140. if (last_keys == None) or (keys != last_keys):
  141. pprint(keys)
  142. print()
  143. last_keys = keys.copy()