-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnesgamepad.py
111 lines (94 loc) · 3.2 KB
/
nesgamepad.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
import time
import pygame
class Gamepad(object):
"""Class designed specifically as a joystick for for the
TOMEE NES USB controller Item# M05178
"""
RELEASE_DELAY = 0.250 #ms
AXIS_DELAY = 2500
QUIT = 0
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
A = 5
B = 6
START = 7
SELECT = 8
DISCONNECTED = 9
def __init__(self, js_num, *args, **kwargs):
self.js = pygame.joystick.Joystick(js_num, *args, **kwargs)
self.js_num = js_num
self.js.init()
self.done = False
def actions(self):
delay_y_current = False
delay_y_next = True
prev_y_time = 0
self.wait_for_all_buttons_released = False
self.wait_until = 0
neutral = True
pressed = 0
last_update = pygame.time.get_ticks()
while not self.done:
for event in pygame.event.get():
pass
#pygame.joystick.quit()
#pygame.joystick.init()
#while pygame.joystick.get_count() == 0:
# yield self.DISCONNECTED
# pygame.joystick.quit()
# pygame.joystick.init()
#self.js = pygame.joystick.Joystick(self.js_num)
#self.js.init()
x_axis, y_axis = (self.js.get_axis(i) for i in range(2))
b_a, b_b, b_select, b_start = (self.js.get_button(i)
for i in (0, 1, 8, 9))
if all((b_a, b_b, b_select, b_start)):
self.wait_for_all_buttons_released = True
self.wait_until = time.time() + self.RELEASE_DELAY
if self.wait_for_all_buttons_released:
if time.time() < self.wait_until:
continue
else:
self.wait_for_all_buttons_released = False
if b_b:
yield self.B
elif b_a:
yield self.A
elif b_select:
yield self.SELECT
elif b_start:
yield self.START
else:
move = False
if y_axis == 0:
neutral = True
pressed = 0
else:
if neutral:
move = True
neutral = False
else:
pressed += pygame.time.get_ticks() - last_update
if pressed > self.AXIS_DELAY:
move = True
if move:
if y_axis > 0.5:
yield self.DOWN
elif y_axis < -0.5:
yield self.UP
last_update = pygame.time.get_ticks()
else:
yield None
if y_axis == 0:
if x_axis > 0.5:
yield self.RIGHT
elif x_axis < -0.5:
yield self.LEFT
def stop(self):
self.done = True
def wait_for_release(self):
"""Wait for RELEASE_DELAY time for all buttons to be released"""
self.wait_for_all_buttons_released = True
self.wait_until = time.time() + self.RELEASE_DELAY