Skip to content

Instantly share code, notes, and snippets.

@yasamoka
Last active December 21, 2018 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasamoka/e97dcc11c76ec0c3cb3b60fb221ce09d to your computer and use it in GitHub Desktop.
Save yasamoka/e97dcc11c76ec0c3cb3b60fb221ce09d to your computer and use it in GitHub Desktop.
import time
from queue import Queue
from inputs import devices
QUEUESIZE = 20
TARGET_AXIS = "LX"
AXES = {
"ABS_X": "LX",
"ABS_Y": "LY",
"ABS_RX": "RX",
"ABS_RY": "RY",
"ABS_Z": "LT",
"ABS_RZ": "RT"
}
def main():
gamepads = devices.gamepads
print(gamepads)
assert len(gamepads) == 1
gamepad = devices.gamepads[0]
last_state = None
q = Queue(maxsize=QUEUESIZE)
while True:
if q.qsize() == QUEUESIZE:
break
events = gamepad.read()
for event in events:
if event.ev_type == "Absolute":
xbox_axis = AXES[event.code]
if xbox_axis == TARGET_AXIS and event.state != last_state:
current_time = time.time()
q.put(current_time)
last_state = event.state
while True:
events = gamepad.read()
for event in events:
if event.ev_type == "Absolute":
xbox_axis = AXES[event.code]
if xbox_axis == TARGET_AXIS and event.state != last_state:
current_time = time.time()
prev_time = q.get()
q.put(current_time)
timedelta = current_time - prev_time
rate = (QUEUESIZE - 1) / timedelta
print("Polling rate: {:0.2f} Hz".format(rate))
last_state = event.state
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment