-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS4_Controller_Server.py
More file actions
36 lines (30 loc) · 1006 Bytes
/
PS4_Controller_Server.py
File metadata and controls
36 lines (30 loc) · 1006 Bytes
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
import pygame
import socket
# Initialize Pygame and the controller
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count() > 0:
controller = pygame.joystick.Joystick(0)
controller.init()
# Create a socket object
s = socket.socket()
# Bind the socket to a local host and port
s.bind(('localhost', 12345))
# Start listening for incoming connections
s.listen()
# Accept an incoming connection
conn, addr = s.accept()
# Send the controller data to the client
while True:
for event in pygame.event.get():
if event.type == pygame.JOYAXISMOTION:
# Get the axis data for the event
axis = event.axis
value = event.value
# Send the data to the client
conn.send(f"axis {axis} {value}".encode())
elif event.type == pygame.JOYBUTTONDOWN:
# Get the button data for the event
button = event.button
# Send the data to the client
conn.send(f"button {button}".encode())