-
Notifications
You must be signed in to change notification settings - Fork 22
/
input_reader_win.py
62 lines (54 loc) · 1.9 KB
/
input_reader_win.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
import os
from threading import Thread
from subprocess import PIPE, Popen
from queue import Queue
g_proc = None
def stop_input_reader():
global g_proc
if g_proc != None:
g_proc.stdout.close()
g_proc.kill()
g_proc = None
def start_input_reader():
global g_proc
stop_input_reader()
this_path = os.path.dirname(os.path.realpath(__file__))
exe_path = os.path.join(this_path, 'lib', 'controller.exe')
g_proc = Popen([exe_path], shell=False, stdin=PIPE, stdout=PIPE)
g_proc.stdin.write('\n'.encode())
g_proc.stdin.flush()
def sample_input_reader(mario_inputs):
global g_proc
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:
line = ""
if g_proc.poll() is None:
line = g_proc.stdout.readline().decode('utf-8')
g_proc.stdin.write('\n'.encode())
g_proc.stdin.flush()
vals = [int(x) for x in line.split()]
if len(vals) < 5:
_sample_empty_inputs(mario_inputs)
return
mario_inputs.stickX = _read_axis(float(vals[0]))
mario_inputs.stickY = _read_axis(float(vals[1]))
mario_inputs.buttonA = vals[2] != 0
mario_inputs.buttonB = vals[3] != 0
mario_inputs.buttonZ = vals[4] != 0
def _sample_empty_inputs(mario_inputs):
mario_inputs.stickX = 0.0
mario_inputs.stickY = 0.0
mario_inputs.buttonA = False
mario_inputs.buttonB = False
mario_inputs.buttonZ = False
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