-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
31 lines (25 loc) · 933 Bytes
/
server.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
import asyncio
import websockets
import json
import vgamepad as vg
gamepad = vg.VX360Gamepad()
def calculate_joystick_position_value_from_percentage_on_canvas(a):
if 0 <= a <= 100:
result = (a - 50) * 655.36
return int(result)
else:
return None
async def listener(websocket, path):
async for message in websocket:
point = json.loads(message)
#print(point)
y_value = calculate_joystick_position_value_from_percentage_on_canvas(point["y"])
if y_value <= -1:
y_value = y_value+1
gamepad.right_joystick(x_value=calculate_joystick_position_value_from_percentage_on_canvas(point["x"]), y_value=-1 * y_value)
gamepad.update()
#await websocket.send(message)
async def main():
async with websockets.serve(listener, "0.0.0.0", 8765):
await asyncio.Future() # run forever
asyncio.run(main())