-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx360.py
More file actions
219 lines (187 loc) · 6.86 KB
/
x360.py
File metadata and controls
219 lines (187 loc) · 6.86 KB
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
214
215
216
217
218
219
from dataclasses import dataclass
import struct
from enum import Enum
from evdev import ecodes
class X360Surfaces(Enum):
A = ("A", ecodes.BTN_A)
B = ("B", ecodes.BTN_B)
X = ("X", ecodes.BTN_X)
Y = ("Y", ecodes.BTN_Y)
OPTIONS = ("OPTIONS", ecodes.BTN_SELECT)
XBOX = ("XBOX", ecodes.BTN_MODE)
START = ("START", ecodes.BTN_START)
STICK_L = ("STICK_L", ecodes.BTN_THUMBL)
STICK_R = ("STICK_R", ecodes.BTN_THUMBR)
BUMPER_L = (("BUMPER_L", ecodes.BTN_TL),)
BUMPER_R = ("BUMPER_R", ecodes.BTN_TR)
# Dpad
DPAD_UP = ("DPAD_UP", ecodes.ABS_HAT0Y) # -1
DPAD_DOWN = ("DPAD_DOWN", ecodes.ABS_HAT0Y) # 1
DPAD_LEFT = ("DPAD_LEFT", ecodes.ABS_HAT0X) # -1
DPAD_RIGHT = ("DPAD_RIGHT", ecodes.ABS_HAT0X) # 1
# Triggers
LEFT_TRIGGER = ("LEFT_TRIGGER", ecodes.ABS_BRAKE) # 8-bit 0-1024
RIGHT_TRIGGER = ("RIGHT_TRIGGER", ecodes.ABS_GAS) # 8-bit 0-1024
# Joystick axis
LEFT_JOYSTICK_X = ("LEFT_JOYSTICK_X", ecodes.ABS_X)
LEFT_JOYSTICK_Y = ("LEFT_JOYSTICK_Y", ecodes.ABS_Y)
RIGHT_JOYSTICK_X = ("RIGHT_JOYSTICK_X", ecodes.ABS_RZ)
RIGHT_JOYSTICK_Y = ("RIGHT_JOYSTICK_Y", ecodes.ABS_Z)
def is_axis(self):
return X360Surfaces[self.name] in (
X360Surfaces.LEFT_TRIGGER,
X360Surfaces.RIGHT_TRIGGER,
X360Surfaces.LEFT_JOYSTICK_X,
X360Surfaces.LEFT_JOYSTICK_Y,
X360Surfaces.RIGHT_JOYSTICK_X,
X360Surfaces.RIGHT_JOYSTICK_Y,
)
def is_button(self):
return not self.is_axis()
@dataclass
class BitPackedButton:
name: str
bit: int
value: bool = False
class BitPackedButtons:
def __init__(self, buttons):
self.buttons = buttons
def to_bytes_repr(self) -> int:
result = 0
for b in self.buttons:
if b.value:
result |= 1 << b.bit
return result & 0xFF
@dataclass
class Axis:
value: int = 0 # default
bits: int = 8 # triggers are 8-bit by default
def to_u8(self) -> int:
return max(0, min(255, self.value))
@dataclass
class JoystickAxis:
value: int = 0
def to_i16(self) -> int:
return max(-32767, min(32767, self.value))
@dataclass
class JoystickState:
x: JoystickAxis
y: JoystickAxis
class XboxButtonState:
def __init__(self):
self.a = BitPackedButton("A", 0x04)
self.b = BitPackedButton("B", 0x05)
self.x = BitPackedButton("X", 0x06)
self.y = BitPackedButton("Y", 0x07)
self.lb = BitPackedButton("LB", 0x00)
self.rb = BitPackedButton("RB", 0x01)
self.l3 = BitPackedButton("L3", 0x06)
self.r3 = BitPackedButton("R3", 0x07)
self.start = BitPackedButton("START", 0x04)
self.options = BitPackedButton("OPTIONS", 0x05)
self.xbox = BitPackedButton("XBOX", 0x02)
self.dpad_up = BitPackedButton("DPAD_UP", 0x00)
self.dpad_down = BitPackedButton("DPAD_DOWN", 0x01)
self.dpad_left = BitPackedButton("DPAD_LEFT", 0x02)
self.dpad_right = BitPackedButton("DPAD_RIGHT", 0x03)
self.all = [
self.a,
self.b,
self.x,
self.y,
self.lb,
self.rb,
self.l3,
self.r3,
self.start,
self.options,
self.xbox,
self.dpad_up,
self.dpad_down,
self.dpad_left,
self.dpad_right,
]
def get_control_byte_2(self) -> int:
return BitPackedButtons(
[
self.dpad_up,
self.dpad_down,
self.dpad_left,
self.dpad_right,
self.start,
self.options,
self.l3,
self.r3,
]
).to_bytes_repr()
def get_control_byte_3(self) -> int:
return BitPackedButtons(
[
self.lb,
self.rb,
self.xbox,
self.a,
self.b,
self.x,
self.y,
]
).to_bytes_repr()
def get_button(self, name: str):
"""Return a BitPackedButton by its name (case-insensitive)."""
name = name.upper()
for btn in self.all:
if btn.name.upper() == name:
return btn
raise ValueError(f"No button found with name '{name}'")
class XboxControllerState:
def __init__(self):
self.buttons = XboxButtonState()
self.left_trigger = Axis(0, 8)
self.right_trigger = Axis(0, 8)
self.left_joystick = JoystickState(JoystickAxis(0), JoystickAxis(0))
self.right_joystick = JoystickState(JoystickAxis(0), JoystickAxis(0))
def to_packet(self) -> bytes:
packet = bytearray(20)
packet[0] = 0x00 # Report ID
packet[1] = 0x14 # Length
packet[2] = self.buttons.get_control_byte_2()
packet[3] = self.buttons.get_control_byte_3()
packet[4] = self.left_trigger.to_u8()
packet[5] = self.right_trigger.to_u8()
# pack little-endian signed 16-bit joystick values
packet[6:8] = struct.pack("<h", self.left_joystick.x.to_i16())
packet[8:10] = struct.pack("<h", self.left_joystick.y.to_i16())
packet[10:12] = struct.pack("<h", self.right_joystick.x.to_i16())
packet[12:14] = struct.pack("<h", self.right_joystick.y.to_i16())
return bytes(packet)
def by_enum(
self, enum_member: X360Surfaces
) -> Axis | JoystickAxis | BitPackedButton:
"""Return the corresponding controller attribute for the given X360Surfaces enum."""
mapping = {
X360Surfaces.A: self.buttons.a,
X360Surfaces.B: self.buttons.b,
X360Surfaces.X: self.buttons.x,
X360Surfaces.Y: self.buttons.y,
X360Surfaces.BUMPER_L: self.buttons.lb,
X360Surfaces.BUMPER_R: self.buttons.rb,
X360Surfaces.STICK_L: self.buttons.l3,
X360Surfaces.STICK_R: self.buttons.r3,
X360Surfaces.START: self.buttons.start,
X360Surfaces.OPTIONS: self.buttons.options,
X360Surfaces.XBOX: self.buttons.xbox,
X360Surfaces.DPAD_UP: self.buttons.dpad_up,
X360Surfaces.DPAD_DOWN: self.buttons.dpad_down,
X360Surfaces.DPAD_LEFT: self.buttons.dpad_left,
X360Surfaces.DPAD_RIGHT: self.buttons.dpad_right,
X360Surfaces.LEFT_TRIGGER: self.left_trigger,
X360Surfaces.RIGHT_TRIGGER: self.right_trigger,
X360Surfaces.LEFT_JOYSTICK_X: self.left_joystick.x,
X360Surfaces.LEFT_JOYSTICK_Y: self.left_joystick.y,
X360Surfaces.RIGHT_JOYSTICK_X: self.right_joystick.x,
X360Surfaces.RIGHT_JOYSTICK_Y: self.right_joystick.y,
}
try:
return mapping[enum_member]
except KeyError:
raise ValueError(f"No control found for enum {enum_member}")