-
Notifications
You must be signed in to change notification settings - Fork 2
/
gamepad_example.py
57 lines (44 loc) · 1.36 KB
/
gamepad_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Simple example showing how to get gamepad events."""
from __future__ import print_function
from inputs import get_gamepad
from utils import XboxController
import time
import matplotlib.pyplot as plt
import matplotlib
def main():
"""Just print out some event infomation when the gamepad is used."""
while 1:
events = get_gamepad()
for event in events:
# print(event.ev_type, event.code, event.state)
print(event.code)
def main2():
# test XboxController
controller = XboxController()
print('init')
values = {}
# determine average polling rate
start = time.time()
count = 0
while True:
try:
t = time.time() - start
values[t] = controller.read()
print(values[t])
count += 1
except KeyboardInterrupt:
print("Stopping due to Ctrl C Event")
break
end = time.time()
print(f"Average polling rate: {count/(end-start)} Hz ({count} events in {end-start} seconds)")
# plot everything on one graph
plt.figure()
plt.title("Xbox Controller")
plt.xlabel("Time (s)")
plt.ylabel("Value")
pairs = [(k, v) for k, v in values.items()]
plt.plot([i[0] for i in pairs], [i[1] for i in pairs])
plt.savefig("xboxcontroller.png")
if __name__ == "__main__":
matplotlib.use('Agg')
main2()