-
Notifications
You must be signed in to change notification settings - Fork 22
/
input_reader.py
67 lines (58 loc) · 2.15 KB
/
input_reader.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
import threading
from . import zeth_inputs
thread_running = False
events = []
def _input_reader_thread():
global thread_running
global events
while thread_running:
events.append(zeth_inputs.get_gamepad())
def start_input_reader():
global thread_running
if thread_running:
return
thread_running = True
thread = threading.Thread(target=_input_reader_thread)
thread.daemon = True
thread.start()
def stop_input_reader():
global thread_running
thread_running = False
def sample_input_reader(mario_inputs):
from . import config, input_value
if config['keyboard_control']:
mario_inputs.stickX = input_value['RIGHT']*1 - input_value['LEFT']*1
mario_inputs.stickY = input_value['DOWN']*1 - input_value['UP']*1
mario_inputs.buttonA = input_value['A']
mario_inputs.buttonB = input_value['B']
mario_inputs.buttonZ = input_value['C']
else:
while len(events) > 0 :
for event in events[0]:
if event.code == "ABS_X":
mario_inputs.stickX = _read_axis(float(event.state))
elif event.code == "ABS_Y":
mario_inputs.stickY = _read_axis(float(event.state))
elif event.code == "BTN_SOUTH":
if event.state == 1:
mario_inputs.buttonA = True
else:
mario_inputs.buttonA = False
elif event.code == "BTN_NORTH":
if event.state == 1:
mario_inputs.buttonB = True
else:
mario_inputs.buttonB = False
elif event.code == "BTN_TL":
if event.state == 1:
mario_inputs.buttonZ = True
else:
mario_inputs.buttonZ = False
#elif event.code != "SYN_REPORT":
# print(event.code + ':' + str(event.state))
events.pop(0)
def _read_axis(val):
val /= 32768.0
if val < 0.2 and val > -0.2:
return 0
return (val - 0.2) / 0.8 if val > 0.0 else (val + 0.2) / 0.8