From 3884e44d2778c2d4952021d258f5397f1b6dc577 Mon Sep 17 00:00:00 2001 From: Parth Shah Date: Fri, 29 Jul 2022 00:03:33 -0700 Subject: [PATCH] added multi controller support --- server.py | 25 ++- static/script.js | 142 +++++++++--------- templates/index.html | 4 - vgamepad/__pycache__/__init__.cpython-39.pyc | Bin 425 -> 429 bytes .../win/__pycache__/__init__.cpython-39.pyc | Bin 170 -> 174 bytes .../__pycache__/vigem_client.cpython-39.pyc | Bin 2086 -> 2090 bytes .../__pycache__/vigem_commons.cpython-39.pyc | Bin 6339 -> 6343 bytes .../virtual_gamepad.cpython-39.pyc | Bin 15795 -> 15799 bytes 8 files changed, 88 insertions(+), 83 deletions(-) diff --git a/server.py b/server.py index 87cdc3a..bceda34 100644 --- a/server.py +++ b/server.py @@ -13,9 +13,7 @@ static_folder='static', template_folder='templates') sock = Sock(app) -gamepad = None - -notif = None +# gamepad = None def create_image(): @@ -35,17 +33,21 @@ def my_callback(client, target, large_motor, small_motor, led_number, user_data) 'led': led_number, } ws.send(json.dumps(notif)) - global gamepad + # global gamepad + gamepad = None if gamepad == None: gamepad = vg.VX360Gamepad() gamepad.register_notification(callback_function=my_callback) while True: if not ws.connected: - gamepad.close() + print("Disconnected") + gamepad.unregister_notification() + del gamepad gamepad = None - break - message = ws.receive() - if message.startswith('{'): + ws.close() + return + message = ws.receive(0.1) + if message and message.startswith('{'): message = json.loads(message) gamepad.left_joystick( int(message['lx']*32767), -int(message['ly']*32767)) @@ -133,6 +135,13 @@ def my_callback(client, target, large_motor, small_motor, led_number, user_data) gamepad.release_button( button=vg.XUSB_BUTTON.XUSB_GAMEPAD_GUIDE) gamepad.update() + elif message and message == 'disconnect': + print("Disconnected") + gamepad.unregister_notification() + del gamepad + gamepad = None + ws.close() + return @app.route('/') diff --git a/static/script.js b/static/script.js index 19416cb..0983b60 100644 --- a/static/script.js +++ b/static/script.js @@ -1,30 +1,8 @@ -var noSleep = new NoSleep(); - -document.addEventListener('click', function enableNoSleep() { - document.removeEventListener('click', enableNoSleep, false); - noSleep.enable(); -}, false); - const sections = { pending: document.getElementById('Pending'), active: document.getElementById('Active'), }; -let gamepadIndex; -window.addEventListener('gamepadconnected', (event) => { - sections.pending.classList.remove('visible'); - sections.active.classList.add('visible'); - noSleep.enable(); - gamepadIndex = event.gamepad.index; -}); - -window.addEventListener('gamepaddisconnected', () => { - sections.active.classList.remove('visible'); - sections.pending.classList.add('visible'); - noSleep.disable(); - gamepadIndex = undefined; -}); - const buttons = { 0: false, 1: false, @@ -77,10 +55,57 @@ const buttonElems = { let url = window.location.href.replace("http", "ws"); -let socket = new WebSocket(url + '/controller'); -socket.onopen = function (e) { - console.log("[open] Connection established"); -}; + +let gamepadIndex; +let socket +let connectSocket = () => { + socket = new WebSocket(url + '/controller'); + socket.onopen = function (e) { + console.log("[open] Connection established"); + }; + socket.onmessage = function (event) { + console.log(`[message] Data received from server: ${event.data}`); + let data = JSON.parse(event.data); + console.log(data.lm); + console.log(data.sm); + // vibrate controller + if (gamepadIndex !== undefined) { + vibrate(data).then(() => { + if (data.lm > 0 || data.sm > 0) { + vibrate(data); + } + }); + } + }; + + socket.onclose = function (event) { + connected = false; + if (event.wasClean) { + console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`); + } else { + console.log('[close] Connection died'); + } + }; + + socket.onerror = function (error) { + console.log(`[error] ${error.message}`); + }; +} + +connectSocket(); +window.addEventListener('gamepadconnected', (event) => { + sections.pending.classList.remove('visible'); + sections.active.classList.add('visible'); + gamepadIndex = 0; +}); + +window.addEventListener('gamepaddisconnected', () => { + sections.active.classList.remove('visible'); + sections.pending.classList.add('visible'); + gamepadIndex = undefined; +}); + + let vibrate = async (data) => { if (navigator.getGamepads()[gamepadIndex].vibrationActuator) @@ -92,34 +117,6 @@ let vibrate = async (data) => { }); } -socket.onmessage = function (event) { - console.log(`[message] Data received from server: ${event.data}`); - let data = JSON.parse(event.data); - console.log(data.lm); - console.log(data.sm); - // vibrate controller - if (gamepadIndex !== undefined) { - vibrate(data).then(() => { - if (data.lm > 0 || data.sm > 0) { - vibrate(data); - } - }); - } -}; - -socket.onclose = function (event) { - connected = false; - if (event.wasClean) { - console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`); - } else { - console.log('[close] Connection died'); - } -}; - -socket.onerror = function (error) { - console.log(`[error] ${error.message}`); -}; - const updateController = () => { for (let buttonIndex = 0; buttonIndex < 19; buttonIndex++) { const buttonValue = buttons[buttonIndex]; @@ -145,23 +142,26 @@ const updateController = () => { const update = () => { if (gamepadIndex !== undefined) { const myGamepad = navigator.getGamepads()[gamepadIndex]; - buttons["lx"] = myGamepad.axes[0]; - buttons["ly"] = myGamepad.axes[1]; - buttons["rx"] = myGamepad.axes[2]; - buttons["ry"] = myGamepad.axes[3]; - - myGamepad.buttons.map(e => e.pressed).forEach((isPressed, buttonIndex) => { - if (buttonIndex == 6 || buttonIndex == 7) { - buttons[buttonIndex] = myGamepad.buttons[buttonIndex].value; - } - else if (isPressed) { - buttons[buttonIndex] = true; - } else { - buttons[buttonIndex] = false; - } - }) - updateController(); - socket.send(JSON.stringify(buttons)); + if (myGamepad) { + buttons["lx"] = myGamepad.axes[0]; + buttons["ly"] = myGamepad.axes[1]; + buttons["rx"] = myGamepad.axes[2]; + buttons["ry"] = myGamepad.axes[3]; + + myGamepad.buttons.map(e => e.pressed).forEach((isPressed, buttonIndex) => { + if (buttonIndex == 6 || buttonIndex == 7) { + buttons[buttonIndex] = myGamepad.buttons[buttonIndex].value; + } + else if (isPressed) { + buttons[buttonIndex] = true; + } else { + buttons[buttonIndex] = false; + } + }) + updateController(); + if (socket.readyState == 1) + socket.send(JSON.stringify(buttons)); + } } } diff --git a/templates/index.html b/templates/index.html index 6548ade..f3ba4db 100644 --- a/templates/index.html +++ b/templates/index.html @@ -158,10 +158,6 @@

Controller Connected

- -