-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_controller.py
More file actions
executable file
·105 lines (84 loc) · 3.3 KB
/
test_controller.py
File metadata and controls
executable file
·105 lines (84 loc) · 3.3 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
102
103
#!/usr/bin/env python3
"""
Test Xbox Controller with Pygame
"""
import pygame
import sys
import time
def main():
print("🎮 Testing Xbox Controller...")
# Initialize pygame
pygame.init()
pygame.joystick.init()
# Check for joysticks
joystick_count = pygame.joystick.get_count()
print(f"Found {joystick_count} joystick(s)")
if joystick_count == 0:
print("❌ No joysticks found!")
print("Make sure your Xbox controller is connected and try:")
print("1. Unplug and replug the controller")
print("2. Check: ls /dev/input/js*")
print("3. Check: lsusb | grep -i xbox")
return
# Initialize the first joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
print(f"✅ Controller: {joystick.get_name()}")
print(f" Axes: {joystick.get_numaxes()}")
print(f" Buttons: {joystick.get_numbuttons()}")
print(f" Hats: {joystick.get_numhats()}")
print("\n🎮 Controller Test - Press buttons and move sticks!")
print(" Press ESC or close window to exit")
print(" Watch the values change as you use the controller")
# Create a small window
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Xbox Controller Test")
font = pygame.font.Font(None, 24)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# Clear screen
screen.fill((0, 0, 0))
y = 20
line_height = 25
# Display controller info
info_text = f"Controller: {joystick.get_name()}"
text = font.render(info_text, True, (255, 255, 255))
screen.blit(text, (10, y))
y += line_height
# Display axes (analog sticks and triggers)
for i in range(joystick.get_numaxes()):
axis_value = joystick.get_axis(i)
axis_text = f"Axis {i}: {axis_value:.3f}"
color = (0, 255, 0) if abs(axis_value) > 0.1 else (128, 128, 128)
text = font.render(axis_text, True, color)
screen.blit(text, (10, y))
y += line_height
# Display buttons
for i in range(joystick.get_numbuttons()):
button_pressed = joystick.get_button(i)
button_text = f"Button {i}: {'PRESSED' if button_pressed else 'released'}"
color = (255, 255, 0) if button_pressed else (128, 128, 128)
text = font.render(button_text, True, color)
screen.blit(text, (10, y))
y += line_height
# Display hats (D-pad)
for i in range(joystick.get_numhats()):
hat_value = joystick.get_hat(i)
hat_text = f"Hat {i}: {hat_value}"
color = (255, 0, 255) if hat_value != (0, 0) else (128, 128, 128)
text = font.render(hat_text, True, color)
screen.blit(text, (10, y))
y += line_height
pygame.display.flip()
clock.tick(60)
pygame.quit()
print("✅ Controller test completed!")
if __name__ == "__main__":
main()