-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
184 lines (170 loc) · 6.8 KB
/
commands.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from time import sleep
from globals import *
from config import *
import inputs
from report import report
def handle_button_input_command(btn:str, value:str):
global pressed_buttons
try:
butIdx = int(btn)
value = int(value)
if value >= 0:
pressed_buttons.add(butIdx)
except Exception as e:
print(f'Error reading button command ({btn}={value}): {e}')
def handle_joystick_axis_input_command(axis:str, value:str):
global gamepad_axes_values
try:
value = int(value)
gamepad_axes_values[axis] = value
except Exception as e:
print(f'Error reading joystick axes command ({axis}={value}): {e}')
def handle_volume_input_command(value: str):
if 'mute' == value:
pressed_buttons.add(BUTTON_VOL_MUTE)
else:
try:
vol = int(value)
if vol < 0:
pressed_buttons.add(BUTTON_VOL_DOWN)
elif vol > 0:
pressed_buttons.add(BUTTON_VOL_UP)
except ValueError as e:
print(f'Error reading volume command (vol={value}): {e}')
def handle_button_mapping_command(digital_in:str, btn_str:str) -> None:
try:
inputs.set_button_mappings({int(btn_str): digital_in})
except ValueError as ve:
print(f'Error mapping button command ({digital_in}={btn_str}): {ve}')
def handle_joystick_mapping_command(analog_in:str, axis:str) -> None:
inputs.set_joystick_mappings({axis: analog_in})
def parse_int_value(value:str) -> int:
try:
value = int(value)
except Exception as e:
print(e)
return 0
return value
def parse_float_value(value:str) -> float:
try:
value = float(value)
except Exception as e:
print(e)
return 0.0
return value
def reset_gamepad_axes_values():
global gamepad_axes_values
gamepad_axes_values.update({ 'x':0, 'y':0, 'z':0, 'r_z':0 })
def process_commands(**cmds):
global pressed_buttons, gamepad_axes_values
global digital_ins, analog_ins
pressed_buttons.clear()
reset_gamepad_axes_values()
pre_wait = 0.0
post_wait = 0.5
hold_time = 0.5
for input, value in cmds.items():
if len(input) > 3 and input[0:3] == 'btn':
# This is a digital button input value, i.e. btn1/btn2/ ... /btn15/btn16
handle_button_input_command(input[3:], value)
elif input in ['x', 'y', 'z', 'r_z']:
# This is an analog input axis value, i.e. x/y/z/r_x
handle_joystick_axis_input_command(input, value)
elif input in analog_ins.keys():
# This is an analog input->joystick axis remap
handle_joystick_mapping_command(input, value)
elif input in digital_ins.keys():
# This is a digital input->button remap
handle_button_mapping_command(input, value)
elif input == 'vol':
# This is a volume value, +ve, -ve or 'mute'
handle_volume_input_command(value)
elif input == 'hold':
# Hold control(s) for a period of time
hold_time = parse_float_value(value)
elif input == 'pre':
# Wait for a period of time BEFORE changing any values
pre_wait = parse_float_value(value)
elif input == 'post':
# Wait for a period of time AFTER changing (and resetting) any values
post_wait = parse_float_value(value)
# pre-wait period
sleep(pre_wait)
# debug
print(f'pressed_buttons={pressed_buttons}')
print(f'gamepad_axes_values={gamepad_axes_values}')
report()
# hold before releasing all buttons and centring axes
sleep(hold_time)
pressed_buttons.clear()
reset_gamepad_axes_values()
report()
# post-wait period
sleep(post_wait)
def configure_emulation_station():
"""
Send the necessary commands to automate EmulationStation 'Configure Input'
"""
cmds = [
{ f'btn{BUTTON_SELECT}' : 1, 'hold' : 3, 'post' : 1 }, # Initial 'hold any button'
{ f'btn{BUTTON_HAT_UP}' : 1, 'post' : 1 },
{ f'btn{BUTTON_HAT_DOWN}' : 1, 'post' : 1 },
{ f'btn{BUTTON_HAT_LEFT}' : 1, 'post' : 1 },
{ f'btn{BUTTON_HAT_RIGHT}' : 1, 'post' : 1 },
{ f'btn{BUTTON_START}' : 1, 'post' : 1 },
{ f'btn{BUTTON_SELECT}' : 1, 'post' : 1 },
{ f'btn{BUTTON_EAST_A}' : 1, 'post' : 1 },
{ f'btn{BUTTON_SOUTH_B}' : 1, 'post' : 1 },
{ f'btn{BUTTON_NORTH_X}' : 1, 'post' : 1 },
{ f'btn{BUTTON_WEST_Y}' : 1, 'post' : 1 },
{ f'btn{BUTTON_SHOULDER_L}' : 1, 'post' : 1 },
{ f'btn{BUTTON_SHOULDER_R}' : 1, 'post' : 1 },
{ f'btn{BUTTON_TRIGGER_L}' : 1, 'post' : 1 },
{ f'btn{BUTTON_TRIGGER_R}' : 1, 'post' : 1 },
{ f'btn{BUTTON_THUMB_L}' : 1, 'post' : 1 },
{ f'btn{BUTTON_THUMB_R}' : 1, 'post' : 1 },
{ 'y' : -32767, 'post' : 1 },
{ 'y' : 32767, 'post' : 1 },
{ 'x' : -32767, 'post' : 1 },
{ 'x' : 32767, 'post' : 1 },
{ 'r_z' : -32767, 'post' : 1 },
{ 'r_z' : 32767, 'post' : 1 },
{ 'z' : -32767, 'post' : 1 },
{ 'z' : 32767, 'post' : 1 },
{ f'btn{BUTTON_SELECT}' : 1, 'post' : 1 }, # Hotkey
{ f'btn{BUTTON_EAST_A}' : 1 }, # 'OK' / Finish
]
for cmd in cmds:
process_commands(**cmd)
def configure_retroarch():
"""
Send the necessary commands to automate RetroArch 'Set All Controls' configuration
"""
cmds = [
{ f'btn{BUTTON_SOUTH_B}' : 1 },
{ f'btn{BUTTON_WEST_Y}' : 1 },
{ f'btn{BUTTON_SELECT}' : 1 },
{ f'btn{BUTTON_START}' : 1 },
{ f'btn{BUTTON_HAT_UP}' : 1 },
{ f'btn{BUTTON_HAT_DOWN}' : 1 },
{ f'btn{BUTTON_HAT_LEFT}' : 1 },
{ f'btn{BUTTON_HAT_RIGHT}' : 1 },
{ f'btn{BUTTON_EAST_A}' : 1 },
{ f'btn{BUTTON_NORTH_X}' : 1 },
{ f'btn{BUTTON_SHOULDER_L}' : 1 },
{ f'btn{BUTTON_SHOULDER_R}' : 1 },
{ f'btn{BUTTON_TRIGGER_L}' : 1 },
{ f'btn{BUTTON_TRIGGER_R}' : 1 },
{ f'btn{BUTTON_THUMB_L}' : 1 },
{ f'btn{BUTTON_THUMB_R}' : 1 },
{ 'x' : 32767 },
{ 'x' : -32767 },
{ 'y' : 32767 },
{ 'y' : -32767 },
{ 'z' : 32767 },
{ 'z' : -32767 },
{ 'r_z' : 32767 },
{ 'r_z' : -32767 },
]
for cmd in cmds:
process_commands(**cmd)