2020-06-02 18:10:07 -05:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Import libraries
|
|
|
|
from inputs import devices
|
|
|
|
from inputs import get_gamepad
|
|
|
|
import threading
|
|
|
|
import socket
|
|
|
|
from termcolor import colored
|
2020-06-03 01:07:28 -05:00
|
|
|
import pickle
|
2020-06-03 20:19:43 -05:00
|
|
|
import re
|
2020-06-02 18:10:07 -05:00
|
|
|
# Define ip/port to use
|
2020-06-02 22:11:32 -05:00
|
|
|
ip = "192.168.122.1"
|
2020-06-02 18:10:07 -05:00
|
|
|
port = 2222
|
|
|
|
# Show available gamepads
|
|
|
|
print("Gamepads available:")
|
|
|
|
print(devices.gamepads)
|
2020-06-03 23:45:10 -05:00
|
|
|
#Define decimal formula
|
|
|
|
decimalFormula = "2/(32767 - -32768)*(event.state-32767)+1"
|
2020-06-02 18:10:07 -05:00
|
|
|
#Define globals
|
|
|
|
controllerData = ""
|
2020-06-03 12:18:51 -05:00
|
|
|
#Dictionary of of all possible values
|
|
|
|
controllerDataDict = {'AxisLx': 0, 'AxisLy': 0, 'AxisRx': 0, 'AxisRy': 0, 'BtnBack': 0, 'BtnStart': 0, 'BtnA': 0, 'BtnB': 0, 'BtnX': 0, 'BtnY': 0, 'BtnThumbL': 0, 'BtnThumbR': 0, 'BtnShoulderL': 0, 'BtnShoulderR': 0, 'Dpad': 0, 'TriggerL': 0, 'TriggerR': 0}
|
|
|
|
#Lookup table to convert values from the "inputs" library
|
2020-06-03 23:45:10 -05:00
|
|
|
lookup_table = {'BTN_SELECT': 'BtnBack', 'BTN_START': 'BtnStart', 'BTN_SOUTH': 'BtnA', 'BTN_EAST': 'BtnB', 'BTN_NORTH': 'BtnX', 'BTN_WEST': 'BtnY', 'BTN_THUMBL': 'BtnThumbL', 'BTN_THUMBR': 'BtnThumbR', 'BTN_TL': 'BtnShoulderL', 'BTN_TR': 'BtnShoulderR'}
|
2020-06-03 12:18:51 -05:00
|
|
|
def sendData():
|
|
|
|
#Pickle for transmittion
|
2020-06-03 23:45:10 -05:00
|
|
|
pickle.dump(controllerDataDict, file)
|
2020-06-03 20:19:43 -05:00
|
|
|
file.flush()
|
2020-06-03 14:15:09 -05:00
|
|
|
#Create Socket
|
2020-06-03 16:28:03 -05:00
|
|
|
socket = socket.create_server((ip, port))
|
2020-06-03 14:51:41 -05:00
|
|
|
#Wait for connections before continuing
|
|
|
|
print("Waiting for connection...")
|
2020-06-03 20:19:43 -05:00
|
|
|
connection, _ = socket.accept()
|
|
|
|
#Create pickler :D
|
|
|
|
file = connection.makefile(mode='wb')
|
|
|
|
pickler = pickle.Pickler(file)
|
|
|
|
file.flush()
|
|
|
|
print(connection)
|
|
|
|
print(type(connection))
|
|
|
|
#Immutiple socket? I don't think so...
|
|
|
|
iwantThatRaddr = re.findall(r"'(?<=raddr=\(')\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'", str(connection))
|
|
|
|
print(iwantThatRaddr)
|
|
|
|
print(colored('Connected by', 'red'), iwantThatRaddr[0])
|
2020-06-03 14:15:09 -05:00
|
|
|
print(controllerDataDict)
|
2020-06-03 20:19:43 -05:00
|
|
|
while True:
|
|
|
|
events = get_gamepad()
|
|
|
|
for event in events:
|
|
|
|
#controllerDataTuple = event.ev_type, event.code, event.state
|
|
|
|
controllerDataTuple = event.code, event.state
|
|
|
|
controllerData = controllerDataTuple
|
|
|
|
print(controllerData)
|
|
|
|
print(controllerDataDict)
|
|
|
|
#If event.code is SYN_REPORT, ignore it
|
|
|
|
if event.code == "SYN_REPORT":
|
|
|
|
continue
|
|
|
|
if event.code == "BTN_MODE":
|
|
|
|
print(colored('The home button is unimplemented on PYXinput, ignoring', 'red'))
|
|
|
|
continue
|
|
|
|
#Ugh, PYXinput and inputs handle the Dpad in the most annoying way possible
|
|
|
|
elif event.code == "ABS_HAT0X":
|
|
|
|
#if ABS_HAT0X is 0, then there is no input on the dpad
|
|
|
|
if event.state == 0:
|
|
|
|
controllerDataDict['Dpad'] = 0 #No input
|
|
|
|
if event.state == 1:
|
|
|
|
controllerDataDict['Dpad'] = 8 #Right
|
|
|
|
if event.state == -1:
|
|
|
|
controllerDataDict['Dpad'] = 4 #Left
|
|
|
|
sendData()
|
|
|
|
elif event.code == "ABS_HAT0Y":
|
|
|
|
#if ABS_HAT0Y is 0, then there is no input on the dpad
|
|
|
|
if event.state == 0:
|
|
|
|
controllerDataDict['Dpad'] = 0 #No input
|
|
|
|
if event.state == 1:
|
|
|
|
controllerDataDict['Dpad'] = 2 #Down
|
|
|
|
if event.state == -1:
|
|
|
|
controllerDataDict['Dpad'] = 1 #Up
|
|
|
|
sendData()
|
2020-06-03 23:45:10 -05:00
|
|
|
# The joysticks and triggers' values need to be normalized before they are sent
|
|
|
|
#'ABS_X': 'AxisLx', 'ABS_Y': 'AxisLy', 'ABS_RX': 'AxisRx', 'ABS_RY': 'AxisRy'
|
|
|
|
#Left Joystick
|
|
|
|
elif event.code == 'ABS_X': # Left Joystick, left+right
|
|
|
|
controllerDataDict['AxisLx'] = 2/(32767 - -32768)*(event.state-32767)+1
|
|
|
|
sendData()
|
|
|
|
elif event.code == 'ABS_Y': # Left Joystick, up+down
|
|
|
|
controllerDataDict['AxisLy'] = - 2/(32767 - -32768)*(event.state-32767)+1-2
|
|
|
|
sendData()
|
|
|
|
#Right Joystick
|
|
|
|
elif event.code == 'ABS_RX': # Left Joystick, left+right
|
|
|
|
controllerDataDict['AxisRx'] = 2/(32767 - -32768)*(event.state-32767)+1
|
|
|
|
sendData()
|
|
|
|
elif event.code == 'ABS_RY': # Left Joystick, up+down
|
|
|
|
controllerDataDict['AxisRy'] = - 2/(32767 - -32768)*(event.state-32767)+1-2
|
|
|
|
sendData()
|
|
|
|
#Now, for the triggers
|
|
|
|
#'ABS_Z': 'TriggerL', 'ABS_RZ': 'TriggerR'
|
|
|
|
elif event.code == 'ABS_Z':
|
|
|
|
controllerDataDict['TriggerL'] = float(event.state) / 1023
|
|
|
|
sendData()
|
|
|
|
elif event.code == 'ABS_RZ':
|
|
|
|
controllerDataDict['TriggerR'] = float(event.state) / 1023
|
|
|
|
sendData()
|
2020-06-03 20:19:43 -05:00
|
|
|
else:
|
2020-06-03 23:45:10 -05:00
|
|
|
# Add button values to controllerDataDict
|
2020-06-03 20:19:43 -05:00
|
|
|
controllerDataDict[lookup_table[str(event.code)]] = event.state
|
|
|
|
sendData()
|