-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
79 lines (61 loc) · 1.98 KB
/
controller.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
from xinput import *
import time
import serial
import string
import math
robo1 = serial.Serial('COM5',9600,write_timeout=2)
robo1.flushInput()
# return a bytes
# maintains same ratio of value to max, with different max.
def ratio_to_new_size(value, sourceLength, newLength):
value = abs(value)
if sourceLength >= newLength:
value = int(value / pow(2, sourceLength - newLength))
else:
value = int(value * pow(2, newLength - sourceLength))
return value
def transmit_axis(type, value):
indicator = 0
if type == 'r_thumb_y':
if value > 0:
indicator = 1
else:
indicator = 4
if type == 'l_thumb_y':
if value > 0:
indicator = 2
else:
indicator = 3
#if thumb axis
if type != 'left_trigger' and type != 'right_trigger':
# value = ratio_to_new_size(value, 15, 8)
value = int(abs(value) * 255)
print(value)
robo1.write(indicator.to_bytes(1,byteorder='big'))
#print(indicator.to_bytes(1,byteorder='big'))
robo1.write(value.to_bytes(1,byteorder='big'))
#print(value.to_bytes(1,byteorder='big'))
# print(bytearray(indicator.to_bytes(1,byteorder='big')).append(value))
def joystick_readout():
joysticks = XInputJoystick.enumerate_devices()
for stick in joysticks:
@stick.event
def on_button(button, pressed):
print('Joystick',stick.device_number,'button',button,' state:', pressed)
@stick.event
def on_axis(axis, value):
print('Joystick',stick.device_number,'axis',axis ,value)
transmit_axis(axis, value)
print("Connected")
while True:
for stick in joysticks:
stick.dispatch_events()
# transmit_axis('r_thumb_y',32767)
# # time.sleep(1)
# transmit_axis('l_thumb_y',32767)
# time.sleep(1)
# transmit_axis('r_thumb_y',-6160)
# # time.sleep(1)
# transmit_axis('l_thumb_y',-6160)
# print("transmitted")
joystick_readout()