This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
joystick.py
145 lines (107 loc) · 4.8 KB
/
joystick.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#######################################
# Code coded by Mike Doty
#
# If you want trackball checking, you will
# have to code it yourself. Sorry!
#
# Oh, and it just grabs the first joystick.
# Yes, that makes me lazy.
#
# Released February 8, 2008.
#######################################
import pygame
from pygame.locals import *
class App:
def __init__(self):
pygame.init()
pygame.display.set_caption("Joystick Analyzer")
# Set up the joystick
pygame.joystick.init()
self.my_joystick = None
self.joystick_names = []
# Enumerate joysticks
for i in range(0, pygame.joystick.get_count()):
self.joystick_names.append(pygame.joystick.Joystick(i).get_name())
print self.joystick_names
# By default, load the first available joystick.
if (len(self.joystick_names) > 0):
self.my_joystick = pygame.joystick.Joystick(0)
self.my_joystick.init()
max_joy = max(self.my_joystick.get_numaxes(),
self.my_joystick.get_numbuttons(),
self.my_joystick.get_numhats())
self.screen = pygame.display.set_mode( (max_joy * 30 + 10, 170) )
self.font = pygame.font.SysFont("Courier", 20)
# A couple of joystick functions...
def check_axis(self, p_axis):
if (self.my_joystick):
if (p_axis < self.my_joystick.get_numaxes()):
return self.my_joystick.get_axis(p_axis)
return 0
def check_button(self, p_button):
if (self.my_joystick):
if (p_button < self.my_joystick.get_numbuttons()):
return self.my_joystick.get_button(p_button)
return False
def check_hat(self, p_hat):
if (self.my_joystick):
if (p_hat < self.my_joystick.get_numhats()):
return self.my_joystick.get_hat(p_hat)
return (0, 0)
def draw_text(self, text, x, y, color, align_right=False):
surface = self.font.render(text, True, color, (0, 0, 0))
surface.set_colorkey( (0, 0, 0) )
self.screen.blit(surface, (x, y))
def center_text(self, text, x, y, color):
surface = self.font.render(text, True, color, (0, 0, 0))
surface.set_colorkey( (0, 0, 0) )
self.screen.blit(surface, (x - surface.get_width() / 2,
y - surface.get_height() / 2))
def main(self):
while (True):
self.g_keys = pygame.event.get()
self.screen.fill(0)
for event in self.g_keys:
if (event.type == KEYDOWN and event.key == K_ESCAPE):
self.quit()
return
elif (event.type == QUIT):
self.quit()
return
self.draw_text("Joystick Name: %s" % self.joystick_names[0],
5, 5, (0, 255, 0))
self.draw_text("Axes (%d)" % self.my_joystick.get_numaxes(),
5, 25, (255, 255, 255))
for i in range(0, self.my_joystick.get_numaxes()):
if (self.my_joystick.get_axis(i)):
pygame.draw.circle(self.screen, (0, 0, 200),
(20 + (i * 30), 50), 10, 0)
else:
pygame.draw.circle(self.screen, (255, 0, 0),
(20 + (i * 30), 50), 10, 0)
self.center_text("%d" % i, 20 + (i * 30), 50, (255, 255, 255))
self.draw_text("Buttons (%d)" % self.my_joystick.get_numbuttons(),
5, 75, (255, 255, 255))
for i in range(0, self.my_joystick.get_numbuttons()):
if (self.my_joystick.get_button(i)):
pygame.draw.circle(self.screen, (0, 0, 200),
(20 + (i * 30), 100), 10, 0)
else:
pygame.draw.circle(self.screen, (255, 0, 0),
(20 + (i * 30), 100), 10, 0)
self.center_text("%d" % i, 20 + (i * 30), 100, (255, 255, 255))
self.draw_text("POV Hats (%d)" % self.my_joystick.get_numhats(),
5, 125, (255, 255, 255))
for i in range(0, self.my_joystick.get_numhats()):
if (self.my_joystick.get_hat(i) != (0, 0)):
pygame.draw.circle(self.screen, (0, 0, 200),
(20 + (i * 30), 150), 10, 0)
else:
pygame.draw.circle(self.screen, (255, 0, 0),
(20 + (i * 30), 150), 10, 0)
self.center_text("%d" % i, 20 + (i * 30), 100, (255, 255, 255))
pygame.display.flip()
def quit(self):
pygame.display.quit()
app = App()
app.main()