-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGamePad.py
108 lines (96 loc) · 3.13 KB
/
GamePad.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/local/bin/python
import struct
import time
import sys
import uinput
import subprocess
kbd = subprocess.Popen("grep -E 'Handlers|EV=' /proc/bus/input/devices | grep -B1 'EV=120013' | grep -Eo 'event[0-9]+'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read().replace("\n","")
infile_path = "/dev/input/" + kbd
#long int, long int, unsigned short, unsigned short, unsigned int
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)
#open file in binary mode
in_file = open(infile_path, "rb")
events = (
uinput.BTN_A,
uinput.BTN_B,
uinput.BTN_X,
uinput.BTN_Y,
uinput.BTN_TL,
uinput.BTN_TR,
uinput.ABS_X + (-1, 1, 0, 0),
uinput.ABS_Y + (-1, 1, 0, 0),
uinput.ABS_Z + (-1, 1, 0, 0),
uinput.ABS_RZ + (-1, 1, 0, 0),
)
buttons = {
38 : uinput.BTN_A,
28 : uinput.BTN_A,
25 : uinput.BTN_B,
1 : uinput.BTN_B,
37 : uinput.BTN_X,
24 : uinput.BTN_Y,
4 : uinput.BTN_TL,
10 : uinput.BTN_TR,
}
axes0 = {
32 : [1,0], # Right
30 : [-1,0],# Left
17 : [0,-1],# Up
31 : [0,1], # Down
}
axes1 = {
106 : [1,0], # Right
105 : [-1,0],# Left
103 : [0,-1],# Up
108 : [0,1], # Down
}
print "+---------------------------+"
print "| ChromePad |"
print "| KEYMAP |"
print "+---------------------------+"
print "| W | L STICK UP |"
print "| S | L STICK DWN |"
print "| A | L STICK LFT |"
print "| D | L STICK RHT |"
print "| UP ARROW | R STICK UP |"
print "| DWN ARROW | R STICK DWN |"
print "| LFT ARROW | R STICK LFT |"
print "| RT ARROW | R STICK DWN |"
print "| L/ENTER | A |"
print "| P/ESC | B |"
print "| K | X |"
print "| O | Y |"
print "| 3 | LT |"
print "| 9 | RT |"
print "+---------------------------+"
prevcode = -1
prevval = -1
with uinput.Device(events) as device:
print "ChromePad Started. Start playing !!"
event = in_file.read(EVENT_SIZE)
while event:
(tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event)
if (type != 0 or code != 0 or value != 0) and (type == 1 and (prevcode != code or prevval != value) and value != 2 ):
if (code in axes0):
x,y = axes0[code]
if y == 0:
device.emit(uinput.ABS_X, x * value)
else:
device.emit(uinput.ABS_Y, y * value)
elif (code in axes1):
x,y = axes1[code]
if y == 0:
device.emit(uinput.ABS_Z, x * value)
else:
device.emit(uinput.ABS_RZ, y * value)
elif(code in buttons):
device.emit(buttons[code],value)
prevcode = code
prevval = value
try:
event = in_file.read(EVENT_SIZE)
except:
print "\nExiting ChromePad..."
exit()
in_file.close()