-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcadapter.py
213 lines (177 loc) · 7.45 KB
/
gcadapter.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from __future__ import annotations
import platform
import usb.core
import usb.util
class GCControllerStatus:
STICK_DEFAULT_VALUE = 0x80
SHOULDER_DEFAULT_VALUE = 0
def __init__(self):
self.connected: bool = False
self.get_origin: bool = False
self.a: bool = False
self.b: bool = False
self.x: bool = False
self.y: bool = False
self.start: bool = False
self.d_left: bool = False
self.d_right: bool = False
self.d_down: bool = False
self.d_up: bool = False
self.z: bool = False
self.r: bool = False
self.l: bool = False
self.joystick_x: int = self.STICK_DEFAULT_VALUE
self.joystick_y: int = self.STICK_DEFAULT_VALUE
self.c_stick_x: int = self.STICK_DEFAULT_VALUE
self.c_stick_y: int = self.STICK_DEFAULT_VALUE
self.l_analog: int = self.SHOULDER_DEFAULT_VALUE
self.r_analog: int = self.SHOULDER_DEFAULT_VALUE
def __str__(self):
return (
f"connected: {self.connected}\n"
f"get_origin: {self.get_origin}\n"
f"A: {self.a}\n"
f"B: {self.b}\n"
f"X: {self.x}\n"
f"Y: {self.y}\n"
f"START: {self.start}\n"
f"D-LEFT: {self.d_left}\n"
f"D-RIGHT: {self.d_right}\n"
f"D-DOWN: {self.d_down}\n"
f"D-UP: {self.d_up}\n"
f"Z: {self.z}\n"
f"R: {self.r}\n"
f"L: {self.l}\n"
f"SX: {self.joystick_x}\n"
f"SY: {self.joystick_y}\n"
f"CX: {self.c_stick_x}\n"
f"CY: {self.c_stick_y}\n"
f"LA: {self.l_analog}\n"
f"RA: {self.r_analog}"
)
class GCAdapter:
MAX_CONTROLLERS = 4
DEFAULT_TIMEOUT = 16
def __init__(self):
self.polling = False
self.dev: usb.core.Device = usb.core.find(idVendor=0x057E, idProduct=0x0337)
if self.dev is None:
raise IOError("Could not find device")
# adapter has one configuration descriptor - claim it
if platform.system() == "Linux":
if self.dev.is_kernel_driver_active(0):
try:
self.dev.detach_kernel_driver(0)
except usb.core.USBError as e:
raise IOError(f"Could not detach kernel driver: {e}") from e
self.dev.reset()
self.dev.set_configuration()
cfg: usb.core.Configuration = self.dev.get_active_configuration()
# configuration has one interface descriptor - get it
intf: usb.core.Interface = cfg[(0, 0)]
# the interface descriptor exposes two endpoints - one for writing and one for reading
# get a handle to both
self.out_ep: usb.core.Endpoint = usb.util.find_descriptor(
intf,
custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress)
== usb.util.ENDPOINT_OUT,
)
self.in_ep: usb.core.Endpoint = usb.util.find_descriptor(
intf,
custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress)
== usb.util.ENDPOINT_IN,
)
if self.out_ep is None or self.in_ep is None:
raise IOError("Could not find endpoints")
def start_polling(self) -> None:
if self.out_ep.write([0x13], timeout=self.DEFAULT_TIMEOUT) != 1:
raise IOError
self.polling = True
def stop_polling(self) -> None:
if self.out_ep.write([0x14], timeout=self.DEFAULT_TIMEOUT) != 1:
raise IOError
bytes = self.in_ep.read(self.in_ep.wMaxPacketSize, timeout=self.DEFAULT_TIMEOUT)
if (len(bytes)) != 2 or bytes[0] != 0x24:
raise IOError
self.polling = False
def get_origins(self) -> list[GCControllerStatus]:
polling: bool = self.polling
self.stop_polling()
if self.out_ep.write([0x12], timeout=self.DEFAULT_TIMEOUT) != 1:
raise IOError
bytes = self.in_ep.read(self.in_ep.wMaxPacketSize, timeout=self.DEFAULT_TIMEOUT)
if len(bytes) != 25 or bytes[0] != 0x22:
raise IOError
if polling:
self.start_polling()
origins = [
GCControllerStatus(),
GCControllerStatus(),
GCControllerStatus(),
GCControllerStatus(),
]
for chan in range(self.MAX_CONTROLLERS):
origins[chan].joystick_x = bytes[1 + (chan * 6)]
origins[chan].joystick_y = bytes[1 + (chan * 6) + 1]
origins[chan].c_stick_x = bytes[1 + (chan * 6) + 2]
origins[chan].c_stick_y = bytes[1 + (chan * 6) + 3]
origins[chan].l_analog = bytes[1 + (chan * 6) + 4]
origins[chan].r_analog = bytes[1 + (chan * 6) + 5]
return origins
def get_status(self) -> list[GCControllerStatus]:
bytes = self.in_ep.read(self.in_ep.wMaxPacketSize, timeout=self.DEFAULT_TIMEOUT)
if len(bytes) != self.in_ep.wMaxPacketSize or bytes[0] != 0x21:
raise IOError
statuses = [
GCControllerStatus(),
GCControllerStatus(),
GCControllerStatus(),
GCControllerStatus(),
]
for chan in range(self.MAX_CONTROLLERS):
statuses[chan].connected = ((bytes[1 + (9 * chan)] >> 4) & 0x03) > 0
buttons: int = (
bytes[(1 + (9 * chan) + 1)] | bytes[(1 + (9 * chan) + 2)] << 8
)
statuses[chan].a = (buttons & (1 << 0)) != 0
statuses[chan].b = (buttons & (1 << 1)) != 0
statuses[chan].x = (buttons & (1 << 2)) != 0
statuses[chan].y = (buttons & (1 << 3)) != 0
statuses[chan].d_left = (buttons & (1 << 4)) != 0
statuses[chan].d_right = (buttons & (1 << 5)) != 0
statuses[chan].d_down = (buttons & (1 << 6)) != 0
statuses[chan].d_up = (buttons & (1 << 7)) != 0
statuses[chan].start = (buttons & (1 << 8)) != 0
statuses[chan].z = (buttons & (1 << 9)) != 0
statuses[chan].r = (buttons & (1 << 10)) != 0
statuses[chan].l = (buttons & (1 << 11)) != 0
statuses[chan].joystick_x = bytes[1 + (9 * chan) + 3]
statuses[chan].joystick_y = bytes[1 + (9 * chan) + 4]
statuses[chan].c_stick_x = bytes[1 + (9 * chan) + 5]
statuses[chan].c_stick_y = bytes[1 + (9 * chan) + 6]
statuses[chan].l_analog = bytes[1 + (9 * chan) + 7]
statuses[chan].r_analog = bytes[1 + (9 * chan) + 8]
# weird bug where sometimes all analog values are 0 despite connected
if statuses[chan].connected and all(
x == 0
for x in (
statuses[chan].joystick_x,
statuses[chan].joystick_y,
statuses[chan].c_stick_x,
statuses[chan].c_stick_y,
statuses[chan].l_analog,
statuses[chan].r_analog,
)
):
raise IOError
return statuses
def set_rumble(self, r1: bool, r2: bool, r3: bool, r4: bool) -> None:
if (
self.out_ep.write(
[0x11, 1 if r1 else 0, 1 if r2 else 0, 1 if r3 else 0, 1 if r4 else 0]
)
!= 1
):
raise IOError
def disconnect(self):
self.dev.reset()