-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_controller_server.py
More file actions
101 lines (76 loc) · 2.58 KB
/
Copy pathvirtual_controller_server.py
File metadata and controls
101 lines (76 loc) · 2.58 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
import pyvjoy
import asyncio
import json
import websockets
LISTEN_IP = "192.168.1.134"
PORT = 1337
DIRECTION_UP = 0
DIRECTION_RIGHT = 1
DIRECTION_DOWN = 2
DIRECTION_LEFT = 3
joystick = pyvjoy.VJoyDevice(1)
joystick.set_axis(pyvjoy.HID_USAGE_X, 16384) # Center position for X axis
joystick.set_axis(pyvjoy.HID_USAGE_Y, 16384) # Center position for Y axis
azimuth = 0.0
move_speed = 0.0
jump = False
def to_axis_value(float_value):
return int(float_value * 16384 + 16384)
def get_cardinal_direction(angle):
# Normalize the angle to be within 0-360
angle = angle % 360
# Determine the direction based on the angle
if angle >= 315 or angle < 45:
print("UP")
return DIRECTION_UP
elif 45 <= angle < 135:
print("RIGHT")
return DIRECTION_RIGHT
elif 135 <= angle < 225:
print("DOWN")
return DIRECTION_DOWN
elif 225 <= angle < 315:
print("LEFT")
return DIRECTION_LEFT
def update_controller():
global joystick, azimuth, move_speed, jump
direction = get_cardinal_direction(azimuth)
joystick_x_value = 0
joystick_y_value = 0
if direction == DIRECTION_UP:
joystick_y_value = move_speed
elif direction == DIRECTION_DOWN:
joystick_y_value = -move_speed
elif direction == DIRECTION_RIGHT:
joystick_x_value = move_speed
elif direction == DIRECTION_LEFT:
joystick_x_value = -move_speed
joystick.set_axis(pyvjoy.HID_USAGE_X, to_axis_value(joystick_x_value))
joystick.set_axis(pyvjoy.HID_USAGE_Y, to_axis_value(joystick_y_value))
joystick.set_button(3, jump) # jump
# todo: grab
# todo: menu navigation stuff
async def controller_loop():
while True:
update_controller()
await asyncio.sleep(1 / 30) # Wait for 1/30th of a second
async def handle_client(websocket, path):
print("New connection at", path)
async for message in websocket:
print(message)
parsed = json.loads(message)
if path == "/crocs":
global move_speed, jump
move_speed = parsed["speed"]
jump = parsed["jump"]
elif path == "/compass":
global azimuth
azimuth = parsed["azimuth"]
async def main():
async with websockets.serve(handle_client, LISTEN_IP, PORT, ping_interval=None):
print("WebSocket server started on", "ws://" + LISTEN_IP + ":" + str(PORT))
# Start the controller loop in the background
await asyncio.create_task(controller_loop())
await asyncio.Future() # Run forever
if __name__ == "__main__":
asyncio.run(main())