|
| 1 | +"""DualSense input must match the teleop layout and fail safe on reconnect.""" |
| 2 | + |
| 3 | +import socket |
| 4 | +import sys |
| 5 | +import time |
| 6 | +from types import SimpleNamespace |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from waybionic_teleop import gamepad, sdl_bridge |
| 11 | + |
| 12 | + |
| 13 | +class FakeController: |
| 14 | + """An SDL controller whose input can change during a test.""" |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + """Start with neutral controls and a connected device.""" |
| 18 | + self.axes = {} |
| 19 | + self.buttons = {} |
| 20 | + self.connected = True |
| 21 | + self.rumbles = [] |
| 22 | + |
| 23 | + def get_axis(self, index): |
| 24 | + """Return a simulated SDL axis.""" |
| 25 | + return self.axes.get(index, 0) |
| 26 | + |
| 27 | + def get_button(self, index): |
| 28 | + """Return a simulated SDL button.""" |
| 29 | + return self.buttons.get(index, False) |
| 30 | + |
| 31 | + def attached(self): |
| 32 | + """Report whether the simulated device is connected.""" |
| 33 | + return self.connected |
| 34 | + |
| 35 | + def rumble(self, low, high, duration): |
| 36 | + """Record a simulated SDL rumble request.""" |
| 37 | + self.rumbles.append((low, high, duration)) |
| 38 | + return True |
| 39 | + |
| 40 | + def stop_rumble(self): |
| 41 | + """Record a simulated SDL rumble stop.""" |
| 42 | + self.rumbles.append('stop') |
| 43 | + |
| 44 | + def quit(self): # noqa: A003 |
| 45 | + """Match the SDL controller cleanup API.""" |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +class FakeSDL: |
| 50 | + """Expose the controller constants used by the production bridge.""" |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + """Assign test constants and start without devices.""" |
| 54 | + self.devices = [] |
| 55 | + for index, name in enumerate(sdl_bridge.AXIS_NAMES): |
| 56 | + setattr(self, 'CONTROLLER_AXIS_' + name, index) |
| 57 | + for index, name in enumerate(sdl_bridge.BUTTON_NAMES.values()): |
| 58 | + setattr(self, 'CONTROLLER_BUTTON_' + name, index) |
| 59 | + |
| 60 | + def get_count(self): |
| 61 | + """Return the current SDL device count.""" |
| 62 | + return len(self.devices) |
| 63 | + |
| 64 | + def is_controller(self, index): |
| 65 | + """Mark every test device as an SDL controller.""" |
| 66 | + return True |
| 67 | + |
| 68 | + def Controller(self, index): |
| 69 | + """Open one simulated SDL controller.""" |
| 70 | + return self.devices[index] |
| 71 | + |
| 72 | + def name_forindex(self, index): |
| 73 | + """Name the test controller like a DualSense.""" |
| 74 | + return 'DualSense Wireless Controller' |
| 75 | + |
| 76 | + |
| 77 | +def pressed(buttons): |
| 78 | + """List normalized names of pressed buttons.""" |
| 79 | + return [name for name, value in zip(gamepad.BUTTONS, buttons) if value] |
| 80 | + |
| 81 | + |
| 82 | +def fake_pygame(sdl): |
| 83 | + """Expose SDL constants where Pygame 2.6 publishes them.""" |
| 84 | + pygame = SimpleNamespace(event=SimpleNamespace(pump=lambda: None)) |
| 85 | + for name in sdl_bridge.AXIS_NAMES: |
| 86 | + setattr(pygame, 'CONTROLLER_AXIS_' + name, |
| 87 | + getattr(sdl, 'CONTROLLER_AXIS_' + name)) |
| 88 | + for name in sdl_bridge.BUTTON_NAMES.values(): |
| 89 | + setattr(pygame, 'CONTROLLER_BUTTON_' + name, |
| 90 | + getattr(sdl, 'CONTROLLER_BUTTON_' + name)) |
| 91 | + return pygame |
| 92 | + |
| 93 | + |
| 94 | +def test_dualsense_controls_match_existing_teleop_layout(): |
| 95 | + """Keep DualSense axes and buttons compatible with teleop packets.""" |
| 96 | + sdl = FakeSDL() |
| 97 | + pad = FakeController() |
| 98 | + pad.axes = {sdl.CONTROLLER_AXIS_LEFTX: -32768, |
| 99 | + sdl.CONTROLLER_AXIS_LEFTY: -32768, |
| 100 | + sdl.CONTROLLER_AXIS_RIGHTX: 16384, |
| 101 | + sdl.CONTROLLER_AXIS_RIGHTY: 32767, |
| 102 | + sdl.CONTROLLER_AXIS_TRIGGERLEFT: 32768} |
| 103 | + pad.buttons = {sdl.CONTROLLER_BUTTON_A: True, # Cross |
| 104 | + sdl.CONTROLLER_BUTTON_B: True, # Circle |
| 105 | + sdl.CONTROLLER_BUTTON_Y: True, # Triangle |
| 106 | + sdl.CONTROLLER_BUTTON_START: True, # Options |
| 107 | + sdl.CONTROLLER_BUTTON_DPAD_UP: True} |
| 108 | + axes, buttons = sdl_bridge.to_joy(pad, sdl) |
| 109 | + assert axes == pytest.approx([1.0, 1.0, -0.5, -32767 / 32768, -1.0, 0.0]) |
| 110 | + assert pressed(buttons) == ['a', 'b', 'y', 'start', 'dpad_up'] |
| 111 | + _, connected, unpacked_axes, unpacked_buttons = gamepad.unpack( |
| 112 | + gamepad.pack(1, axes, buttons)) |
| 113 | + assert connected |
| 114 | + assert unpacked_axes == pytest.approx(axes) |
| 115 | + assert unpacked_buttons == buttons |
| 116 | + |
| 117 | + |
| 118 | +def test_disconnect_sends_neutral_and_reconnect_requires_release(): |
| 119 | + """Do not expose controls until a new device has been released.""" |
| 120 | + sdl = FakeSDL() |
| 121 | + first = FakeController() |
| 122 | + sdl.devices = [first] |
| 123 | + bridge = sdl_bridge.SDLBridge(fake_pygame(sdl), sdl) |
| 124 | + assert bridge.poll(0.0)[2] is False # neutral reset |
| 125 | + assert bridge.poll(0.01)[2] is True |
| 126 | + first.buttons[sdl.CONTROLLER_BUTTON_START] = True |
| 127 | + assert 'start' in pressed(bridge.poll(0.02)[1]) |
| 128 | + |
| 129 | + first.connected = False |
| 130 | + axes, buttons, connected, _ = bridge.poll(0.03) |
| 131 | + assert not connected and (axes, buttons) == sdl_bridge.NEUTRAL |
| 132 | + |
| 133 | + second = FakeController() |
| 134 | + second.buttons[sdl.CONTROLLER_BUTTON_START] = True |
| 135 | + sdl.devices = [second] |
| 136 | + # Held Options cannot enable teleop on reconnect. |
| 137 | + assert bridge.poll(1.04)[2] is False |
| 138 | + second.buttons.clear() |
| 139 | + assert bridge.poll(1.05)[2] is False # wait for a fresh Options press |
| 140 | + assert bridge.poll(1.06)[2] is True |
| 141 | + second.buttons[sdl.CONTROLLER_BUTTON_START] = True |
| 142 | + assert 'start' in pressed(bridge.poll(1.07)[1]) |
| 143 | + |
| 144 | + |
| 145 | +def test_trigger_or_stick_held_blocks_connection(): |
| 146 | + """A held control must block the reset after connection.""" |
| 147 | + sdl = FakeSDL() |
| 148 | + pad = FakeController() |
| 149 | + pad.axes[sdl.CONTROLLER_AXIS_TRIGGERLEFT] = 20000 |
| 150 | + sdl.devices = [pad] |
| 151 | + bridge = sdl_bridge.SDLBridge(fake_pygame(sdl), sdl) |
| 152 | + assert bridge.poll(0.0)[2] is False |
| 153 | + pad.axes.clear() |
| 154 | + pad.axes[sdl.CONTROLLER_AXIS_LEFTX] = 20000 |
| 155 | + assert bridge.poll(0.01)[2] is False |
| 156 | + pad.axes.clear() |
| 157 | + assert bridge.poll(0.02)[2] is False |
| 158 | + assert bridge.poll(0.03)[2] is True |
| 159 | + |
| 160 | + |
| 161 | +def test_missing_controller_reports_a_useful_status(): |
| 162 | + """Tell the operator when the bridge is running without a controller.""" |
| 163 | + sdl = FakeSDL() |
| 164 | + bridge = sdl_bridge.SDLBridge(fake_pygame(sdl), sdl) |
| 165 | + |
| 166 | + axes, buttons, connected, message = bridge.poll(0.0) |
| 167 | + |
| 168 | + assert not connected and (axes, buttons) == sdl_bridge.NEUTRAL |
| 169 | + assert message == ('No controller detected; connect a PS5 DualSense ' |
| 170 | + 'controller') |
| 171 | + assert bridge.poll(0.5)[3] is None |
| 172 | + |
| 173 | + |
| 174 | +def test_rumble_reaches_only_an_open_controller(): |
| 175 | + """Rumble for a second per request like the Xbox bridge; zero stops it.""" |
| 176 | + sdl = FakeSDL() |
| 177 | + bridge = sdl_bridge.SDLBridge(fake_pygame(sdl), sdl) |
| 178 | + assert bridge.rumble(1.0) is False |
| 179 | + pad = FakeController() |
| 180 | + sdl.devices = [pad] |
| 181 | + bridge.poll(0.0) |
| 182 | + assert bridge.rumble(0.5) is True |
| 183 | + assert bridge.rumble(0.0) is True |
| 184 | + assert pad.rumbles == [(0.5, 0.5, 1000), 'stop'] |
| 185 | + pad.rumble = lambda low, high, duration: False |
| 186 | + assert bridge.rumble(0.5) is False |
| 187 | + |
| 188 | + |
| 189 | +@pytest.mark.parametrize('bluetooth_rumble', [False, True]) |
| 190 | +def test_rumble_replies_do_not_interrupt_the_controller_packets( |
| 191 | + monkeypatch, bluetooth_rumble): |
| 192 | + """Only the ground station's replies rumble; the bridge keeps sending.""" |
| 193 | + sdl = FakeSDL() |
| 194 | + pad = FakeController() |
| 195 | + sdl.devices = [pad] |
| 196 | + |
| 197 | + def init(): |
| 198 | + expected = '1' if bluetooth_rumble else None |
| 199 | + assert sdl_bridge.os.environ.get('SDL_JOYSTICK_HIDAPI_PS5_RUMBLE') == expected |
| 200 | + sdl.init = init |
| 201 | + sdl.quit = lambda: None |
| 202 | + pygame = fake_pygame(sdl) |
| 203 | + pygame.HIDDEN = 0 |
| 204 | + pygame.display = SimpleNamespace( |
| 205 | + init=lambda: None, set_mode=lambda size, flags: None, |
| 206 | + quit=lambda: None) |
| 207 | + monkeypatch.setitem(sys.modules, 'pygame', pygame) |
| 208 | + monkeypatch.setitem(sys.modules, 'pygame._sdl2', |
| 209 | + SimpleNamespace(controller=sdl)) |
| 210 | + monkeypatch.setenv('SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS', '1') |
| 211 | + monkeypatch.delenv('SDL_JOYSTICK_HIDAPI_PS5_RUMBLE', raising=False) |
| 212 | + station = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 213 | + stranger = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 214 | + station.bind(('127.0.0.1', 0)) |
| 215 | + station.settimeout(2.0) |
| 216 | + packets = [] |
| 217 | + |
| 218 | + def sleep(seconds): |
| 219 | + data, address = station.recvfrom(64) |
| 220 | + packets.append(gamepad.unpack(data)) |
| 221 | + if len(packets) == 3: |
| 222 | + stranger.sendto(gamepad.pack_rumble(1.0), address) |
| 223 | + station.sendto(gamepad.pack_rumble(0.5), address) |
| 224 | + elif len(packets) == 6: |
| 225 | + raise KeyboardInterrupt |
| 226 | + |
| 227 | + monkeypatch.setattr(sdl_bridge, 'time', SimpleNamespace( |
| 228 | + monotonic=time.monotonic, sleep=sleep)) |
| 229 | + arguments = ['sdl_bridge', '--port', str(station.getsockname()[1])] |
| 230 | + if bluetooth_rumble: |
| 231 | + arguments.append('--bluetooth-rumble') |
| 232 | + monkeypatch.setattr(sys, 'argv', arguments) |
| 233 | + try: |
| 234 | + sdl_bridge.main() |
| 235 | + finally: |
| 236 | + station.close() |
| 237 | + stranger.close() |
| 238 | + assert [packet[1] for packet in packets] == [False] + [True] * 5 |
| 239 | + assert pad.rumbles == [(0.5, 0.5, 1000), 'stop'] |
0 commit comments